Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'make -n' equivalent for ant

Tags:

makefile

ant

According to the man page of make, -n option does the following job:

Print the commands that would be executed, but do not execute them.

I am looking for an option which acts the same in Apache Ant.

like image 518
Javad Avatar asked Feb 03 '12 07:02

Javad


1 Answers

Horrific, but here it is. We can hack the targets at runtime using some code inside a <script> tag*. The code in do-dry-run below sets an unless attribute on each of your targets, and then sets that property so that none of them executes. Ant still prints out the names of targets that are not executed because of an unless attribute.

*(JavaScript script tags seem to be supported in Ant 1.8+ using the Oracle, OpenJDK and IBM versions of Java.)

<?xml version="1.0" encoding="UTF-8"?>
<project default="build">

    <target name="targetA"/>
    <target name="targetB" depends="targetA">
        <echo message="DON'T RUN ME"/>
    </target>
    <target name="targetC" depends="targetB"/>

    <target name="build" depends="targetB"/>

    <target name="dry-run">
        <do-dry-run target="build"/>
    </target>

    <macrodef name="do-dry-run">
        <attribute name="target"/>
        <sequential>
            <script language="javascript"><![CDATA[

                var targs = project.getTargets().elements();
                while( targs.hasMoreElements() ) {
                    var targ = targs.nextElement();
                    targ.setUnless( "DRY.RUN" );
                }
                project.setProperty( "DRY.RUN", "1" );
                project.executeTarget( "@{target}" );

            ]]></script>
        </sequential>
    </macrodef>

</project>

When I run this normally, the echo happens:

$ ant
Buildfile: build.xml

targetA:

targetB:
     [echo] DON'T RUN ME

build:

BUILD SUCCESSFUL
Total time: 0 seconds

But when I run dry-run, it doesn't:

$ ant dry-run
Buildfile: build.xml

dry-run:

targetA:

targetB:

build:

BUILD SUCCESSFUL
Total time: 0 seconds
like image 78
Andy Balaam Avatar answered Oct 21 '22 19:10

Andy Balaam