Reputation: 6657
I have a build system where many of the components are the exact same, so I've been changing it to use 'import' on a common file for most build tasks. Then the build system also needs to build any modules that it's dependent on. So I have roughly...
common.xml
project1.xml
- import common.xml
project2.xml
- include project1.xml
- import common.xml
There's a target named 'build' in common.xml. I want project2 to be able to include project1 and use extension-point to place a dependency on the build target. But for some reason, it doesn't recognize that project1 has a build target. If I change it to depend on a target that is defined in project1.xml, then it's fine. But I need to be able to specify a target that's in common.xml.
Is there anyway to do this?
No ant-contrib. The project used to use that, but I'm trying to get rid of it so we don't have horrible, procedural, build system code. The project has many cases of dependencies that look like a diamond and a proper dependency graph (one that ant generates automatically) would help greatly.
So I created a few simple files to illustrate my problem.
<project name="top" default="build">
<include file="mid.xml" as="mid" />
<target name="build">
<antcall target="mid.build" />
</target>
</project>
<project name="mid" default="build">
<!-- <target name="build"> -->
<!-- <echo message="build mid" /> -->
<!-- </target> -->
<import file="common.xml" />
</project>
<project name="common">
<target name="build">
<echo message="build common" />
</target>
</project>
This fails. top.xml can't find mid.build. When I run ant -p to list the targets, it shows build and common.build. So I tried adding in to the mid.xml build file and I got it to build correctly. The only problem now is that if I uncomment the XML above for mid to define its own build step, it get overwritten by the import and ignored. So it ends up becoming [build, common.build] instead of [build, mid.build].
Upvotes: 2
Views: 754
Reputation: 78145
Try using the include
task instead, in particular at the level of mid.xml
:
<project name="mid" default="build">
<include file="common.xml" />
</project>
The 'include' and 'import' tasks have alternate approaches to task overriding. See 'How is <import> different from <include>?' in the include
task docs:
The short version: Use import if you intend to override a target, otherwise use include
It sounds like you want the common 'build' target to be picked up, unless it is defined in mid. The problem is that with the 'import' task the read-in (i.e. common.xml) version will override the one in mid.xml. Because 'include' doesn't allow overrides, the one defined in the caller (mid.xml) is not overridden when common.xml is read.
Example: for the above mid.xml, ant -f top.xml
reports:
build:
mid.build:
[echo] build common
With a build
target override in mid.xml:
<project default="build" name="mid">
<target name="build">
<echo message="build mid" />
</target>
<include file="common.xml" />
</project>
the result becomes:
build:
mid.build:
[echo] build mid
Upvotes: 2