Reusing the list of zipfileset statements in ANT

advertisements

I have a build.xml where I put together some fat runnable jars with targets similar to:

<target name="create_myapp_jar">
  <jar destfile="./production/myapp.jar" filesetmanifest="mergewithoutmain">
    <manifest>
      <attribute name="Main-Class" value="com.something.entrypoints.MyApp"/>
      <attribute name="Class-Path" value="."/>
    </manifest>
    <fileset dir="./classes"/>
    <zipfileset excludes="META-INF/*.SF" src="./lib/commons-cli-1.2.jar"/>
    <zipfileset excludes="META-INF/*.SF" src="./lib/commons-codec-1.10-sources.jar"/>
    <zipfileset excludes="META-INF/*.SF" src="./lib/commons-codec-1.10.jar"/>
    <zipfileset excludes="META-INF/*.SF" src="./lib/commons-io-2.4.jar"/>
    <zipfileset excludes="META-INF/*.SF" src="./lib/commons-lang-2.6.jar"/>
    <zipfileset excludes="META-INF/*.SF" src="./lib/commons-pool2-2.2.jar"/>

    lots and lots of more JARs here....
</target>

The code was generated by Eclipse (Export runnable Jar) and with minor changes by me.

The list of Jars that are included is quite long actually. I build several of these fat-jars, and I was wondering how I could avoid listing the <zipfileset> statements for each target?

How can I smack them all together and then reference them from each fat-jar build target? All in the purpose of not letting my build.xml swell to hundreds and hundreds of lines. Also the pain of adding a new JAR to each of the targets would go away if I could have one definition somewhere.


as far as I understand, your zipfileset entries list is always the same for each built jars ? Maybe you should consider making a macro that contains the zipfileset entries and use variables to adjust the macro for the context of each target:

<macrodef name="make-jar">
    <attribute name="destfile"/>
    <attribute name="mainclass"/>
    <attribute name="classpath"/>
    <attribute name="obj.dir"/>
    <sequential>
        <jar destfile="@{destfile}" filesetmanifest="mergewithoutmain">
            <manifest>
                <attribute name="Main-Class" value="@{mainclass}"/>
                <attribute name="Class-Path" value="@{classpath}"/>
            </manifest>
            <fileset dir="@{obj.dir}"/>
            <zipfileset excludes="META-INF/*.SF" src="./lib/commons-cli-1.2.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="./lib/commons-codec-1.10-sources.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="./lib/commons-codec-1.10.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="./lib/commons-io-2.4.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="./lib/commons-lang-2.6.jar"/>
            <zipfileset excludes="META-INF/*.SF" src="./lib/commons-pool2-2.2.jar"/>

            lots and lots of more JARs here....
        </jar>
    </sequential>
</macrodef>

<target name="create_myapp_jar">
    <make-jar destfile="./production/myapp.jar" mainclass="com.something.entrypoints.MyApp" classpath="." obj.dir="./classes"/>
</target>