braveterry
braveterry

Reputation: 3744

When are Ant path references set?

When I use a path reference ID, Ant seems to evaluate any variables inside the definition before any tasks run. For example, ${common.dist} and ${common.lib} below seem to be evaluated before any tasks run.

<path id="compile.classpath">
      <fileset dir="lib">
         <include name="*.jar" />
      </fileset>
      <fileset dir="${common.dist}">
         <include name="*.jar" />
      </fileset>
      <fileset dir="${common.lib}">
         <include name="*.jar" />
      </fileset>
</path>

In the Ant output I see something like this:

Adding reference: compile.classpath
Property "common.dist" has not been set
Property "common.lib" has not been set
...
Build sequence for target(s) `package' is [...]
Complete build sequence is [...]

This makes it seem like the path reference is being processed before any targets are run.

I have a compile target like this:

  <target name="compile" depends="init,common">
    <javac destdir="build/classes" debug="true" deprecation="true" optimize="true">
      <src path="src/java" />
      <classpath>
        <path refid="compile.classpath" />
      </classpath>
    </javac>
  </target>

If I copy the guts of the path reference into the classpath element inside the compile target, things seem to work fine.

Upvotes: 0

Views: 1759

Answers (2)

Ian Roberts
Ian Roberts

Reputation: 122394

Any tasks outside a target are executed on every build, in order of appearance in the build.xml, before any targets are run. If you want to use properties in a <path> defined outside a target then you need to put the <property> task that defines the properties also outside a target, and before the <path>. If you need to load the properties within a target then you'll have to put the <path> definition inside a target too (either the same one or one that runs after the one defining the properties).

See this question (and my answer) for more details.

Upvotes: 2

dcernahoschi
dcernahoschi

Reputation: 15250

The answer is in the Ant manual - path like structures:

By default a path like structure will re-evaluate all nested resource collections whenever it is used, which may lead to unnecessary re-scanning of the filesystem ...

I think you maybe forgot to set the ${common.dist} and ${common.lib} properties. They should be outside any target:

<property name="common.dist" location="dist"/>
<property name="common.lib" location="lib"/>

Upvotes: 2

Related Questions