创建一个具有spring依赖关系的超级jar

tic*_*ock 3 spring executable-jar

我正在尝试创建一个应用程序überjar,但由于依赖于spring框架而遇到了问题.特别是,xml架构的命名空间是有问题的.你得到臭名昭着的NamespaceHandler问题:

Unable to locate Spring NamespaceHandler for XML schema namespace [http://www.springframework.org/schema/c]
Run Code Online (Sandbox Code Playgroud)

对于创建(简单)uber jar,使用ant创建一个bundle jar,但是如果你有spring依赖,那么这不起作用,因为spring jar有spring.handlers,spring.schemas和spring.tooling等文件.许多jar文件的META-INF目录.我认为,命名空间解析依赖于这些文件.

überjar似乎以某种方式包含所有必要的文件,但我猜测运行时只看到一个.

例如,我的超级罐的jar -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
Run Code Online (Sandbox Code Playgroud)

那么:问题..有没有办法创造一个有弹簧罐捆绑在里面的超级罐?我需要合并META-INF文件吗?任何人都有与蚂蚁构建文件合并的经验吗?

tic*_*ock 6

嗯..这真是一种痛苦.

<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>
Run Code Online (Sandbox Code Playgroud)

  • 你救了我至少一天的工作.谢谢! (2认同)