Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you use ant to unjar multiple JAR files and rebuild them into one JAR file?

Tags:

jar

ant

unjar

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

like image 875
shelt536 Avatar asked May 12 '10 22:05

shelt536


People also ask

How do you Unjar a JAR file?

Type in jar xf followed by a space followed by the name of the JAR file. This is the command to extract a JAR file.

How do I compile an Ant project?

In the Project tool window, right-click the generated build file and select Add as Ant Build File to open it in the Ant tool window. In the Select Path dialog, navigate to the desired build. xml file, and click OK. A build file should have at least a root element to be added to the Ant Build tool window.

How do I Unjar a JAR file in Windows?

Right-click on the JAR file and select Extract All.


2 Answers

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.

like image 94
Cesar Avatar answered Oct 15 '22 02:10

Cesar


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.
like image 40
3 revs Avatar answered Oct 15 '22 03:10

3 revs