How do I set up multiple projects to share the same Ant targets?
Author: Deron Eriksson
Description: This Ant tutorial describes how to let multiple projects share the same Ant targets using the import task.
Tutorial created using: Windows XP || JDK 1.5.0_09 || Eclipse Web Tools Platform 1.5.1 || Tomcat 5.5.20


Page:    1 2 >

Ant's import task is probably the easiest way of referencing another AntSW build file from a project's build file. Why would we want to do this? Let's say that you developed some nice Ant targets for one of the many JavaSW web application projects that you work on. Things would quickly get messy if you developed a large build.xml file that you copy/pasted into each of your projects, since it would be difficult keeping all the build files synchronized as you eventually modified them. It would be much cleaner if you could have one main build file that each of your various projects referenced. Perhaps the easiest way of doing this is with the Ant 'import' task.

To demonstrate, consider the following EclipseSW working set shown below. Let's say 'tomcat-demo' is the original project that we used to develop our original build file, but we'd like to use the same targets in 'tomcat-demo-2' and other projects. To do this, I copied tomcat-demo's build.properties file and build.xml file to a new project called 'ant-utilities' and I renamed them to be build-main.properties and build-main.xml. I made a couple very minor tweaks in each file, such as the 'project-name' property.

Eclipse Navigator View

After doing this, I deleted tomcat-demo's build.properties file, and I modified tomcat-demo's build.xml file as shown below. I included a reference to the ant-utilities build-main.properties file using the Ant property element, and I used the import task to reference the ant-utilities build-main.xml file.

tomcat-demo build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="tomcat-demo" default="" basedir=".">

	<property name="project-name" value="${ant.project.name}" />
	<property file="../ant-utilities/build-main.properties" />
	<import file="../ant-utilities/build-main.xml" />

</project>

The tomcat-demo-2 project's build.xml file is identical to tomcat-demo's build.xml except the project name is different.

tomcat-demo-2 build.xml

<?xml version="1.0" encoding="UTF-8"?>
<project name="tomcat-demo-2" default="" basedir=".">

	<property name="project-name" value="${ant.project.name}" />
	<property file="../ant-utilities/build-main.properties" />
	<import file="../ant-utilities/build-main.xml" />

</project>

If we add the tomcat-demo build.xml and tomcat-demo-2 build.xml files to the Ant view in Eclipse, we can see that they now both share all the targets available in the ant-utilities project.

Eclipse Ant View

(Continued on page 2)

Page:    1 2 >