Ever had a situation where you are using Ant to build a JAR file and this JAR uses classes from other JAR OR needs few other JAR files to be in class path. Placing dependent JARs in class path manually is one way of doing it, but what if you want to generate/deliver/use only one JAR [may be executable] which includes all the dependent JARs in it. ‘zipfileset’ attribute of the ‘jar’ ANT task is the one to use.
Say, you are trying to generate a JAR file with name ‘my-application.jar’, which needs/depends on another JAR with the name ‘some-dependency.jar’. Assuming all the compiled classes are available under ‘_bin’ directory, the dependent JAR is available under ‘lib’ directory and you want to place the generated JAR under ‘_dist’ directory, ANT script might look as given below
What if you have more than one JAR to include, say all the JAR files under ‘lib’ directory, one way is to add multiple ‘zipfileset’ tag for each of those JAR files, if you’ve a big list of JARs under your ‘lib’ directory then, you can use the ANT script given below
Say, you are trying to generate a JAR file with name ‘my-application.jar’, which needs/depends on another JAR with the name ‘some-dependency.jar’. Assuming all the compiled classes are available under ‘_bin’ directory, the dependent JAR is available under ‘lib’ directory and you want to place the generated JAR under ‘_dist’ directory, ANT script might look as given below
<jar destfile="_dist/my-application.jar"> <fileset dir="_bin" /> <zipfileset src="lib/some-dependency.jar" includes="**/*.class" excludes="META-INF/**/*" /> </jar>That is all you need, only extra line you’ve is ‘zipfileset’ attribute. This attribute takes the JAR file [some-dependency.jar] to be added to the target JAR. You can mention what files from this JAR need to included OR excluded using the ‘includes’ and ‘excludes’ attributes. The above code says to include all ‘.class’ files and exclude all files under ‘META-INF’ directory.
What if you have more than one JAR to include, say all the JAR files under ‘lib’ directory, one way is to add multiple ‘zipfileset’ tag for each of those JAR files, if you’ve a big list of JARs under your ‘lib’ directory then, you can use the ANT script given below
<unjar dest="_unjar" overwrite="false"> <fileset dir="lib" includes="**/*.jar"/> </unjar>What we did here is, we un-jared all the JAR files under ‘lib’ directory to a temporary directory ‘_unjar’, and include all the ‘.class’ files using the ‘fileset’ tag, simple isn’t it!
<jar destfile="_dist/my-application.jar"> <fileset dir="_bin" /> <fileset dir="_unjar" includes="**/*.class"/> </jar>
Comments
Post a Comment