eclipse插件導出war包中遇到的問題以及解決方式

最近爲了配合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

相關文章
相關標籤/搜索