Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to generalize an Apache ANT target?

Tags:

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.

like image 829
Outlaw Programmer Avatar asked Sep 08 '08 15:09

Outlaw Programmer


People also ask

How do I run a specific target in Ant?

To run the ant build file, open up command prompt and navigate to the folder, where the build. xml resides, and then type ant info. You could also type ant instead. Both will work,because info is the default target in the build file.

What is the default target in Ant?

the default target to use when no target is supplied. No; however, since Ant 1.6. 0, every project includes an implicit target that contains any and all top-level tasks and/or types. This target will always be executed as part of the project's initialization, even when Ant is run with the -projecthelp option.

What does Apache Ant stand for?

Techopedia Explains Apache Ant Ant is an acronym for "another neat tool." Sometimes the software is described as being similar to the insect that shares its name; although ants are very small, they can build large and well. Ant is commonly used on Java-based projects.

How does Apache ant work?

Ant builds are based on three blocks: tasks, targets and extension points. A task is a unit of work which should be performed and constitutes of small atomic steps, for example compile source code or create Javadoc. Tasks can be grouped into targets. A target can be directly invoked via Ant.


2 Answers

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"

like image 152
Vladimir Avatar answered Oct 18 '22 16:10

Vladimir


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.

like image 26
Chris Dail Avatar answered Oct 18 '22 18:10

Chris Dail