Sometimes we need to build our Java projects from outside Eclipse IDE, by executing an Ant build script from command line. For example, when we made some very little changes to the project and need to re-build it without opening Eclipse (which may take times and huge memory for the bulky IDE while we are busy on other stuffs). Another typical case is bug-fixing, re-configuration and re-deployment in a production environment (i.e. a web application running on a server) in which we cannot use an IDE to re-built the project. Having only project’s source code with all necessary jar files, the only missing thing is an Ant build file from which we can build the project from command line independently of Eclipse.

Although the build system in Eclipse does not based on Ant, Eclipse offers a function that lets us creating Ant build script for the project manually.

Suppose we are working on a Java web application project named “UploadServlet30” in Eclipse. To create Ant build file for this project, follow these steps:

  • Select File > Export from main menu (or right click on the project name and select Export > Export…).
  • In the Export dialog, select General > Ant Buildfilesas follows:Export dialog Ant Buildfiles

  • Click Next. In the Generate Ant Buildfilesscreen:
    • Check the project in list.
    • Uncheck the option "Create target to compile project using Eclipse compiler" because we want to create a build file which is independent of Eclipse.
    • Leave the Name for Ant buildfile as default: build.xml
      Generate Ant Buildfiles dialog
  • Click Finish, Eclipse will generate the build.xmlfile under project’s directory as follows:ant buildfile in project directory

Double click on the build.xml file to open its content in Ant editor:

content of Ant buildfile

We can see that Eclipse generated a complete Ant build file with all libraries referenced by the project and standard build targets such as init, clean, build, etc. However, Eclipse did not generate a target to create WAR file for this web application, so we have to add it manually. Add the following code snippet at the end of the build.xml file, just above the closing tag </project>: 

<target name="war" description="Bundles the application as a WAR file" depends="clean, build">
	<mkdir dir="WebContent/WEB-INF/classes"/>
	
	<copy includeemptydirs="false" todir="WebContent/WEB-INF/classes">
    		<fileset dir="build/classes">
			<include name="**/*.class"/>
    		</fileset>
	</copy>
	
	<war destfile="UploadServlet30.war"
		basedir="WebContent"
		needxmlfile="false">
	</war>
</target>
 



Save the file. Now switch to a command line prompt, change current directory to the project’s directory, and type the following command to compile and build WAR file for the project:

ant war

Output:

output of running Ant build file

NOTE: You must have Ant installed and configured properly to execute the above command. This Ant installation is separate from the one bundled with Eclipse.

 

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.



Add comment

   


Comments 

#17GEO2021-04-26 06:35
tHANKS. gOT UR PAGE AFTER LOT OF SEARCH
Quote
#16Isuru2020-06-18 22:43
Thanks for the article. It was so useful. I have a question on one thing: if I need to use an email target how can I use it with the help of this build.xml?

Including an email target didn't do the job for me.

E.g.
Quote
#15Nam2019-10-30 23:10
Thank you very much for this great article
Quote
#14Kalaiselvi D2016-12-03 01:03
I'm using Windows 10 , when I entered "ant war" in cmd prompt it throws me an error -

operable program or batch file.
'operable' is not recoginized as an internal or external command, operable program or batch file
Quote
#13Kalaiselvi D2016-12-03 00:46
Getting an 'ant' is not recognized as an internal or external command - Error

Does anyone help on this above issue facing in my cmd prompt
Quote