2014-05-09 57 views
3

我想創建一個應用程序的jar,但運行到一個問題,因爲對spring框架的依賴。特別是,xml模式的命名空間是有問題的。你得到了臭名昭著的NamespaceHandler問題:創建一個與春天依賴關係的超級jar

Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/c] 

爲了創建(簡單)尤伯杯罐子,Creating a bundle jar with ant,但如果你有春天的依賴由於彈簧罐子有文件,如spring.handlers這不起作用,spring.schemas和spring.tooling在他們的許多jar文件的META-INF目錄中。我相信命名空間的解析依賴於這些文件。

überjar似乎以某種方式包含所有必要的文件,但我猜測運行時只看到一個。

例如,我的尤伯杯罐子顯示一個罐子-tf(部分)

META-INF/spring.handlers 
META-INF/spring.schemas 
META-INF/spring.tooling 
META-INF/license.txt 
META-INF/notice.txt 
META-INF/spring.factories 
META-INF/spring.handlers 
META-INF/spring.schemas 
META-INF/spring.tooling 
META-INF/license.txt 
META-INF/notice.txt 
META-INF/license.txt 
META-INF/notice.txt 
META-INF/spring.handlers 
META-INF/spring.schemas 
META-INF/spring.tooling 
META-INF/license.txt 
META-INF/notice.txt 
META-INF/license.txt 

所以:問題..是有辦法創造尤伯杯罐子有內部捆綁春瓶?我需要合併META-INF文件嗎?任何人都有與蟻羣構建文件合併的經驗?

+1

這是[另一種方式](http://stackoverflow.com/a/24083318/2208271)來做到這一點。 – Sithsu

回答

5

好吧..這是一個痛苦。

<target name="make-bundle" depends="jar"> 
    <!-- retrieve the dependencies --> 
    <ivy:retrieve conf="deploy" pattern="${dist.dir}/dependencies/[artifact].[ext]"/> 

    <delete dir="${dist.dir}/dependencies/uber" failonerror="false" /> 
    <mkdir dir="${dist.dir}/dependencies/uber"/> 
    <!-- iterate over the dependencies --> 
    <for param="file"> 
    <path> 
     <fileset dir="${dist.dir}/dependencies"> 
     <include name="**/*.jar"/> 
     </fileset> 
    </path> 
    <sequential> 
     <propertyregex override="yes" 
     property="jarname" input="@{file}" 
     regexp=".*/([^/]*)\.jar" replace="\1"/> 

     <!-- put the spring.* jars into special sub-directories --> 
     <mkdir dir="${dist.dir}/dependencies/${jarname}"/> 
     <unzip dest="${dist.dir}/dependencies/${jarname}" src="@{file}"> 
     <patternset> 
      <include name="**/META-INF/spring.*"/> 
     </patternset> 
     </unzip> 
     <!-- put everything else in the 'uber' directory --> 
     <unzip dest="${dist.dir}/dependencies/uber" src="@{file}"> 
     <patternset> 
      <exclude name="**/META-INF/spring.*"/> 
     </patternset> 
     </unzip> 
    </sequential> 
    </for> 

    <!-- build the concatenated spring.* files --> 
    <mkdir dir="${dist.dir}/dependencies/META-INF"/> 
    <concat destfile="${dist.dir}/dependencies/META-INF/spring.handlers" fixlastline="true"> 
    <fileset dir="${dist.dir}/dependencies/" includes="*/*/spring.handlers"/> 
    </concat> 
    <concat destfile="${dist.dir}/dependencies/META-INF/spring.schemas" fixlastline="true"> 
    <fileset dir="${dist.dir}/dependencies/" includes="*/*/spring.schemas"/> 
    </concat> 
    <concat destfile="${dist.dir}/dependencies/META-INF/spring.tooling" fixlastline="true"> 
    <fileset dir="${dist.dir}/dependencies/" includes="*/*/spring.tooling"/> 
    </concat> 
    <concat destfile="${dist.dir}/dependencies/META-INF/spring.factories" fixlastline="true"> 
    <fileset dir="${dist.dir}/dependencies/" includes="*/*/spring.factories"/> 
    </concat> 

    <!-- build the uber jar! --> 
    <delete file="${dist.dir}/myproject-with-dependencies.jar" failonerror="false"/> 
    <jar destfile="${dist.dir}/myproject-with-dependencies.jar"> 
    <!-- all dependency files except spring.* --> 
    <fileset dir="${dist.dir}/dependencies/uber"/> 
    <!-- the spring.* files --> 
    <fileset dir="${dist.dir}/dependencies/" includes="META-INF/*"/> 
    <!-- my project's classes & etc --> 
    <zipgroupfileset dir="${dist.dir}" includes="myproject.jar" /> 
    <manifest> 
     <attribute name="Main-Class" value="${main.class}"/> 
    </manifest> 
    </jar> 
</target> 
+2

你至少爲我節省了一天的工作時間。謝謝! – Spina

+0

我需要在哪裏添加這個定義? –

+0

您的ant build.xml文件。 – ticktock