Override properties in an Ant object

We use many properties in our Ant scripts, starting with Eclipse. I want to set up a parallel deployment that builds a project with slightly different property values ​​and deploys elsewhere ... The deployment location is also a property.

[How] can my new goal update some properties for custom test values ​​and then run the regular goal to get the desired result?

A simple example script is very welcome, I know Ant enough to get past :)

+5
source share
3 answers
<?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>
+6
source

antcall (https://ant.apache.org/manual/Tasks/antcall.html), , , , -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

, "" "param" ( , , "" ), ( "param" ).

, 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
+7

, , .

For example, create a base file, for example base.xml, which will consist of all common properties and goals:

<?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>

Then, in the main file (for example, build.xml) import,, the file overrides the properties:

<?xml version="1.0" encoding="UTF-8"?>
<project name="MyProject">
  <import file="${basedir}/base.xml"/> <!-- File to include -->
  <!--<import><url url="https://example.com/base.xml"/></import>--> <!-- Include remote file -->
  <property name="admin.user" value="root"/> <!-- Overriden property -->
  <property name="admin.pass" value="new_pass"/> <!-- Overriden property -->
  <target name="job1">
  <!-- Overriden job1 -->
  </target>
  <target name="job2">
  <!-- Overriden job2 -->
  </target>
</project>

In Example GitHub: National-Theatre/base-build-xml.

For parallel execution you can use paralleltask .

0
source

All Articles