javaantmacrodef

Ant macrodef compilation task


I have a compiler (and language) I am building that is normally invoked thus:

java -jar nc.jar \
    -p some/referenced/package.nc \ 
    -p framework.nc \ 
    source1.ns source2.ns sourceN.ns \ 
    -o output/package.nc

I'd like to include a task in my ANT build file that invokes the compiler to compile the standard library and all test cases, but specifying each separate compiler invocation as a <java> task is painful:

<target name="framework" depends="compiler" description="Build the n framework">
    <!-- More compile steps -->
    <java jar="nc.jar" fork="true">
        <arg value="-p"/>
        <arg path="../nframework/build/n.core.nc"/>
        <arg path="../nframework/n/debug/DebugPrint.ns"/>
        <arg path="../nframework/n/debug/Trace.ns"/>
        <arg value="-o"/>
        <arg path="../nframework/build/n.debug.nc"/>
    </java>
    <!-- More compile steps -->
</target>

I would like to create an ANT task of some sort that can simplify this into something like:

<target name="framework" depends="compiler" description="Build the n framework">
    <!-- More compile steps -->
    <nc output="../nframework/build/n.debug.nc">
        <link-package path="../nframework/build/n.core.nc"/>
        <src>
            <fileset dir="../nframework/n/debug" includes="**/*.ns"/>
        </src>
    </nc>
    <!-- More compile steps -->
</target>

To this end, I tried macrodef:

<macrodef name="nc">
    <attribute name="output"/>
    <element name="link-package"/>
    <element name="src"/>
    <sequential>
        <java jar="nc.jar" fork="true">
            <arg value="-p"/>
            <!-- This doesn't do what I want -->
            <link-package/>
            <!-- Neither does this -->
            <src/>
            <arg value="-o"/>
            <arg path="@{output}"/>
        </java>
    </sequential>
</macrodef>

I've tried several variations on the above, but each errors out with something like: /home/jwarner/code/nlang/nc/build.xml:55: java doesn't support nested "fileset" element.

Is there a way to do this without extending ANT itself? Alternatively, would it be fairly easy to add an ant task to my compiler? I'm not terribly picky about the syntax of the final <nc> task.


Solution

  • I have had a similar problem in the past where the out-of-the-box Ant tasks didn't quite do what I wanted them to do. I found that it was very easy to write my own Ant task.

    The documentation is concise but does a good job of explaining what you need to do.

    http://ant.apache.org/manual/develop.html#writingowntask