【问题标题】:How do I pass an argument to an Ant task?如何将参数传递给 Ant 任务?
【发布时间】:2009-12-16 00:41:09
【问题描述】:

我不太擅长使用 Ant,但我们将它用作构建工具。现在,我们可以运行“ant test”,它会运行所有的单元测试。

但是,我希望能够执行 ant test some_module 之类的操作,并让它接受 some_module 作为参数,并且只对其进行测试。

我还没有找到如何将命令行参数传递给 Ant - 有什么想法吗?

【问题讨论】:

  • 我知道这个问题已经有一段时间了,但如果你能接受答案,请接受。

标签: ant


【解决方案1】:

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

有一个类似于test 的单独目标,其中fileset 将测试限制为仅一个类。然后在 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>

【讨论】:

【解决方案2】:

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

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

然后运行它:

ant test -DmoduleName=ModuleX

【讨论】:

    【解决方案3】:

    在您的测试目标中使用一些条件并指定-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 faq

    【讨论】:

      【解决方案4】:

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

      ant -Dtest.module=mymodulename
      

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

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

      看看Ant's manual

      【讨论】:

        【解决方案5】:

        我尝试了此处发布的解决方案来解决相同的原始问题。是的,只需使用ant -D&lt;arg_name&gt;。我猜-D 是一个“关键字”。我不是蚂蚁专家,也没有详细阅读手册。然后在ant里面的XML文件可以像这样访问:${arg_name}

        例如,您可以有一个参数名称,如:arg.myarg,因此在 XML 中为 ${arg.myarg}

        【讨论】:

          【解决方案6】:

          您可以尝试一次访问一个目标。将这些行添加到您的 build.xml 文件中:

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

          这允许您输入要执行的模块并自行执行,而不是运行整个 build.xml

          您可能需要对 build.xml 进行更多更改才能使其完美运行。

          【讨论】:

            【解决方案7】:

            Ant 确实没有构建文件的参数_。我可以想到几种方法来做到这一点:

            使用一个特殊的目标来指定测试。您可以使用AntContrib 中的&lt;for/&gt; 任务来指定多个测试。您需要下载Ant-Contrib jar 文件。我建议将它放在您的项目中的“${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>
            

            您现在可以像这样运行多个测试:

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

            【讨论】:

            • 我的朋友传递了不同的参数,他是怎么做到的?示例:ant osname stagingEnv browser regression/smoke/sanity
            【解决方案8】:

            对于 arguments ,有一个名为 property 的 Facility。您需要设置属性。在 ANT 中,普通参数被视为目标名称。

            【讨论】:

              【解决方案9】:

              免得说您的项目 ModuleX 和 ModuleY 中有两个模块,其中 ModuleX 有 2 个要运行的测试用例,而 ModuleY 有 10 个测试用例。

              你可以这样做:

              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>
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 2012-01-29
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-08-23
                • 2018-10-19
                • 2015-04-16
                • 1970-01-01
                相关资源
                最近更新 更多