How do I pass an argument to an Ant task?

Solution 1:

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>

Solution 2:

You can also define a property with an optional default value that can be replaced via command line, e.g.

<target name="test">
  <property name="moduleName" value="default-module" />
  <echo message="Testing Module: ${moduleName}"/>
  ....
</target>

and run it as:

ant test -DmoduleName=ModuleX

Solution 3:

What about using some conditional in your test target and the specifying -Dcondition=true?

<target name="test" depends="_test, _test_if_true>
   ...
</target> 

<target name="_test_if_true" if="condition">
   ...
</target>

<target name="_test" unless="condition">
   ...
</target>

Adapted a bit from the ant faq.

Solution 4:

You can define a property on commandline when invoking ant:

ant -Dtest.module=mymodulename

Then you can use it as any other ant property:

...
    <fileset dir="${test.dir}" includes="**/${test.module}.class" />
...

Have a look at Ant's manual.