Is there a way to generalize an Apache ANT target?

13,650

Solution 1

I would suggest to work with macros over subant/antcall because the main advantage I found with macros is that you're in complete control over the properties that are passed to the macro (especially if you want to add new properties).

You simply refactor your Ant script starting with your target:

<target name="vss.check">
    <vssadd localpath="D:\build\build.00012.zip" 
        comment="Added by automatic build"/>
</target>

creating a macro (notice the copy/paste and replacement with the @{file}):

<macrodef name="private-vssadd">
    <attribute name="file"/>
    <sequential>
        <vssadd localpath="@{file}" 
            comment="Added by automatic build"/>
    </sequential>
</macrodef>

and invoke the macros with your files:

<target name="vss.check">
    <private-vssadd file="D:\build\File1.zip"/>
    <private-vssadd file="D:\build\File2.zip"/>
</target>

Refactoring, "the Ant way"

Solution 2

It is generally considered a bad idea to version control your binaries and I do not recommend doing so. But if you absolutely have to, you can use antcall combined with param to pass parameters and call a target.

<antcall target="reusable">
    <param name="some.variable" value="var1"/>
</antcall>

<target name="reusable">
    <!-- Do something with ${some.variable} -->
</target>

You can find more information about the antcall task here.

Solution 3

Take a look at Ant macros. They allow you to define reusable "routines" for Ant builds. You can find an example here (item 15).

Solution 4

Also check out the subant task, which lets you call the same target on multiple build files:

<project name="subant" default="subant1">
    <property name="build.dir" value="subant.build"/>
    <target name="subant1">
        <subant target="">
            <property name="build.dir" value="subant1.build"/>
            <property name="not.overloaded" value="not.overloaded"/>
            <fileset dir="." includes="*/build.xml"/>
        </subant>
    </target>
</project>

Solution 5

You can use Gant to script your build with groovy to do what you want or have a look at the groovy ant task.

Share:
13,650
Tim Frey
Author by

Tim Frey

Gametime.

Updated on June 03, 2022

Comments

  • Tim Frey
    Tim Frey almost 2 years

    We have an Apache ANT script to build our application, then check in the resulting JAR file into version control (VSS in this case). However, now we have a change that requires us to build 2 JAR files for this project, then check both into VSS.

    The current target that checks the original JAR file into VSS discovers the name of the JAR file through some property. Is there an easy way to "generalize" this target so that I can reuse it to check in a JAR file with any name? In a normal language this would obviously call for a function parameter but, to my knowledge, there really isn't an equivalent concept in ANT.

  • Dominic Mitchell
    Dominic Mitchell over 13 years
    It's probably a better idea to use macros rather than antcall (see the other answers). With antcall you break ant's dependency mechanism and are apt to end up with some rather confusing build logs.
  • Vladislav Rastrusny
    Vladislav Rastrusny over 13 years
    It seems, you have forgotten to close <attribute> tag. It should be like <attribute name="file" />