Ant (with ant-contrib) build - path / dependency issue - Reference Not Found

I'm trying to create a system where I can use a common build.xml as a template for a vast majority of our builds. I'm very close. Basically, the application-level build.xml file just needs to define a few things (target jar, dependencies (as a list), and a few other items of state) and then import the template build.xml.

The problem I have is that I'm trying to take the dependencies and build up a classpath programmatically. I'm very close, I think. However, once the classpath is referenced in the compile step, it tries to resolve a reference which seemingly was properly prepared before the compile step occurs. And, it results in a BUILD FAILED : Reference cpDependency4 not found.

Note: COTS stuff is stored from the same directory, so making that part work was trivial. The Project dependencies may all come from different subdirectories and require pulling in the child dependencies/ directory in also.

Here's an example of the items prepared in the application build.xml:

<property name="PROJECT_DEPENDENCIES" value="/path/to/other/dependency1,/path/to/other/dependency2,/path/to/other/dependency3,/path/to/other/dependency4" />
<property name="COTS_DEPENDENCIES" value="commons-io-*.jar,log4j-*.jar,spring-aop-*.RELEASE.jar,spring-beans-*.RELEASE.jar,spring-context-*.RELEASE.jar,spring-core-*.RELEASE.jar,spring-jms-*.RELEASE.jar,spring-oxm-*.RELEASE.jar,spring-web-*.RELEASE.jar"/>
<!-- ... -->
<import file="${DEVELOPMENT_BASE_DIR}/common/ant/antTemplate.xml" />  

and then the antTemplate file contains:

<path id="cpDependency0" />
<var name="trackDependencyPath" value="cpDependency0" />
<var name="index" value="0"/>        
<for list="${PROJECT_DEPENDENCIES}" param="dependency">
  <sequential>
    <var name="prior" value="${index}"/>
    <math result="index" operand1="${index}" operation="+" operand2="1" datatype="int" />        
    <property name="var${index}" value="@{dependency}" />
    <path id="cpDependency${index}">          
      <path refid="cpDependency${prior}"/>
      <fileset dir="@{dependency}/jar/${env}" includes="*.jar" />
      <fileset dir="@{dependency}/jar/${env}/dependencies" includes="*.jar" />
    </path>
    <var name="trackDependencyPath" value="cpDependency${index}" />
  </sequential>      
</for>

<path id="classpath.compile">    
  <fileset dir="${COTS_DIR}/main" includes="${COTS_DEPENDENCIES}" />            
  <path refid="${trackDependencyPath}" />
</path>

Which seems to set things up properly, but... later, when it hits the compile target:

<target name="compile" depends="init">
  <echo message="Compiling ${ant.project.name}..." />
  <javac source="1.7" target="1.7" debug="${debug}" srcdir="${SOURCE_JAVA_MAIN_DIR}" destdir="${BUILD_JAVA_MAIN_DIR}" classpathref="classpath.compile" />
  <jar destfile="${PACKAGE_DIR}/${TARGET_JAR}" basedir="${BUILD_JAVA_MAIN_DIR}" excludes="*.jar" />
  <copy todir="${PACKAGE_DIR}">
    <path refid="classpath.compile" />
  </copy>
</target>

I get the following error (on the javac line):

BUILD FAILED
/path/to/common/ant/antTemplate.xml:188: Reference cpDependency4 not found.

Answers


Your problem is that :

<path id="classpath.compile">    
  <fileset dir="${COTS_DIR}/main" includes="${COTS_DEPENDENCIES}" />            
  <path refid="${trackDependencyPath}" />
</path>

refers not to a refid but to a property (or antcontrib variable) which in this case has the same value => 'cpDependency4'

-- EDIT after comment --

Even using the string 'cpDependency4' won't help as the path id is never set correctly within for loop, as some test reveals :

<for list="${PROJECT_DEPENDENCIES}" param="dependency">
  <sequential>
    ...
    <var name="trackDependencyPath" value="cpDependency${index}" />
    <!-- put pathconvert here -->
  </sequential>      
</for>

Try :

<pathconvert property="foo"><path refid="cpDependency4"/></pathconvert>

or

<pathconvert property="foo"><path refid="cpDependency${index}"/></pathconvert>

and you'll get :

Reference cpDependency4 not found.

in both cases. Increase noiselevel to debug and you'll see :

Adding reference: cpDependency0
Setting ro project property: trackDependencyPath -> cpDependency0
Setting ro project property: index -> 0
Setting ro project property: prior -> 0
Setting ro project property: index -> 1
Setting project property: var1 -> /path/to/other/dependency1
Adding reference: cpDependency${index}
Property "env" has not been set
Property "env" has not been set
Setting ro project property: trackDependencyPath -> cpDependency1
Setting ro project property: prior -> 1
Setting ro project property: index -> 2
Setting project property: var2 -> /path/to/other/dependency2
Overriding previous definition of reference to cpDependency${index}
Adding reference: cpDependency${index}
Property "env" has not been set
Property "env" has not been set
Setting ro project property: trackDependencyPath -> cpDependency2
Setting ro project property: prior -> 2
Setting ro project property: index -> 3
Setting project property: var3 -> /path/to/other/dependency3
Overriding previous definition of reference to cpDependency${index}
Adding reference: cpDependency${index}
Property "env" has not been set
Property "env" has not been set
Setting ro project property: trackDependencyPath -> cpDependency3
Setting ro project property: prior -> 3
Setting ro project property: index -> 4
Setting project property: var4 -> /path/to/other/dependency4
Overriding previous definition of reference to cpDependency${index}
Adding reference: cpDependency${index} 

Need Your Help

How to set a variable to the indexPath.section of the cell selected?

ios objective-c uitableview

When a user taps on a cell in my UITableView, I want to set the sectionSelected variable to the indexPath.section of that cells respective section, and then send that variable over to the next view

Does jmeter timeout SOAP requests

java web-services soap jmeter soap-client

While sending WebService(SOAP) requests, I occasionally receive the response below, and always for requests that take more then 60 seconds to complete.