7.1. Connecting Ant to Eclipse

Problem

You want to start working with Ant from Eclipse, but you need to know how to connect Ant to Eclipse.

Solution

All you have to do is to add a build.xml file to an Eclipse project. Eclipse will know that a file with that name should be treated as an Ant build file.

Discussion

As an example, we’ll create an Ant build file, build.xml, that builds an Eclipse project, storing the resulting .class file in a .jar file stored in the directory we want. To follow along, create a Java Eclipse project named AntProject.

Store the source for this project in a folder named src and the output in a folder named bin. In the third step of the New Project dialog, the Java Setting dialog, click the Source tab and the Add Folder button. Then click the Create New Folder button to open the New Folder dialog. Enter src in the Folder name box and click OK twice. Eclipse will ask:

Do you want to remove the project as source folder and update build output folder to 
'AntProject/bin'?

Click Yes, which gives you the results shown in Figure 7-1. Click Finish to finish creating the project, which now includes src and bin folders.

Creating the AntProject project

Figure 7-1. Creating the AntProject project

Add a new class, AntClass, in the org.cookbook.ch07 package. We’re just going to use some sample code in this class to display a message, as shown in Example 7-1.

Example 7-1. Simple Ant test class

package org.cookbook.ch07; ...

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.