We use lots of properties in our Ant scripts, run from Eclipse. I want to set up a parallel deployment which builds the project with slightly different property values, and deploys to a different location... deploy location is also a property.
[How] can my new target update some properties to custom test values and then run the normal target to get the desired result?
Simple sample script is very welcome, I only know enough Ant to get by :)
You can use antcall (https://ant.apache.org/manual/Tasks/antcall.html) to override a property using param in the called target, for example, consider this file example-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>
and see the result
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
Note that the value of the property has changed in the second invocation of target "second" using "param" (and the change is propagated to targets called inside the called target, "third" in this example), but not in the other calls (without "param").
However, as reported in the documentation https://ant.apache.org/manual/Tasks/antcall.html,
Properties defined on the command line can not be overridden by nested
<param>
elements.
This can be see by this invocation:
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
<?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>
Overriding properties can be done by splitting the logic into separate files which can be included as a base, then overridden.
For example, create a base file, like base.xml
which consist all the default properties and targets:
<?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 (such as build.xml
), override the properties then import
the base file:
<?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>
Example at GitHub: National-Theatre/base-build-xml
.
For a parallel execution, you can use parallel
task.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With