Anuja Chinchwadkar
Anuja Chinchwadkar

Reputation: 301

Set a property inside one ant target and use it in another target

My build file is

<target name="default">  
 <antcall target="child_target"/>  
 <echo> ${prop1}   </echo>  
</target>

<target name="child_target">  
 <property name="prop1" value="val1"/>   
</target>

I get an error that ${prop1} has not been set. How do I set a property in the target?

Upvotes: 21

Views: 22223

Answers (2)

Silas Greenback
Silas Greenback

Reputation: 1220

Old and probably dead issue I know, but a property file loaded outside targets but inside the project would also work. Android does this with local.properties like so:

<?xml version="1.0" encoding="UTF-8"?>
<project name="avia" default="help">

    <!-- The local.properties file is created and updated by the 'android' tool.
         It contains the path to the SDK. It should *NOT* be checked into
         Version Control Systems. -->
    <property file="local.properties" />

Upvotes: 3

Brett Kail
Brett Kail

Reputation: 33936

antcall creates a new project. From the Ant documentation:

The called target(s) are run in a new project; be aware that this means properties, references, etc. set by called targets will not persist back to the calling project.

Use depends instead:

<project default="default">
  <target name="default" depends="child_target">
    <echo>${prop1}</echo>
  </target>
  <target name="child_target">
    <property name="prop1" value="val1"/>
  </target>
</project>

Upvotes: 22

Related Questions