JavaBeat

  • Home
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Privacy

Writing simple ANT build script

July 24, 2008 by Krishna Srinivasan Leave a Comment

Apache Ant Example

This article explains how to write a very basic ANT build script. This does not explain indepth knowledge on building your project using ANT script. But, this writeup will be more useful for the beginners who haben’t writen any ANT script before. Before start writing the script, lets look into how to install the ANT in your machine.

Install Apache Ant

Download the Apache Ant from the link : Download Apache Ant

  • Unzip the files into your machine.
  • Set your system path variable to ANT’s bin directory. For example d:/projects/ant/bin”
  • Set the ANT_HOME in your system variables. ANT_HOME should point to your Ant installation directory. For example “d:/projects/ant”.
  • Set JAVA_HOME in your system environment variables.
  • Copy tools.jar file into the “JRE/lib” folder.

To test the setup open your command prompt and type as “ant”. You will see a message saying
[code]
Buildfile: build.xml does not exist!
Build failed
[/code]
Set up is done. Let’s look into our first simple example program using ANT script.

This example program shows how to create the JAR file after building all the java files.
[code lang=”xml”]
<project name="sampleProject" basedir="." default="jar">
[/code]
In the above code “sampleProject” is the project name for this prticular build. We are setting base directory as the current directory by just giving .(dot). We are telling the script to start from “jar” target name. The default attribute is used for setting the starting point for the ANT script.
We call a seperate task as target. We can define as many number of targets inside a build file. If we closely look into the build file, we have many number of targets based its operations. So, our build starts with “jar” target.
Target has depends attribute to indicate, that particular task can be executed only after the certain number of dependencies. In our case, before building the jar file, we have to compile the files. So, jar target calls compile target. That has some dependencies that is executed. Once the build is successful, test.jar will be created in the same directory.

build.xml

[code lang=”xml”]<?xml version="1.0"?>
<project name="sampleProject" basedir="." default="jar">
<property name="src" value="ant-source"/>
<property name="output" value="bin"/>

<target name="compile" depends="create">
<javac destdir="bin">
<src path="${src}"/>
<classpath refid="java"/>
</javac>
</target>

<target name="jar" depends="compile">
<jar destfile="test.jar">
<fileset dir="bin"/>
</jar>
</target>

<target name="clean">
<delete dir="${output}"/>
</target>

<target name="create" depends="clean">
<mkdir dir="${output}"/>
</target>

<path id="java">
<fileset dir="D:JarsHibernate">
<include name="*.jar"/>
</fileset>
</path>
</project>[/code]

Filed Under: Apache Ant Tagged With: ANT Basics

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Follow Us

  • Facebook
  • Pinterest

As a participant in the Amazon Services LLC Associates Program, this site may earn from qualifying purchases. We may also earn commissions on purchases from other retail websites.

JavaBeat

FEATURED TUTORIALS

Answered: Using Java to Convert Int to String

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Copyright © by JavaBeat · All rights reserved