How to add custom Ant build script to Java project in Eclipse
- Details
- Written by Nam Ha Minh
- Last Updated on 07 August 2019   |   Print Email
By default, Eclipse does not generate any Ant build file for project. However you can add your own Ant script for the project when required. Suppose you want to have Eclipse bundles your dynamic web project in a WAR file after every build of the project. Follow these steps:
Create an Ant build script in Eclipse:
First off, we need to create our own Ant build file. Right click on the project, select New > Other… from context menu. In the New dialog, select XML > XML File:
Click Next. In the New XML File dialog, type antbuild.xmlinto File name field:
Click Finish, the empty antbuild.xml file is created under project’s root directory. Write content for this file as follows:
<?xml version="1.0" encoding="UTF-8"?> <project name="project"> <description> This is a custom Ant build file which contains a target that package the web application as a deployable WAR file. </description> <target name="makeWAR" description="Bundles the application as a WAR file"> <war destfile="UploadServlet30.war" basedir="WebContent" needxmlfile="false"> </war> </target> </project>
The build file contains only one target - makeWAR, which uses war task to generate WAR file UploadServlet30.war includes files from WebContent directory. The property needxmlfile is set to false because the project is using Servlet 3.0 API which does not require web.xml file.
Note: it is more convenient to edit Ant build file using Ant editor in order to take full advantages of code hint and completion for Ant script. To do so, right click on Ant build file, select Open With > Ant Editor. If the Ant Editor is not available in the context menu, select Other…, and select Ant Editor from Editor selection dialog.
Add Ant build to project’s builders
Next, we need to make some configurations to tell Eclipse executing our custom Ant build after every project build.
Open project’s properties dialog by right click on the project and select Properties (or from main menu Project > Properties). In the properties dialog, select Builders, you will see a screen like this:
This screen shows a list of builders which are used by Eclipse when building the project. The order of builders does matter: the top most is called first, then to the second one, and so on.
To add a new Ant build to the list, click the New… button. In the dialog Choose configuration type, select Ant builder and click OK:
In the Edit Configuration dialog, under Main tab:
- Type Ant_Builder into Name field.
- Under Buildfile section, click Browse Workspace to select antbuild.xml file created previously.
- Under Base Directory section, click Browse Workspace to select project’s root directory.
Now switch to Targets tab. In this screen, we can assign targets from our Ant build file for 4 build actions:
- After a “Clean”.
- Manual Build.
- Auto Build.
- During a “Clean”.
Since we need to build a WAR file automatically after every build, we should assign the target makeWAR for Auto Build action. Click button Set Targets to the right of Auto Build section. In the Set Targets dialog, select the target makeWAR:
Click OK. The target makeWAR is now assigned to Auto Build action:
Note: You can select multiple targets for a build action.
Click OK to close the Edit Configuration dialog and return to project properties dialog, our new builder – Ant_Builder is appended to the list:
The Ant_Builder is at the end of the list which means it is executed lastly, after other builders.
Note: use Up or Down button to change order of the builder.
Ant build execution in Eclipse
Because we assigned the target makeWAR for Auto Build action, the target is executed after every Eclipse’s auto build, as seen in the Console view:
And a WAR file, UploadServlet30.war, is generated after every build, as a result of executing the custom ant build script.
Remove Ant build from project’s builders
When the custom Ant build is no longer needed, we can disable or remove it by clicking the checkbox to disable or clicking button Remove to remove it completely.
The builder is removed from builders list, but the ant build file is not deleted.
Related Tutorials:
Other Eclipse Tutorials:
- How to use Eclipse IDE for Java EE Developers
- How to create, build and run a Java Hello World program with Eclipse
- How to change font for Java code in Eclipse
- How to Add Copyright License Header for Java Source Files in Eclipse
- How to generate Javadoc in Eclipse
- How to create JAR file in Eclipse
- How to generate WAR file in Eclipse
- How to pass arguments when running a Java program in Eclipse
- How to create Java web project with Maven in Eclipse
- 25 Eclipse Shortcut Keys for Code Editing
Comments