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
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:

<?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>
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:

- 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”.



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:
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.
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
About the Author:

Comments