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:

create xml file

 

Click Next. In the New XML File dialog, type antbuild.xmlinto File name field:

type xml file name

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:

eclipse builders

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:

 

choose configuration type

 

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.

main tab

 

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:

 

set target

 

Click OK. The target makeWAR is now assigned to Auto Build action:

 

target assigned

 

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:

new builder appended

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:

ant build executed

 

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.

 

remove builder

 

The builder is removed from builders list, but the ant build file is not deleted.

 

Related Tutorials:

 

Other Eclipse Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Attachments:
Download this file (antbuild.xml)antbuild.xml[Ant build file]0.4 kB

Add comment

   


Comments 

#1qb2021-12-07 11:51
Hello, Where are the ant build builder properties stored? I had done this once before , but when I went to do a current build, the reference to the ant file was missing in the builder.
Quote