最近为了配合docker 容器平台的工做,实现一个eclipse插件,领导但愿的功能是,右键点击项目提供一个war包commit的按钮,点击按钮后,自动调用eclipse的打包过程,输出war包,最终将war包上传到服务端,实现自动化的快速部署。java
好了,功能如上,在原有插件的基础上加了相应的菜单,在获取项目的过程就不在累述,最后调用的打包过程以下:git
public static void exportWar(IProject webProject) throws CoreException { webProject.build(IncrementalProjectBuilder.FULL_BUILD, null); WebComponentExportDataModelProvider modelProvider = new WebComponentExportDataModelProvider(); IDataModel dataModel = DataModelFactory.createDataModel(modelProvider); String projectName = webProject.getName(); //war包的位置 String destination_string = webProject.getLocation().append(projectName).addFileExtension("war").toOSString(); //datamodel的相关参数设置 dataModel.setStringProperty(IJ2EEComponentExportDataModelProperties.ARCHIVE_DESTINATION, destination_string); dataModel.setStringProperty(IJ2EEComponentExportDataModelProperties.PROJECT_NAME, projectName); dataModel.setProperty(IJ2EEComponentExportDataModelProperties.COMPONENT, ComponentCore.createComponent(webProject)); // dataModel.setBooleanProperty(IJ2EEComponentExportDataModelProperties.EXPORT_SOURCE_FILES, false); // dataModel.setBooleanProperty(IJ2EEComponentExportDataModelProperties.OVERWRITE_EXISTING, true); IDataModelOperation modelOperation = dataModel.getDefaultOperation(); try { //执行打包 modelOperation.execute(new NullProgressMonitor(), null); } catch (org.eclipse.core.commands.ExecutionException e) { e.printStackTrace(); } }
如上代码,我看到某些方法,须要的类基本都在eclipse目录下的plugin下面,所以,就想固然的将全部须要的jar包直接拷贝到了插件项目下的lib下,而且加入了buildpath,乍看之下项目已经不报错了,通常来讲,就能够进行操做了,使用eclipse的debug的方式,启动了插件,建立一个dynamic web project,而后右键导出 而后就出错了,一个很奇怪的错误,错误没有保存,可是最终的错误以下github
org.eclipse.wst.common.modulecore.ModuleCoreNature cannot cast org.eclipse.wst.common.modulecore.ModuleCoreNature
跟踪代码发现,这里是在dataModel.setProperty()中,有个预处理的过程,其中有一部强转,爱强转的时候报了如上错误。web
解决思路,一开始觉得,我获取project的方式是否是有问题,拿到的project不是web项目,后来发现,取project的方法都是同样,直接经过workspace类获取,因此没有你问题,实在找不到解决办法,发现aws的eclipse插件有相似的功能,从github上下载下来看了下代码,发现他插件的处理方式几乎是同样的sql
public static IPath exportProjectToWar(IProject project, IPath directory) { File tempFile; try { tempFile = File.createTempFile("aws-eclipse-", ".war", directory.toFile()); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException("Unable to create web application archive: " + e.getMessage(), e); } return exportProjectToWar(project, directory, tempFile.getName()); } public static IPath exportProjectToWar(IProject project, IPath directory, String fileName) { IDataModel dataModel = DataModelFactory.createDataModel(new WebComponentExportDataModelProvider()); if (directory.toFile().exists() == false) { if (directory.toFile().mkdirs() == false) { throw new RuntimeException("Unable to create temp directory for web application archive."); } } String filename = new File(directory.toFile(), fileName).getAbsolutePath(); dataModel.setProperty(IJ2EEComponentExportDataModelProperties.ARCHIVE_DESTINATION, filename); dataModel.setProperty(IJ2EEComponentExportDataModelProperties.PROJECT_NAME, project.getName()); try { IDataModelOperation operation = dataModel.getDefaultOperation(); operation.execute(new NullProgressMonitor(), null); } catch (ExecutionException e) { // TODO: better error handling e.printStackTrace(); throw new RuntimeException("Unable to create web application archive: " + e.getMessage(), e); } return new Path(filename); }
而后问题就卡这里,后来终于发现问题的所在,在aws的项目中,Plug-in Dependences中的包含了全部eclipse的相关的依赖,具体以下docker
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: AWS Elastic Beanstalk Eclipse Plugin Bundle-SymbolicName: com.amazonaws.eclipse.elasticbeanstalk;singleton:=true Bundle-Version: 1.0.0.qualifier Bundle-Activator: com.amazonaws.eclipse.elasticbeanstalk.ElasticBeanstalkPlugin Bundle-Vendor: Amazon Web Services Require-Bundle: org.eclipse.ui, org.eclipse.core.filesystem;bundle-version="1.2.0", org.eclipse.core.runtime;bundle-version="3.4.0", org.eclipse.core.resources;bundle-version="3.4.0", org.eclipse.jst.j2ee;bundle-version="1.1.0", org.eclipse.jst.j2ee.web;bundle-version="1.1.0", org.eclipse.jst.server.core;bundle-version="1.2.0", org.eclipse.wst.common.core;bundle-version="1.1.0", org.eclipse.wst.common.frameworks;bundle-version="1.1.0", org.eclipse.wst.server.core;bundle-version="1.1.0", org.eclipse.jdt.core;bundle-version="3.4.0", org.eclipse.jdt.launching;bundle-version="3.4.0", com.amazonaws.eclipse.core;bundle-version="2.3.1", com.amazonaws.eclipse.ec2;bundle-version="1.1.0", com.amazonaws.eclipse.sdk.ui;bundle-version="2.1.0", org.eclipse.core.databinding;bundle-version="1.2.0", org.eclipse.core.databinding.beans;bundle-version="1.2.0", org.eclipse.jface.databinding;bundle-version="1.3.0", org.eclipse.jface.text;bundle-version="3.4.0", org.eclipse.wst.common.project.facet.core;bundle-version="1.3.0", org.eclipse.debug.ui;bundle-version="3.4.0", org.eclipse.wst.server.ui;bundle-version="1.1.0", org.eclipse.core.net;bundle-version="1.2.0", org.eclipse.ui.forms;bundle-version="3.3.0", org.eclipse.jst.j2ee.ejb;bundle-version="1.1.0", org.eclipse.ui.navigator;bundle-version="3.2.0", com.jcraft.jsch;bundle-version="0.1.41" Bundle-RequiredExecutionEnvironment: J2SE-1.5 Export-Package: com.amazonaws.eclipse.elasticbeanstalk.server.ui, com.amazonaws.eclipse.elasticbeanstalk.deploy Bundle-ClassPath: ., src/, lib/jgit/jgit-1.3.0-aws-git-push.jar Bundle-ActivationPolicy: lazy
而个人项目中eclipse相关的添加方式不同app
Manifest-Version: 1.0 Bundle-ManifestVersion: 2 Bundle-Name: Sina App Engine Eclipse Plug-in Bundle-SymbolicName: com.sina.sae.eclipse; singleton:=true Bundle-Version: 1.1.0 Bundle-Vendor: Sina Require-Bundle: org.eclipse.ui, org.eclipse.core.runtime, org.eclipse.jdt.core, org.eclipse.core.resources, org.eclipse.jdt.ui, Bundle-RequiredExecutionEnvironment: JavaSE-1.7 Bundle-ActivationPolicy: lazy Import-Package: org.eclipse.ui.part Bundle-Activator: com.sina.sae.eclipse.SAEPlugin Bundle-ClassPath: lib/sequence-library-1.0.1.jar, lib/sqljet-1.1.0.jar, lib/svnkit-1.7.4.jar, lib/svnkit-cli-1.7.4.jar, lib/svnkit-javahl16-1.7.4.jar, lib/trilead-ssh2-1.0.0-build214.jar, lib/org.eclipse.jst.j2ee_1.1.850.v201506041749.jar, lib/org.eclipse.jst.j2ee.web_1.1.850.v201503311656.jar, lib/org.eclipse.ui.workbench_3.107.0.v20150825-2206.jar, lib/org.eclipse.wst.common.frameworks_1.2.200.v201304241450.jar, lib/org.eclipse.wst.common.modulecore_1.2.401.v201408132036.jar, lib/org.eclipse.jem.util_2.1.200.v201404021757.jar, lib/org.eclipse.wst.common.emfworkbench.integration_1.2.101.v201107081800.jar, lib/org.eclipse.emf.common_2.11.0.v20150805-0538.jar, lib/org.eclipse.emf.ecore_2.11.1.v20150805-0538.jar, lib/org.eclipse.emf.ecore.xmi_2.11.1.v20150805-0538.jar, lib/org.eclipse.jst.j2ee.core_1.3.200.v201505041449.jar, lib/org.eclipse.wst.common.core_1.2.0.v200908251833.jar, lib/org.eclipse.wst.common.project.facet.core_1.4.300.v201111030423.jar, .
注意aws中的方式,是在Require-Bundle: 中添加须要的模块,而我直接在Bundle-ClassPath中添加了eclipse的相关模块(其实这里是添加外部引用类的,不能添加eclipse相关的类),后来也采用了aws的所示的方式将依赖从新引用,在此debug就没事了,war包能正常输出了eclipse