如何将参数传递给Ant任务?

Jon*_*dad 43 ant

我对Ant不太满意,但我们将它用作构建工具.现在,我们可以运行"蚂蚁测试",它将贯穿所有单元测试.

但是,我希望能够做一些类似的事情ant test some_module并让它some_module作为参数接受,并且只测试它.

我一直无法找到如何将命令行参数传递给Ant - 任何想法?

mar*_*ton 38

一种解决方案可能如下.(我有一个项目可以做到这一点.)

有类似于一个个独立的目标testfileset制约的测试只有一个类.然后-D在ant命令行中使用该类的名称:

ant -Dtest.module=MyClassUnderTest single_test
Run Code Online (Sandbox Code Playgroud)

在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>
Run Code Online (Sandbox Code Playgroud)

  • 马丁在这里略微低估了这个案子.根据蚂蚁常见问题解答,属性是实现您想要的方式.有关详细信息,请参见http://ant.apache.org/faq.html#passing-cli-args (5认同)

Jea*_*ean 7

您还可以使用可选的默认值定义属性,该默认值可以通过命令行替换,例如

<target name="test">
  <property name="moduleName" value="default-module" />
  <echo message="Testing Module: ${moduleName}"/>
  ....
</target>
Run Code Online (Sandbox Code Playgroud)

并运行它:

ant test -DmoduleName=ModuleX
Run Code Online (Sandbox Code Playgroud)


Ric*_*ler 6

如何在测试目标中使用一些条件并指定-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>
Run Code Online (Sandbox Code Playgroud)

ant faq改编了一下.


Pet*_*lka 5

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

ant -Dtest.module=mymodulename
Run Code Online (Sandbox Code Playgroud)

然后你可以将它用作任何其他 ant 属性:

...
    <fileset dir="${test.dir}" includes="**/${test.module}.class" />
...
Run Code Online (Sandbox Code Playgroud)

看一下Ant 的手册