Bug 39597 - Reference not seen by top level tasks in sub-projects
Summary: Reference not seen by top level tasks in sub-projects
Status: NEW
Alias: None
Product: Ant
Classification: Unclassified
Component: Core tasks (show other bugs)
Version: 1.6.5
Hardware: All All
: P1 major with 1 vote (vote)
Target Milestone: ---
Assignee: Ant Notifications List
Depends on:
Reported: 2006-05-17 10:33 UTC by Juerg Wanner
Modified: 2010-05-10 11:28 UTC (History)
1 user (show)

The 'master' buildfile (283 bytes, text/plain)
2006-05-17 10:37 UTC, Juerg Wanner
The sub buildfile (91 bytes, text/plain)
2006-05-17 10:37 UTC, Juerg Wanner
The imported buildfile (165 bytes, text/plain)
2006-05-17 10:38 UTC, Juerg Wanner
tweak to "Ant" task (702 bytes, patch)
2010-05-10 11:17 UTC, Frank Wilson
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Juerg Wanner 2006-05-17 10:33:52 UTC
Given three buildfiles a.xml, b.xml and c.xml (see attachemnts)
a.xml calls b.xml with
<ant antfile="b.xml" target="b">
  <reference refid="somereference"/>

In b.xml there is an
<include file="c.xml"/>

and in c.xml the reference 'somereference' is used outside any target
<project ...>
  <pathconvert refid="somereference" .../>

The reference can not be seen in c.xml ??
Comment 1 Juerg Wanner 2006-05-17 10:37:10 UTC
Created attachment 18297 [details]
The 'master' buildfile

/tmp/a.xml:10: The following error occurred while executing this line:
/tmp/b.xml:3: The following error occurred while executing this line:
/tmp/c.xml:5: Reference somepath not found.
Comment 2 Juerg Wanner 2006-05-17 10:37:43 UTC
Created attachment 18298 [details]
The sub buildfile
Comment 3 Juerg Wanner 2006-05-17 10:38:14 UTC
Created attachment 18299 [details]
The imported buildfile
Comment 4 Peter Reilly 2006-09-22 10:50:03 UTC
If b.xml is written like:
<project name="b" default="b">
  <pathconvert refid="somepath" pathsep=":" property="xx"/>
  <target name="b"/>

it also fails.

however this works and may be used as a work-around for the moment.

<project name="b" default="b">
  <target name="b-init">
    <pathconvert refid="somepath" pathsep=":" property="xx"/>
  <target name="b" depends="b-init"/>

The problem is a little historic. In ant 1.5, it was
not possible to have top-level tasks (except property).
As this was the case, references would not be used except
in targets. As this was the case, it was decided to allow
references in sub-projects to be over-ridden by references
specified in the reference nested element of <ant>. This
is implemented by running all the top level tasks and types
of the sub-project and then setting the references set
in the <reference> element of ant.
Comment 5 Clark Archer 2010-03-13 17:15:34 UTC
Given Peter's explanation of why this occurs, should we just close this bug as a won't fix?  Seems like changing the current behavior could have dire implications for existing projects and the workaround seems reasonable.
Comment 6 Frank Wilson 2010-05-10 11:16:00 UTC
This is a message I posted to the users mailinglist which is relevant to this bug. The work around suggested by Peter Reilly will not work for my use case because the import task cannot be put inside a target element. Hence, references must be resolvable outside targets. 

I will attach to this bug report a tweak to the "Ant" task source that seems to work for me.


I've been looking at various ways of importing other ant buildfiles
from the classpath to reduce coupling in my multimodule build. This would
allow me to checkout only the code and build files I intend to

I've been looking at using Maven to facilitate this. My solution uses
the maven ant-run plugin to run a build.xml file with a classpath
that includes a file echo.xml on which build.xml depends (see diagram below).

However, this requires a few modifications to both ant and the ant-run 

The modification to the ant-run plugin is pretty trivial, we 
just need it to use ant 1.8.1 so we can put <javaresource/> types inside
<import/>, this basically a change to the ant-run pom.xml so that we bring
ant 1.8.1 onto the classpath instead of 1.7.1. We then reinstall the ant-run plugin into our repository.

Somewhat more siginificant is the change to ant. To support this mechanism 
we need to be able inherit references in the file being called by the 
"Ant" task. The Ant tasks supports this, but what is not clear is that 
it will not export references in the preamble of an ant build file. 
References are only available within targets. However, since the <import/> element 
cannot exist within a <target/> making references in the preable is really 
necessary in order for this to work.

Hence, I made a change to the "Ant" task, so that it brings in import references 
before the file (build.xml in the example) is parsed. Consequently, the
references should be available in the preamble.

The example I have given seems to work ok with this code change.

I would like to ask if this tweak to the ant task code is a good idea and
whether there is any chance of it or something similar being adopted in ant itself.

The change is only one line, but I am worried I might be missing some serious 

I attach a patch for the ant task which should apply cleanly to the ANT_181 tag on svn.


BuildCommon Project:

| |
| +-resources
|   |
|   +echo.xml // the ant file I want to import.
+-pom.xml // bog standard pom, builds a jar which has echo.xml at the root

<project name="echo">

	<target name="echo">
		<echo>Hello Modular Ant World!! </echo>


BuildClient Project:

+-pom.xml // standard pom, with ant-run executing build.xml in "compile" phase
+-build.xml // the ant file with classpath dependencies that we want to run.

// ... standard pom header

	<ant antfile="build.xml" target="echo" inheritrefs="true"/>


// ... dependencies

<project name="BuildClient">
		<javaresource name="echo.xml">
			<classpath refid="maven.compile.classpath" />

Comment 7 Frank Wilson 2010-05-10 11:17:08 UTC
Created attachment 25422 [details]
tweak to "Ant" task