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.7.0
Hardware: All All
: P1 major with 1 vote (vote)
Target Milestone: ---
Assignee: Ant Notifications List
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-05-17 10:33 UTC by Juerg Wanner
Modified: 2017-03-03 03:31 UTC (History)
1 user (show)



Attachments
The 'master' buildfile (283 bytes, text/plain)
2006-05-17 10:37 UTC, Juerg Wanner
Details
The sub buildfile (91 bytes, text/plain)
2006-05-17 10:37 UTC, Juerg Wanner
Details
The imported buildfile (165 bytes, text/plain)
2006-05-17 10:38 UTC, Juerg Wanner
Details
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"/>
</ant>

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

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

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

BUILD FAILED
/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"/>
</project>

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>
  <target name="b" depends="b-init"/>
</project>

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.

MAILINGLIST MESSAGE:

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
change.

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 
plugin. 

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 
consequences.

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

-------
Example
-------


BuildCommon Project:
--------------------

BuildCommon
|
+-src
| |
| +-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


echo.xml:
*********
<project name="echo">


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

</project>


BuildClient Project:
--------------------

BuildClient
|
+-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.

pom.xml
*******
// ... standard pom header

<build>
    <plugins>
      <plugin>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
          <execution>
            <phase>compile</phase>
            <configuration>
              <tasks>
              
	<ant antfile="build.xml" target="echo" inheritrefs="true"/>

              </tasks>
            </configuration>
            <goals>
              <goal>run</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
    </plugins>
  </build>

// ... dependencies


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

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