Using custom Ant build script for Eclipse projects
by, 02-05-2012 at 05:25 AM (5885 Views)
Ant is XML-based script for building Java applications. Ant is popularly used in Java development because of its powerful and flexibility. It makes developer’s life easier by automatically processing repetitive tasks that happen before, during and after building of Java applications. Eclipse is a great IDE which supports Ant by integrating Ant’s libraries as its plug-in.
Generally, you may not deal with any Ant script during development of a Java project in Eclipse. However if your project has a quite complex process that involves in building or testing of a application and the Eclipse IDE itself cannot help, it’s time to consider to use custom Ant build script for the project. For example, suppose you have to package your application as a JAR file, include some documents and put all together in a zip file, then finally upload the zip archive to a FTP server. This process has to be made after every build of the application. It would be boring and time-consuming if you do all this repetitive tasks again and over again, thus using a custom Ant script to automate the tasks in this scenario will make things much easier.
In this article you will learn how to configure a custom Ant build script in Eclipse to automate the repetitive tasks of the above scenario.
The Eclipse IDE has built-in support for Ant so that you don’t have to download Ant distribution to run Ant script.
Suppose you already have written a Java project in Eclipse. After the project is built (by Eclipse’s auto-build feature), you want to do the following tasks:
- Package the compiled classes as a JAR file.
- Create a distribution directory.
- Copy two support documents to the distribution directory.
- Compress the distribution directory into a single zip archive.
- Upload the Zip archive to a FTP server.
Creating Ant build file
Right click on the project name in the Project Explorer view, select New -> File. In the New dialog, type antbuild.xml as File name:
Figure: Create new Ant build file
Click Finish, and then add some code for the antbuild.xml file as following:
In our custom build, we created 6 targets. The first 5 targets are equivalent to the 5 tasks in the project’s scenario. The last target will be the main one that calls the others in the order as specified by the depends attribute of the target default. Each target simply prints out a message corresponding to its purpose, by using the <echo /> tag. We don’t write real Ant tasks for the targets because this article’s purpose is to demonstrate how a custom build can be used with Eclipse, and the implementation of details is up to you. Ant provides various tasks that help you to complete the described scenario, here are some suggestions:XML Code: Content of the antbuild.xml file<project name="Custom Ant Build"> <target name="createJar"> <echo message="Creating JAR file..." /> </target> <target name="makeDirectory"> <echo message="Making dist directory..." /> </target> <target name="copyDocument"> <echo message="Copying documents..." /> </target> <target name="zipDist"> <echo message="Compressing data..." /> </target> <target name="upload"> <echo message="Uploading to FTP server..." /> </target> <target name="default" depends="createJar, makeDirectory, copyDocument, zipDist, upload" /> </project>
- Task jar: creates a JAR file
- Task mkdir: creates a directory
- Task copyfile: copies files.
- Task zip: creates a zip file.
- Task FTP: performs various operations with a FTP server.
Adding custom Ant build to the project
Select Project -> Properties from the main menu. In the project properties dialog, select Builders:
Figure: Configure Builders for the project
You can see the Java Builder in the list on the right, this is the default build which Eclipse uses to build the project. We cannot modify this default build. To add our custom build script, click New. In the dialog Choose configuration type, select Ant Builder, click OK:
Figure: Create Ant Builder type
The dialog Properties for New_Builder appears to let us configure our custom build.
- Type Ant_Builder into the Name field.
On the Main tab:
- Click Browse Workspace… under Buildfile section to select the Ant build script.
- Under the Base Directory section, click Browse Workspace to select the directory in which our Ant script will consider as its home directory.
Figure: Configure Ant builder
Select Targets tab. This tab allows us to specify when and what will be executed. There are four point of time (event) that we can select to run our custom build script:
- After a “Clean”: run the script after a clean project action is performed.
- Manual Build: run the script before or after a manual build action is performed. We can specify before or after later.
- Auto Build: run the script before or after an auto build action is performed.
- During a “Clean”: run the script during a clean action is performed.
On the right of each event, there is a button Set Targets. This button allows us to specify the target in our Ant script will be executed. We select the event Manual Build, click Set Targets button, the target default is automatically selected:
Figure: Select target to run
Click OK to close the Set Targets dialog and return to the Properties for New_Builder dialog:
Figure: Configure targets for build event
Click OK to close this dialog and return to the Project properties dialog:
Figure: New Ant builder is added
Our custom build, Ant_Builder is added to the Builders list, after the built-in Java Builder. The order of builders does matter here: Eclipse will process the builders in that order. The Ant_Builder is after Java Builder, so Eclipse will execute Java Builder first, then to Ant_Builder. If you want to change the order, select the builder name, click Up or Down button. You can add as many custom builders as you want, and make sure everything are in order.
Testing the custom build in Eclipse
Now, let’s test how the custom build gets running. Click OK to close the project properties dialog. Because we have set to run our custom build for the event Manual Build, we can test by manually build the project by selecting Project -> Build All from the main menu:
Figure: Manually build all
After the project is built, our custom ant build is executed and we will see some outputs in the Console view, like the following screen:
Figure: Console output for build
That’s what we’d expect! We have successfully configured a custom build for the project.
Writing build script with Ant Editor
In order to write Ant script efficiently, the XML file should be opened by Ant editor which is not opened by default for XML file. In the Project Explorer view, right click on the Ant build file, select Open With -> Ant Editor:
Figure: Open the XML with Ant Editor
Eclipse will provide code completion for writing Ant script:
Figure: The Ant script editor
Configure Ant settings
You can configure various Ant’s runtime and editor settings via the Preferences dialog by selecting Window -> Preferences from the main menu. Then select Ant branch:
Figure: Configure Ant settings