Target-san
Target-san

Reputation: 461

Generate Ant tasks with macro

I've been searching for a possibility to generate ANT targets from top-level macro.

Details:

We have heterogenic build system. ANT+IVY is used as top-level (inherited solutin, can't be changed). Some projects are built via MSBuild, called from ANT via exec task. For each of these projects, there's at least two distinct calls to msbuild (wrapped with macro for brevity), one in "build" target, and one in "clean". Two of them are different only by "target" parameter. So I was guessing, if there's possibility for something like this:

Extension nodes:

<extensionpoint name="build-ext-point" />
<extensionpoint name="clean-ext-point" />
<target name="build" depends="build-ext-point" />
<target name="clean" depends="clean-ext-point" />

My magic macro:

<macrodef name="msbuild-proj" />
    <attribute name="project" />
    <sequential>
        <target name="@{project}-build" >
            <msbuild project="@{project}" target="Build" />
        </target>
        <target name="@{project}-clean" >
            <msbuild project="@{project}" target="Clean" />
        </target>
    </sequential>
</macrodef>

How it would be used:

<msbuild-proj project="CPP-proj" />

Thanks!

P.S: Yeah I know that I can define those build and clean overridden, or via ext point, or whatever. The question is actually whether I can remove some code duplication.

UPD: I'd answer this by myself. At the point, there's no such possibility. Mainly, because Target class is a task container, but not a task. So, it cannot be placed into container. So I guess I'll write some kind of extensible task.

Upvotes: 0

Views: 284

Answers (2)

Target-san
Target-san

Reputation: 461

Actually done this. Does its job, although some caveats are present. For those interested: https://bitbucket.org/targetsan/ant-events

Upvotes: 0

Mark O&#39;Connor
Mark O&#39;Connor

Reputation: 77941

ANT has a couple of mechanisms for building modular builds.

First of all I think your main question was on how to build "extension points" to your build? The following ANT tasks are designed to import common build logic from another build file:

Since you're already planning to extend your build using macrodefs, I'd recommend packaging these as a reusable ANTlib. The ANTlib can live within your project, but it's really designed to be packaged within a jarfile which another build can pickup, for example by installing it in the standard ANT lib directory:

  • $ANT_HOME/lib
  • $HOME/.ant/lib

Finally, if you're already using ivy and you package your taskdefs as ANT libs, you could version your build logic by installing it in a Maven repository manager like Nexus. This addresses one of the key problems with large ANT builds. Over time they become so big it's impossible to change the common logic without impacting older builds (Demonstrating that the builds are not properly isolated from each other).

Upvotes: 1

Related Questions