20

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?

Chris Forrence
  • 10,042
  • 11
  • 48
  • 64
Anuja Chinchwadkar
  • 291
  • 2
  • 4
  • 7

2 Answers2

21

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>
Brett Kail
  • 33,593
  • 2
  • 85
  • 90
  • `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.` Are you aware if this also applies to the `ant->antfile` task? https://stackoverflow.com/questions/68594740 – tresf Jul 30 '21 at 17:52
3

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" />
Silas Greenback
  • 1,200
  • 8
  • 15