tags:

views:

739

answers:

3

I would like to unjar multiple JAR files and then rebuild into one JAR using an ant build script. Is this possible?

+1  A: 

Yes, it's possible with ant. A jar file is basically a zip with a special manifest file. So to unjar, we need to unzip the jars. Ant includes an unzip task.

To unzip/unjar all the jar files in your project:

<target name="unjar_dependencies" depends="clean">
    <unzip dest="${build.dir}">
        <fileset dir="${lib.dir}">
            <include name="**/*.jar" />
        </fileset>    
    </unzip>
</target>

Obviously you need to declare ${build.dir} and ${lib.dir} first. The line <include name="**/*.jar" /> tells ant to include all files that end up with the jar extension, you can tweak that include to suit your needs.

To pack everything into a jar, you use the jar task:

<target name="make_jar" depends="compile, unjar_dependencies">
    <jar basedir="${build.dir}"
         destfile="${dist.dir}/${project_name}.jar">
        <manifest>
            <attribute name="Main-Class" value="${mainclass}" />
        </manifest>
        <fileset dir="${build.dir}">
            <include name="**/*.class" />
        </fileset>
        <fileset dir="${src.dir}">
            <include name="applicationContext.xml" />
            <include name="log4j.properties" />
        </fileset>
    </jar>
</target>

In this example, we include different filesets. In one fileset we are including all compiled classes. In another fileset we include two config files that this particular project depends upon.

Cesar
+2  A: 

Yes it is !

You have two possibilities :

  • Espen answer :

One possible solution that creates one jar file from all the jar files in a given directory:

<target name="dependencies.jar">
    <jar destfile="WebContent/dependencies.jar">
        <zipgroupfileset dir="lib/default/" includes="*.jar" 
              excludes="*.properties" />
    </jar>
</target>

This is useful if you don't need to exclude content that are in some jars (like for example some properties configuration file that might override yours, etc). Here the excludes properties is filtering out files from the dir property.

  • Use zipfileset

The other solution is to use the zipfileset tag where the excludes property this time will filter out content from the jar to be merged.

<jar destfile="your_final_jar.jar" filesetmanifest="mergewithoutmain">
    <manifest>
        <attribute name="Main-Class" value="main.class"/>
        <attribute name="Class-Path" value="."/>
    </manifest>
    <zipfileset 
       excludes="META-INF/*.SF"
       src="/path/to/first/jar/to/include.jar"/>
</jar>
  • Of course you can combine the two tags (zipfileset and zipgroupfileset) inside the same jar tag to get the best of the two.
Thierry
+2  A: 

Yes, it's possible.

One possible solution that creates one jar file from all the jar files in a given directory:

<target name="dependencies.jar">
    <jar destfile="WebContent/dependencies.jar">
        <zipgroupfileset dir="lib/default/" includes="*.jar" 
              excludes="*.properties" />
    </jar>
</target>
Espen