spring - 创建一个具有 spring 依赖项的 uber jar

标签 spring executable-jar

我正在尝试创建一个 über jar 应用程序,但由于依赖于 spring 框架而遇到问题。特别是,xml 模式的 namespace 存在问题。你遇到了臭名昭著的 NamespaceHandler 问题:

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

用于创建(简单的) super jar ,Creating a bundle jar with ant ,但如果您有 spring 依赖项,这将不起作用,因为 spring jar 在其许多 jar 文件的 META-INF 目录中具有 spring.handlers、spring.schemas 和 spring.tooling 等文件。我相信命名空间解析取决于这些文件。

über jar 似乎以某种方式包含所有必需的文件,但我猜运行时只看到一个。

例如,我的 uber jar 的一个 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

所以:问题.. 有没有一种方法可以创建一个将 spring jar 捆绑在里面的 super jar ?我需要合并 META-INF 文件吗?任何人都有使用 ant builds 进行文件合并的经验吗?

最佳答案

嗯..这很痛苦。

<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>

关于spring - 创建一个具有 spring 依赖项的 uber jar,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23574979/

相关文章:

java - Spring中@Bean和Autowired有什么区别?

java - 构建和执行 JAR 文件错误 : could not find main class and unsupported major. 次要版本 52.0

java - 使用 JAR 导出图像/文本文件

java - 需要帮助来反编译 jar 文件、编辑类并保存为 jar 文件

java - 将源文件包含在可运行的 jar 文件中

java - 如何使用 ModelAndView 进行标题重定向

Java:如何理解神秘的堆栈跟踪?

java - RelationshipEntity 未保留

java - 根据消息类型选择消息实现

java - Final jar 依赖什么?