Eyad Ebrahim
Eyad Ebrahim

Reputation: 991

Invoking an Ant Target Automatically upon Import

I have a rather simple question. we usually do this as a build.xml file for Ant

<project default="" name="Caterpillar Common settings">
    <property name="some.name" value="some.value" />
</project>

So if we import this one in another .xml project it will set the 'some.name' to 'some.value'.

Some times setting this 'some.name' is a bit complicated and needs some logic that is written inside of couple of small targets.

QUESTION: how can I invoke a target automatically just like the tag in the top level does?

I tried <antcall> and apparently top-level is not its cup of tea?

Upvotes: 2

Views: 800

Answers (2)

Eyad Ebrahim
Eyad Ebrahim

Reputation: 991

Use

<Sequential>
    <echo message="something" />
</sequential>

There is no need for macrodef to encapsulate this.

Upvotes: 1

Christopher Peisert
Christopher Peisert

Reputation: 24134

The Ant task may be used to call targets in other Ant projects.

By default, all of the properties of the current project will be available in the new project. Alternatively, you can set the inheritAll attribute to false and only "user" properties (i.e., those passed on the command-line) will be passed to the new project. In either case, the set of properties passed to the new project will override the properties that are set in the new project

One way to structure your build, would be to call your subproject first, which would then call the master project using the Ant task.

In the following example, project2.xml initializes properties, which are then used by the master project file build.xml.

project2.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="project2" default="initialize">
  <dirname property="project2.dir" file="${ant.file.project2}" />
  <property name="caterpillar.dir" location="${project2.dir}" />

  <target name="initialize">
    <property name="some.name" value="some.value" />
    <ant dir="${caterpillar.dir}" antfile="build.xml" target="build" />
  </target>
</project>

build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="Caterpillar-Common-Settings">
  <target name="build">
    <echo message="${some.name}" />
  </target>
</project>

The build would be initialized from the command line as follows:

$ ant -f project2.xml

Output

Buildfile: /home/caterpillar/project2.xml

initialize:

build:
     [echo] some.value

BUILD SUCCESSFUL
Total time: 0 seconds

Upvotes: 2

Related Questions