DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone >

Ant Installation Tutorial

Meyyappan Muthuraman user avatar by
Meyyappan Muthuraman
·
Jun. 13, 12 · · Tutorial
Like (0)
Save
Tweet
14.41K Views

Join the DZone community and get the full member experience.

Join For Free

ant is free and open source build tool, written in java, helps in automating the entire build process of a java development project.

  • ant uses xml build files.
  • by default, ant looks for a build file named build.xml.
  • the build file contains information about how to build a particular project.
  • each project contains multiple targets like creating directory, compiling source codes.
  • target can depend on other targets.
  • targets contain tasks.
  • behind each task is a java class that performs the described work.

to install ant follow the steps given below.

  • download the latest ant distribution.
  • extract it (my location is e:\apache-ant-1.7.1 )
  • set the following system variables
  • ant_home =e:\apache-ant-1.7.1
  • path = %ant_home%\bin

to make sure the installation is proper, go to command prompt and execute the command "ant - version", you will see the installed ant version.

in this example you will see how to compile a java program and compress it into a .jar file using ant build file. the following listing shows the build.xml file.

<?xml version="1.0" ?> 
<project name="hello world" default="compress">

	<target name="compile">
		<javac srcdir="."/>
		<echo> compilation complete! </echo>
	</target>
	
	<target name="compress" depends="compile">
        <jar destfile="helloworld.jar" basedir="." includes="*.class" />
        <echo> building .jar file complete! </echo>
	</target>

</project>
  • the <project> element is the root element in ant build files. the name attribute of the project element indicates the project name. each project element can contain multiple <target> elements.
  • a target represents a single stage in the build process. a build process can have multiple targets. here we have two targets compile and compress .
  • the default attribute of project element indicates the default target to be executed. here the default target is compress .
  • when you see the compress target, it in turn depends on the compile target, that is indicated by the depends attribute. so the compile target will be executed first.
  • the compile target has two task elements <javac> and <echo> . the javac task is used to compile the java files. the attribute srcdir="." indicates all the java files in the current directory. echo task is used to display message on the console.
  • the compress target also performs two tasks, first the <jar> element as the name indicates, is used to build the jar file. the attributes destfile="helloworld.jar" , basedir="." and includes="*.class" indicates all the .class files in the current directory should be compressed into helloworld.jar file. later the echo task is used to display the success message on the console.

to run the build.xml file, open the command prompt, go to the example directory, type the command " ant ". you will see the following information.

you can download the build file here.

build file : download
project : download

Build (game engine) Element

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Writing Beautiful, Optimized, and Better .NET Code With NDepend Static Analysis
  • SQL Database Schema: Beginner’s Guide (With Examples)
  • Version Number Anti-Patterns
  • 5 Steps to Create a Successful Agile Release Plan

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo