Summary: | Reference not seen by top level tasks in sub-projects | ||
---|---|---|---|
Product: | Ant | Reporter: | Juerg Wanner <juerg> |
Component: | Core tasks | Assignee: | Ant Notifications List <notifications> |
Status: | NEW --- | ||
Severity: | major | CC: | fajwilson |
Priority: | P1 | ||
Version: | 1.7.0 | ||
Target Milestone: | --- | ||
Hardware: | All | ||
OS: | All | ||
Attachments: |
The 'master' buildfile
The sub buildfile The imported buildfile tweak to "Ant" task |
Description
Juerg Wanner
2006-05-17 10:33:52 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.
Created attachment 18298 [details]
The sub buildfile
Created attachment 18299 [details]
The imported buildfile
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. 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. 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> Created attachment 25422 [details]
tweak to "Ant" task
|