【问题标题】:How can I use Ant to build a single executable .jar that has dependency .jars in it in a /lib dir in the .jar?如何使用 Ant 在 .jar 的 /lib 目录中构建具有依赖项 .jar 的单个可执行 .jar?
【发布时间】:2016-12-13 15:34:06
【问题描述】:

如何使用 Ant 在 .jar/lib 目录中构建单个可执行文件 .jar,其中包含依赖项 .jars?

我在项目根文件中有一个/lib 目录,其中包含所有二进制依赖项.jar

【问题讨论】:

  • 为什么不用 Maven 或者 gradle 来代替 ant?
  • 好的,当然,这将如何使用 Maven 完成?谢谢

标签: java intellij-idea ant executable-jar


【解决方案1】:

在 java 中,可执行 jar 具有特殊格式的 manifest,其中包括以下属性:

  1. 主类
  2. 类路径

在 ANT 中,这很容易通过 jar 任务完成,如下所示:

<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}" includes="*.class">
    <manifest>
        <attribute name="Main-Class" value="${main-class}" />
        <attribute name="Class-Path" value="${jar-classpath}" />
    </manifest>
</jar>

对于额外的功劳,可以使用非常有用的manifestclasspath 任务来帮助组装正确的类路径字符串。

<manifestclasspath property="jar-classpath" jarfile="${jar.dir}/${ant.project.name}.jar">
    <classpath>
        <fileset dir="/path/to/lib/dir" includes="*.jar"/>
    </classpath>
</manifestclasspath>

【讨论】:

    猜你喜欢
    • 2015-04-21
    • 2012-01-06
    • 1970-01-01
    • 2014-06-11
    • 2013-11-11
    • 1970-01-01
    • 2015-02-01
    • 2021-05-25
    • 2021-09-04
    相关资源
    最近更新 更多