My current build file has the following repetitive tasks:
<jar jarfile="${build.lib}/${prefix}-foo.jar">
<fileset dir="${build.classes}">
<include name="com/a/c/foo/**"/>
</fileset>
</jar>
<jar jarfile="${build.lib}/${prefix}-bar.jar">
<fileset dir="${build.classes}">
<include name="com/a/c/bar/**"/>
</fileset>
</jar>
... etc. The issue is that the build.xml must be modified for each new package or for each new sub-project. This is a frequent occurrence where I work.
I would like to replace this with logic that will dynamically generate the JARs and their file names based off of a "root" package. So, for example, I could set the root package to be com/a/c, and all packages directly under that package would get their own JAR. Note that all packages under "foo" or "bar" would just be part of "foo.jar" or "bar.jar".
I looked up for loop logic tasks for ANT. I found one in each ant-contrib and JWare/AntXtras, but I could not get either to work as desired.
I don't know about looping over and finding all package names, but you could use a macro to avoid code duplication.
I haven't tried this, but it could work
<macrodef name="build_jar">
<attribute name="name"/>
<sequential>
<jar jarfile="${build.lib}/${prefix}-@{name}.jar">
<fileset dir="${build.classes}">
<include name="com/a/c/@{name}/**"/>
</fileset>
</jar>
</sequential
</macrodef>
<target name="build_foo">
<build_jar name="foo"/>
</target>
<target name="build_bar">
<build_jar name="bar"/>
</target>
How about this:
<project name="dynjar" default="jar" basedir=".">
<property name="build.classes" value="${basedir}/classes"/>
<property name="build.lib" value="${basedir}/lib"/>
<property name="prefix" value="prefix"/>
<property name="root" value="com/a/c"/>
<target name="jar">
<!-- ${ant.file} is the name of the current build file -->
<subant genericantfile="${ant.file}" target="do-jar">
<!-- Pass the needed properties to the subant call. You could also use
the inheritall attribute on the subant element above to pass all
properties. -->
<propertyset>
<propertyref name="build.classes"/>
<propertyref name="build.lib"/>
<propertyref name="prefix"/>
<propertyref name="root"/>
</propertyset>
<!-- subant will call the "do-jar" target for every directory in the
${build.classes}/${root} directory, making the subdirectory the
basedir. -->
<dirset dir="${build.classes}/${root}" includes="*"/>
</subant>
</target>
<target name="do-jar">
<!-- Get the basename of the basedir (foo, bar, etc.) -->
<basename file="${basedir}" property="suffix"/>
<jar jarfile="${build.lib}/${prefix}-${suffix}.jar">
<fileset dir="${build.classes}">
<include name="${root}/${suffix}/**"/>
</fileset>
</jar>
</target>
</project>