Creating a banner with ant

I use Ant to create some Java projects.
In some cases, I have a lib/ directory that contains external dependencies, in the form in JAR files.

During the build, I create a merged jar that contains the project code, along with the dependencies, adding a zipfileset to the jar package file for each of the jars in the lib/ directory.

The problem is that every time I add a jar or change the names, I need to remember to update the build.xml file, since I could not find a way to add these zipfilesets automatically, it will include all banks in a specific template (for example, lib/*.jar ).

Is there a better way to do this?

I thought about writing my own Ant task for this or using the Groovy Ant API for this programmatically, but I wondered if there was a way to do this with the "vanilla" ant.

+58
java jar ant
Nov 30 '09 at 19:16
source share
4 answers

For my purpose, 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 this is how I create 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 specified above. This part that I copied from another place, so I'm not so familiar with it.

Quick edit. Javak should also know about these jars.

 <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> 
+51
Nov 30 '09 at 19:34
source share

Use zipgroupfileset . For example:

 <target name="jar"> <jar destfile="foo.jar" basedir="${dir.classes}"> <zipgroupfileset dir="lib" includes="*.jar"/> </jar> </target> 

zipgroupfileset documented using the zip task.

+29
Dec 06 '09 at 17:23
source share

For those using NetBeans, you can create a JAR archive with libraries related to zipgroupfileset:

 <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 have added this target definition to the end of the build.xml file. The target name is post-jar.

+7
Mar 29 2018-12-12T00:
source share

Here is a simple example of an ant target that will create a jar (named test.jar) that includes all jar files in the lib directory. Perhaps this will solve your problem?

 <target name="jar"> <jar destfile="test.jar"> <fileset dir="lib/" includes="**/*.jar" /> </jar> </target> 
+1
Nov 30 '09 at 19:27
source share



All Articles