eclipse插件导出war包中遇到的问题以及解决方式

最近为了配合docker 容器平台的工作,实现一个eclipse插件,领导希望的功能是,右键点击项目提供一个war包commit的按钮,点击按钮后,自动调用eclipse的打包过程,输出war包,最终将war包上传到服务端,实现自动化的快速部署。

好了,功能如上,在原有插件的基础上加了相应的菜单,在获取项目的过程就不在累述,最后调用的打包过程如下:

	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,然后右键导出 然后就出错了,一个很奇怪的错误,错误没有保存,但是最终的错误如下

org.eclipse.wst.common.modulecore.ModuleCoreNature cannot cast org.eclipse.wst.common.modulecore.ModuleCoreNature

跟踪代码发现,这里是在dataModel.setProperty()中,有个预处理的过程,其中有一部强转,爱强转的时候报了如上错误。

 

解决思路,一开始以为,我获取project的方式是不是有问题,拿到的project不是web项目,后来发现,取project的方法都是一样,直接通过workspace类获取,所以没有你问题,实在找不到解决办法,发现aws的eclipse插件有类似的功能,从github上下载下来看了下代码,发现他插件的处理方式几乎是一样的

   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的相关的依赖,具体如下

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相关的添加方式不一样

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包能正常输出了

转载于:https://my.oschina.net/u/268957/blog/689984

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值