1

如何使用 Java 以编程方式从 Eclipse .web 项目中导出 WAR 文件?

由于复杂的项目结构(ProjectX.web 依赖于 ProjectX.java),我 对战蚁任务有很大的问题,而且我对WebComponentExportWizard的实现感到非常困惑。

是否有任何 WTP API 可供使用?(像这个旧版本http://www.eclipse.org/webtools/jst/components/j2ee/api/j2ee_operationsAPI.html

4

2 回答 2

2

经过一番激烈的斗争,我设法通过这种方法获得了战争文件:

@SuppressWarnings("restriction")
public static void exportWar(IProject webProject) throws CoreException {
    WebComponentExportDataModelProvider modelProvider = new WebComponentExportDataModelProvider();

    IDataModel dataModel = DataModelFactory.createDataModel(modelProvider);
    dataModel.setBooleanProperty(IJ2EEComponentExportDataModelProperties.EXPORT_SOURCE_FILES, false);
    dataModel.setBooleanProperty(IJ2EEComponentExportDataModelProperties.OVERWRITE_EXISTING, true);
    dataModel.setStringProperty(IJ2EEComponentExportDataModelProperties.PROJECT_NAME, webProject.getName());

    dataModel.setStringProperty(IJ2EEComponentExportDataModelProperties.ARCHIVE_DESTINATION, webProject
            .getLocation().append(webProject.getName()).addFileExtension("war").toOSString());
    dataModel.setProperty(
            IJ2EEComponentExportDataModelProperties.COMPONENT,
            ComponentCore.createComponent(webProject));

    IDataModelOperation modelOperation = dataModel.getDefaultOperation();
    try {
        log.debug("Start  the export war operation");
        modelOperation.execute(null, null);
    }
    catch (ExecutionException e) {
        log.error("Error when exporting .war project", e);
    }
}
于 2013-06-19T16:47:37.007 回答
0

org.eclipse.wst.server.core.util.PublishHelper.publishZip()像下面这样使用它,它对我有用。

IPath war = workDirectory.append("app.war");
PublishHelper publishHelper = new PublishHelper(null);
J2EEFlexProjDeployable deployable =
        new J2EEFlexProjDeployable(project, ComponentCore.createComponent(project));
publishHelper.publishZip(deployable.members(), war, null);
于 2017-04-07T17:50:03.230 回答