覆盖Ant目标中的属性

时间:2013-02-23 16:30:02

标签: ant

我们在Ant脚本中使用许多属性,从Eclipse运行。我想建立一个并行部署,它使用稍微不同的属性值构建项目,并部署到不同的位置......部署位置也是一个属性。

[如何]我的新目标可以将某些属性更新为自定义测试值,然后运行正常目标以获得所需的结果?

非常欢迎简单的示例脚本,我只知道足够的Ant来获取:)

3 个答案:

答案 0 :(得分:7)

您可以使用antcall(https://ant.apache.org/manual/Tasks/antcall.html)在被调用目标中使用param覆盖属性,例如,考虑此文件示例-antcall-properties.ant

<?xml version="1.0" encoding="UTF-8"?>
<project name="AntCall Properties and Params" default="first">
    <property name="my.property" value="initial" />

    <target name="first">
      <echo message="main: my.property=${my.property}"/>
      <antcall target="second" />
      <antcall target="second">
        <param name="my.property" value="changed"/>
      </antcall>
      <antcall target="second" />
    </target>

    <target name="second">
      <echo message="second: my.property=${my.property}"/>
      <antcall target="third" />
    </target>

    <target name="third">
      <echo message="third: my.property=${my.property}"/>
    </target>
</project>

并查看结果

ant -f example-antcall-properties.ant
Buildfile: example-antcall-properties.ant

first:
     [echo] main: my.property=initial

second:
     [echo] second: my.property=initial

third:
     [echo] third: my.property=initial

second:
     [echo] second: my.property=changed

third:
     [echo] third: my.property=changed

second:
     [echo] second: my.property=initial

third:
     [echo] third: my.property=initial

BUILD SUCCESSFUL
Total time: 0 seconds

请注意,在第二次调用target&#34; second&#34;时,属性的值已更改。使用&#34; param&#34; (并且更改将传播到被调用目标内部调用的目标,在此示例中为#34;第三个&#34;但在其他调用中不会传播(没有&#34; param&#34;)。

但是,正如文档https://ant.apache.org/manual/Tasks/antcall.html中所述,

  

命令行中定义的属性不能被嵌套的<param>元素覆盖。

这可以通过此调用看到:

ant -f example-antcall-properties.ant -Dmy.property="from command line"
Buildfile: example-antcall-properties.ant

first:
     [echo] main: my.property=from command line

second:
     [echo] second: my.property=from command line

third:
     [echo] third: my.property=from command line

second:
     [echo] second: my.property=from command line

third:
     [echo] third: my.property=from command line

second:
     [echo] second: my.property=from command line

third:
     [echo] third: my.property=from command line

BUILD SUCCESSFUL
Total time: 0 seconds

答案 1 :(得分:6)

<?xml version="1.0" encoding="UTF-8" ?>
<project default="all" basedir="."> 
    <taskdef resource="net/sf/antcontrib/antcontrib.properties" />
    <target name="all">
        <property name="prop" value="1" />
        <echo message="prop = ${prop}" />
        <var name="prop" unset="true"/>
        <property name="prop" value="2" />
        <echo message="prop = ${prop}" />
    </target>
</project>

答案 2 :(得分:1)

可以通过将逻辑拆分为单独的文件来完成覆盖属性,这些文件可以作为基础包含在内,然后重写。

例如,创建一个基本文件,例如base.xml,它包含所有默认属性和目标:

<?xml version="1.0" encoding="UTF-8"?>
<project>
  <!-- Sets the default properties. Override in your main build file when needed. -->
  <property name="admin.user" value="admin"/>
  <property name="admin.pass" value="admin"/>
  <target name="job1"/>
  <target name="job2"/>
</project>

然后在主文件(例如build.xml)中覆盖属性,然后import基础文件:

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject">
  <!-- Override properties -->
  <property name="admin.user" value="root"/>
  <property name="admin.pass" value="new_pass"/>

  <!-- Import default properties -->
  <import file="${basedir}/base.xml"/>
  <!--<import><url url="https://example.com/base.xml"/></import>-->

  <target name="job1">
  <!-- Overriden job1 -->
  </target>
  <target name="job2">
  <!-- Overriden job2 -->
  </target>
</project>

GitHub上的示例:National-Theatre/base-build-xml

对于并行执行,您可以使用parallel task