如何将parameter passing给Ant任务?

我对Ant不太好,但是我们将它用作构build工具。 现在我们可以运行“ant test”,它会运行所有的unit testing。

不过,我希望能够做一些像ant test some_module东西,并让它接受some_module作为参数,并且只testing一下。

我还没有能够find如何将命令行parameter passing给Ant – 任何想法?

一个解决scheme可能如下。 (我有一个这样的项目。)

有一个单独的目标,类似于一个filesettesting,只限于一个类的testing。 然后在ant命令行中使用-D传递该类的名称:

 ant -Dtest.module=MyClassUnderTest single_test 

在build.xml中(高度缩减):

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

那么在你的testing目标中使用一些条件并指定-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> 

从ant常见问题适应了一下。

你可以在调用ant时在命令行上定义一个属性:

 ant -Dtest.module=mymodulename 

那么你可以使用它作为任何其他ant属性:

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

看看Ant的手册 。

你也可以定义一个可选的默认值,可以通过命令行来replace一个属性,例如

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

并运行它为:

 ant test -DmoduleName=ModuleX 

我试着在这里发布的解决scheme,原来的问题。 是的,只需使用ant -D<arg_name> 。 我猜 – -D是一个“关键字”。 我不是ant专家,没有详细阅读手册。 然后在ant里面可以像访问XML文件一样: ${arg_name}

例如,你可以有一个参数名称: arg.myarg ,所以在XML ${arg.myarg}

ant真的没有build立文件的parameters_。 我可以想到几个方法来做到这一点:

使用特殊的目标来指定testing。 您可以使用AntContrib中的<for/>任务来指定多个testing。 您需要下载Ant-Contrib jar文件。 我build议把它放在`$ {basedir} / antlib / antcontrib“目录下,这样当别人签出你的项目时,他们就得到了所需的Ant-Contrib jar文件。

 <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> 

你现在出租车运行这样的多个testing:

 $ ant -D"test-one test-two test-three" select-test 

对于争论,有设施称财产。 你需要设置属性。 正如在ANT中一样,简单的参数被当作目标名称。

恐怕你的项目ModuleX和ModuleY有两个模块,其中ModuleX有两个testing用例运行,ModuleY有十个testing用例。

你可以做这样的事情:

 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>