用 ant 创建一个bundle jar [英] Creating a bundle jar with ant
问题描述
我正在使用 Ant 构建一些 Java 项目.
在某些情况下,我有一个 lib/
目录,其中包含 JAR 文件形式的外部依赖项.
I'm using Ant to build some Java projects.
In some, I've got a lib/
directory, which contains external dependencies, in the form on JAR files.
在构建过程中,我通过为 lib 中的每个 jar 添加一个
目录.zipfileset
到捆绑 jar 文件中,创建了一个捆绑的 jar,其中包含项目的代码和依赖项//
During the build, I create a bundled jar, that contains the project's code, alongside the dependencies, by adding to the bundle jar file a zipfileset
for each of the jars in the lib/
directory.
问题是,每次添加 jar 或更改名称时,我都需要记住更新 build.xml
文件,因为我找不到添加那些 zipfilesets
以自动方式包含所有 jars 的特定模式(例如 lib/*.jar
).
The problem is, that every time I add a jar, or change names, I need to remember to update the build.xml
file, as I couldn't find a way for adding those zipfilesets
in an automatic manner that will include all jars in a certain pattern (e.g. lib/*.jar
).
有没有更好的方法来做到这一点?
Is there a better way for doing this?
我已经考虑为此编写自己的 Ant 任务,或者使用 Groovy 的 ant API 以编程方式执行此操作,但想知道是否有使用vanilla"ant 执行此操作的方法.
I've considered writing my own Ant Task for this, or using Groovy's ant API to do this programmatically, but was wondering if there's a way for doing this using "vanilla" ant.
推荐答案
在我的目标中,我有这样的事情:
In my target, I have something like this:
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar" excludes=""/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
<attribute name="Class-Path" value="${mf.classpath}"/>
</manifest>
</jar>
这是我构建类路径的方法:
And here is how I build my classpath:
<path id="build.classpath">
<fileset dir="${basedir}/">
<include name="${lib.dir}/*.jar"/>
</fileset>
</path>
<pathconvert property="mf.classpath" pathsep=" ">
<path refid="build.classpath"/>
<mapper>
<chainedmapper>
<flattenmapper/>
<globmapper from="*.jar" to="lib/*.jar"/>
</chainedmapper>
</mapper>
</pathconvert>
mf.classpath 用于上面发布的包目标.这部分是从别处抄来的,不是很熟悉.
mf.classpath is used from the package target posted above. This part I copied from somewhere else, so I'm not all that familiar with it.
快速编辑.Javac 也需要了解这些 jar.
Quick edit. Javac needs to know about those jars too.
<path id="jars">
<fileset dir="${lib.dir}" includes="**/*.jar"/>
</path>
<target name="compile">
<mkdir dir="${build.dir}"/>
<javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="jars" debug="on"/>
</target>
这篇关于用 ant 创建一个bundle jar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!