How to pass an argument to an Ant task?

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

However, I would like to do something like ant test some_module and take it some_module as a parameter and only check it.

I was unable to find how to pass command line arguments to Ant - any ideas?

+41
ant
Dec 16 '09 at 0:41
source share
8 answers

One solution might be the following. (I have a project that does this.)

You have a separate goal, similar to test with fileset , which restricts validation to only one class. Then pass the name of this class with -D at the ant command line:

 ant -Dtest.module=MyClassUnderTest single_test 

In the build.xml file (greatly 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> 
+36
Dec 16 '09 at 0:49
source share

How about using some conditional conditions in a test target and 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 ant faq .

+6
Dec 16 '09 at 0:51
source share

You can define a property on the command line by calling ant:

 ant -Dtest.module=mymodulename 

Then you can use it like any other ant property:

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

See Ant Guide .

+4
Nov 20 '12 at 16:27
source share

You can also define a property with an optional default value, which can be replaced using the command line, for example

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

and run it like:

 ant test -DmoduleName=ModuleX 
+3
Oct 11 '16 at 9:21
source share

I tried the solutions posted here for the same original question. Yes, just use ant -D<arg_name> . I think -D is the key word. I am not an ant expert and have not read the manuals in detail. Then, inside XML ant files you can access, for example: ${arg_name}

For example, you might have an argument name like: arg.myarg , so in XML ${arg.myarg} .

+1
May 29 '13 at 9:50 pm
source share

Ant really has no options for the build file. I can come up with several ways to do this:

Use a special purpose to set the tests. You can use the <for/> task from AntContrib so you can specify multiple tests. You will need to download the Ant-Contrib jar file. I recommend placing it inside your project in the $ {basedir} / antlib / antcontrib directory. That way, when others check your project, they get the necessary Ant -Contrib jar file.

 <property name="antlib.dir" value="${basedir}/antlib"/> <property name="antcontrib.dir" value="${antlib}/antcontrib"/> <!-- Set up the ant contrib tasks for your use --> <taskdef resource="net/sf/antcontrib/antlib.xml"> <classpath> <fileset dir="${antcontrib.dir}"/> </classpath> </taskdef> <target name="select-test" description="Select the tests to run" depends="test-compile" if="junit-tests"> <for parameter="module" list="${junit-tests}" delimiter=" "> <sequential> <junit fork="true" ...> <batchtest todir="$target/unit-tests"> <fileset dir="${test.destdir}"> <include name="**/@{module}.class"/> </fileset> </junit> </sequential> </for> </target> 

Now you run several tests as follows:

 $ ant -D"test-one test-two test-three" select-test 
0
May 29 '13 at 23:27
source share

For arguments, there is a property called Facility. You need to set the property. As in ANT, simple arguments are accepted as the target name.

0
Nov 26 '13 at 15:36
source share

Suppose you have two modules in your project ModuleX and ModuleY, where ModuleX has 2 test screens to run and ModuleY with 10 test windows.

You can do something like this:

 ant runTestsOnModule -Dtestmodule="ModuleX" OR to test all modules by calling ant tests <target name="runTestsOnModule"> <antCall target="testcase${testmodule}"/> </target>' <! -- run single module --> <target name="runTestsOnModule"> <antCall target="testcase${testmodule}"/> </target> <!--run all tests--> <target name="tests"> <antcall target="testcaseModuleX"> <antcall target="testCaseModuleY"> </target> <target name="testcaseModuleX"> ..run junit task to call 2 testcase </target> <target name="testcaseModuleY"> ....run junit task to call 10 testcase </target> 
0
08 Sep '14 at 11:02
source share



All Articles