How do I pass an argument to an Ant task?

2019-01-23 00:29发布

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?

标签: ant
9条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-01-23 01:27

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>
查看更多
爷的心禁止访问
3楼-- · 2019-01-23 01:31

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
查看更多
forever°为你锁心
4楼-- · 2019-01-23 01:33

You could try this to access one target at a time. Add these lines to your build.xml file :

<project name="whatever" default="default">
    <input message="Please select module:" addproperty="mod" />
    <target name="default" depends="${mod}/>
...
</project>

This allows you to enter the module you want to execute and execute that itself instead of running the whole build.xml

You might need to make a few more changes to your build.xml for this to work perfectly.

查看更多
登录 后发表回答