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

Ant really doesn't have parameters_ for the build file. I can think of a few ways to do this:

Use a special target to specify the tests. You can use the <for/> task from AntContrib to allow you to specify multiple tests. You'll need to download the Ant-Contrib jar file. I recommend placing it inside your project under the `${basedir}/antlib/antcontrib" directory. That way, when others checkout your project, they get the needed 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>

You cab now run multiple tests like this:

$ ant -D"test-one test-two test-three" select-test
查看更多
霸刀☆藐视天下
3楼-- · 2019-01-23 01:08

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.

查看更多
4楼-- · 2019-01-23 01:12

I tried the solutions posted here for the very same original question. Yes just use ant -D<arg_name>. THe -D is a "keyword" I guess. I'm no ant expert and have not read the manuals in detail. Then inside the ant XML files can be accessed like: ${arg_name}

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

查看更多
Ridiculous、
5楼-- · 2019-01-23 01:14

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.

查看更多
疯言疯语
6楼-- · 2019-01-23 01:20

Lest say you have two modules in your project ModuleX and ModuleY where ModuleX has 2 testcases to run and ModuleY with 10 testcases.

You could 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>
查看更多
迷人小祖宗
7楼-- · 2019-01-23 01:21

For the arguments , there is Facility called property. You need to set the property. As in ANT plain arguments is taken as target name.

查看更多
登录 后发表回答