How do I pass an argument to an Ant task?

ant
Jonathan Haddad picture Jonathan Haddad · Dec 16, 2009 · Viewed 61.1k times · Source

I'm not very good with Ant, but we're using it as a build tool. Right now, we can run "ant test" and it'll run through all the unit tests.

However, I'd love to be able to do something like ant test some_module and have it accept some_module as a parameter, and only test that.

I haven't been able to find how to pass command line args to Ant - any ideas?

Answer

martin clayton picture martin clayton · Dec 16, 2009

One solution might be as follows. (I have a project that does this.)

Have a separate target similar to test with a fileset that restricts the test to one class only. Then pass the name of that class using -D at the ant command line:

ant -Dtest.module=MyClassUnderTest single_test

In the build.xml (highly reduced):

<target name="single_test" depends="compile" description="Run one unit test">
    <junit>
        <batchtest>
            <fileset dir="${test.dir}" includes="**/${test.module}.class" />
        </batchtest>
    </junit>
</target>