Java 用 ant 创建一个bundle jar
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1821803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating a bundle jar with ant
提问by abyx
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.
我正在使用 Ant 构建一些 Java 项目。
在某些情况下,我有一个lib/
目录,其中包含 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,其中包含项目的代码以及依赖项,方法是将目录中的zipfileset
每个jar 添加到捆绑 jar 文件中lib/
。
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
).
问题是,每次添加 jar 或更改名称时,我都需要记住更新build.xml
文件,因为我找不到以zipfilesets
自动方式添加这些文件的方法,该方法将包含特定模式中的所有 jar(例如lib/*.jar
)。
Is there a better way for doing this?
有没有更好的方法来做到这一点?
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.
我已经考虑为此编写自己的 Ant 任务,或者使用 Groovy 的 ant API 以编程方式执行此操作,但想知道是否有使用“vanilla”ant 执行此操作的方法。
采纳答案by jonescb
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 is used from the package target posted above. This part I copied from somewhere else, so I'm not all that familiar with it.
mf.classpath 用于上面发布的包目标。这部分是从别处抄来的,所以不是很熟悉。
Quick edit. Javac needs to know about those jars too.
快速编辑。Javac 也需要了解这些 jar。
<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>
回答by Upgradingdave
Here's an simple example of an ant target that will create a jar (named test.jar) that includes all jar files under the lib directory. Maybe this will solve your problem?
这是一个简单的 ant 目标示例,它将创建一个 jar(名为 test.jar),其中包含 lib 目录下的所有 jar 文件。也许这会解决你的问题?
<target name="jar">
<jar destfile="test.jar">
<fileset dir="lib/" includes="**/*.jar" />
</jar>
</target>
回答by Jason Day
回答by Jason Day
For those of you using NetBeans here is how you can create JAR archive with libraries bundled using zipgroupfileset:
对于那些使用 NetBeans 的人,这里是如何使用使用 zipgroupfileset 捆绑的库创建 JAR 存档的方法:
<target name="-post-jar">
<property name="store.jar.name" value="MyJarName"/>
<property name="store.dir" value="dist"/>
<property name="store.jar" value="${store.dir}/${store.jar.name}.jar"/>
<echo message="Packaging ${application.title} into a single JAR at ${store.jar}"/>
<jar destfile="${store.dir}/temp_final.jar" filesetmanifest="skip">
<zipgroupfileset dir="dist" includes="*.jar"/>
<zipgroupfileset dir="dist/lib" includes="*.jar"/>
<manifest>
<attribute name="Main-Class" value="${main.class}"/>
</manifest>
</jar>
<zip destfile="${store.jar}">
<zipfileset src="${store.dir}/temp_final.jar"
excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
</zip>
<delete file="${store.dir}/temp_final.jar"/>
<delete dir="${store.dir}/lib"/>
<delete file="${store.dir}/README.TXT"/>
</target>
I've added this target definition to the end of build.xml file. Target name is -post-jar.
我已将此目标定义添加到 build.xml 文件的末尾。目标名称是-post-jar。