O'Reilly Book Excerpts: Ant: The Definitive Guide, Second Edition
Integrating Ant with Eclipse, Part 2
Editor's Note: In last week's excerpt from Ant: The Definitive Guide, 2nd Edition, Steve Holzner showed off the basics of integrating Ant with Eclipse. But if you've ever written a
build.xml file for a large project, you know that there's more to Ant than spelling out the targets. Providing a build script that can be customized to the developer's OS, file-system layout, and other preferences is important for keeping a team of developers working consistently. In this excerpt, Steve shows how to use Eclipse to set property values and environment variables that can be picked up by Ant. He also shows how to configure the Ant editor to work on Ant files from within Eclipse, how to use a different version of Ant, and more.
Modifying the Ant Classpath
When using an optional or custom task, adding extra libraries to the classpath may be necessary. The Ant classpath can be modified globally or by using an individual project's launch configuration. To set the Ant classpath for an individual Eclipse project, open the Ant launch configuration for the project and click the Classpath tab. You can add external JARs by clicking the Add External JARs button.
You can modify the Ant classpath globally for all projects. To do that, select Window→ Preferences→ Ant→ Runtime, and click the Classpath tab. You can add JAR files as needed here, and they'll be used globally for all Ant builds.
Setting Property Values
You can set global Ant properties using the Ant preferences page, which you open by selecting Window→ Preferences→ Ant→ Runtime and clicking the Properties tab. To add a new property, click the Add Property button and fill in the Name and Value fields in the dialog that appears. This will set the global properties for all Ant builds in Eclipse, and since properties are immutable, you will be setting the final value for such properties.
You can set properties on a project-by-project basis by setting the project's Ant launch configuration. Click the Properties tab in the launch configuration (as seen in Figure 11-8), deselect the "Use global properties as specified in the Ant runtime preferences" checkbox, and click the Add Property button to set new properties.
Figure 11-8. Setting properties
Setting Environment Variables
You can set the environment variables you want passed to Ant, but you have to ensure Ant will run in its own JRE (the default). In the project's Ant launch configuration, click the JRE tab and click the Separate JRE radio button. To set environment variables, click the Environment tab, shown in Figure 11-9, and click the New button to create a new environment variable.
Figure 11-9. Setting environment variables
When you click the New button, the New Environment Variable dialog appears. Enter the name and value of the environment variable in the appropriate fields and click OK.