tags:

views:

767

answers:

1

I have a bunch of ant projects that build plug-ins that all conform to the same API. The plug-ins incorporate the third-party libraries that they depend on so that they are self-contained. As much of the behaviour in the individual build scripts was similar, I decided to pull out the common parts to a common build script.

The original build scripts looked something like this:

ProjectA/build.xml:

<project name="ProjectA" basedir=".">
...
    <target name="jar">
        <jar destfile="${project.target}" manifest="manifest.mf">
            <fileset dir="${project.build.bin.dir}" />
            <zipfileset src="externlib1.jar" />
            <zipfileset src="externlib2.jar" />
        </jar>
    </target>
...
</project>

ProjectB/build.xml:

<project name="ProjectB" basedir=".">
...
    <target name="jar">
        <jar destfile="${project.target}" manifest="manifest.mf">
            <fileset dir="${project.build.bin.dir}" />
            <zipfileset src="externlib2.jar" />
            <zipfileset src="externlib3.jar" />
        </jar>
    </target>
...
</project>

Here is what my build scripts look like after refactoring:

Common.xml:

<project name="Common" basedir=".">
...
    <target name="jar">
        <jar destfile="${project.target}" manifest="common-manifest.mf">
            <fileset dir="${project.build.bin.dir}" />
            <zipfileset refid="extern.libs" />
        </jar>
    </target>
...
</project>

ProjectA/build.xml:

<project name="ProjectA" basedir=".">
...
    <zipfileset id="extern.libs">
        <file file="externlib1.jar" />
        <file file="externlib2.jar" />
    </zipfileset>
...
    <import file="../common.xml" />
</project>

ProjectB/build.xml:

<project name="ProjectB" basedir=".">
...
    <zipfileset id="extern.libs">
        <file file="externlib2.jar" />
        <file file="externlib3.jar" />
    </zipfileset>
...
    <import file="../common.xml" />
</project>

However, the refactored build does not work--I believe that the problem is that I cannot declare a zipfileset that has multiple files.

I cannot figure out a way that I can declare a fileset such that the behaviour with the common jar task is the same as the behaviour where the jar tasks are declared in each project's build script. Has anyone solved this problem before? Is there a different way I can accomplish the same thing?

+1  A: 

It's a bit fiddly, but the zipgroupfileset of the jar task may help.

Something like this for ProjectA should work. I've guessed the directory, adjust as appropriate.

<fileset dir="${project.build.lib.dir}" id="extern.libs">
    <include name="externlib1.jar" />
    <include name="externlib2.jar" />
</fileset>

Then in the common.xml file, I've renamed your common-manifest.mf.

<target name="jar">
    <jar destfile="${project.target}" duplicate="preserve" manifest="common-manifest.mf">
        <zipgroupfileset refid="extern.libs"/>
    </jar>
</target>
martin clayton
Thanks for the suggestion, but I don't think it works--none of the jars that are included in the zipfileset are expanded into the final project jar. If I use <file file=... only the last jar that is listed (externlib2 in this case) is expanded into the project jar while externlib1 is ignored.
iboisver
@iboisver - I've revised it. Missed something fundamental there - apologies. I've run a test on this one and it looks like it works for me.
martin clayton