Ant – How to create a Java Project

In this tutorial, we will show you how to use Ant build tool to manage a Java project, compile, and package it into a Jar file.

Technologies used :

  1. Eclipse 4.2
  2. Ant 1.9.4
  3. JDK 1.7

1. Create a Java Project

In Eclipse IDE, create a new Java project named “AntDateUtils”.

ant-java-project

2. Java Source Code

Create a new Java class to print out the current date :

src/com/mkyong/core/utils/DateUtils.java

package com.mkyong.core.utils;

import java.util.Date;

public class DateUtils {

	public static void main(String[] args) {

		System.out.println(getLocalCurrentDate());
		
	}

	private static Date getLocalCurrentDate() {
		return new Date();		
	}

}

3. build.xml

Create a new build.xml in the project root folder, read comment for self-explanatory.

build.xml

<project name="AntJavaProject" default="main" basedir=".">
	<description>
		Create a Java Project (JAR) with Ant build script
	</description>

	<property name="projectName" value="DateUtils" />
	
	<!-- Java sources -->
	<property name="src.dir" location="src" />
	
	<!-- Java classes -->
	<property name="build.dir" location="bin" />
	
	<!-- Output, Jar -->
	<property name="dist.dir" location="dist" />

	<target name="init">
		<!-- Create the time stamp -->
		<tstamp />
		<!-- Create the build directory structure used by compile -->
		<mkdir dir="${build.dir}" />
	</target>

	<target name="compile" depends="init" description="compile the source ">
		<!-- Compile the java code from ${src.dir} into ${build.dir} -->
		<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" />
	</target>

	<target name="dist" depends="compile" description="package, output to JAR">

		<!-- Create the distribution directory -->
		<mkdir dir="${dist.dir}" />

		<!-- Put everything in ${build} into the {$projectName}-${DSTAMP}.jar file -->
		<jar jarfile="${dist.dir}/${projectName}-${DSTAMP}.jar" basedir="${build.dir}" >
		    <manifest>
			<!-- create an executable Jar -->
			<attribute name="Main-Class" value="com.mkyong.core.utils.DateUtils" />
		    </manifest>
		</jar>
	</target>

	<target name="clean" description="clean up">
		<delete dir="${build.dir}" />
		<delete dir="${dist.dir}" />
	</target>
	
	<!-- Default, run this -->
	<target name="main" depends="clean, compile, dist" />
	
</project>

4. Ant Build Scripts

Done, try few Ant’s commands

4.1 Compile the source code


$ ant compile
build.xml

<target name="compile" depends="init" description="compile the source ">
	<javac includeantruntime="false" srcdir="${src.dir}" destdir="${build.dir}" />
</target>

4.2 Package the project into an executable Jar file


$ ant dist
build.xml

<target name="dist" depends="compile" description="package, output to JAR">
	<mkdir dir="${dist.dir}" />
	<jar jarfile="${dist.dir}/${projectName}-${DSTAMP}.jar" basedir="${build.dir}">
	  <manifest>
		<attribute name="Main-Class" value="com.mkyong.core.utils.DateUtils" />
	  </manifest>
	</jar>
</target>

4.3 Delete folders


$ ant clean
build.xml

<target name="clean" description="clean up">
	<delete dir="${build.dir}" />
	<delete dir="${dist.dir}" />
</target>

4.4 If no options, the default target will be executed, in this example, the default target is main

build.xml

<project name="AntJavaProject" default="main" basedir=".">
	...
	<target name="main" depends="clean, compile, dist" />

$ ant

Output


Buildfile: /Users/mkyong/Documents/workspace/AntDateUtils/build.xml
clean:
   [delete] Deleting directory /Users/mkyong/Documents/workspace/AntDateUtils/bin
   [delete] Deleting directory /Users/mkyong/Documents/workspace/AntDateUtils/dist
init:
    [mkdir] Created dir: /Users/mkyong/Documents/workspace/AntDateUtils/bin
compile:
    [javac] Compiling 1 source file to /Users/mkyong/Documents/workspace/AntDateUtils/bin
dist:
    [mkdir] Created dir: /Users/mkyong/Documents/workspace/AntDateUtils/dist
      [jar] Building jar: /Users/mkyong/Documents/workspace/AntDateUtils/dist/DateUtils-20141030.jar
main:
BUILD SUCCESSFUL
Total time: 1 second

Final Directory Structure

ant-java-project-final

5. Test

5.1 Run a class inside a Jar file.


$ pwd
/Users/mkyong/Documents/workspace/AntDateUtils

$ java -cp dist/DateUtils-20141030.jar com.mkyong.core.utils.DateUtils
Thu Oct 30 17:39:21 MYT 2014

5.2 Run the executable Jar file


$ pwd
/Users/mkyong/Documents/workspace/AntDateUtils

$ java -jar dist/DateUtils-20141030.jar
Thu Oct 30 17:40:21 MYT 2014

Download Source Code

Download It – AntDateUtils.zip (6 KB)

References

  1. Apache Ant Hello World Official Guide

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Rajendra
Guest
Rajendra

It is possible to create a jar using specific jdk by passing parameters?
As per above command it will used default jdk but I do not want to used a default one but of my choice. It is achievable.

Vaibhav Reddy Guddeti
Guest
Vaibhav Reddy Guddeti

why do we need to keep depends=”compile” in the target here . We are already doing a compile before going to dist…?

beginnerstutorialexamples
Guest
beginnerstutorialexamples

Nicely explained tutorial.

You can also find more java ant tutorial with example on beginnerstutorialexamples.com .

http://www.beginnerstutorialexamples.com/ant-tutorial-for-beginners-with-examples/

trackback
Ant – How To Create A Jar File with external libraries

[…] Previous Ant Java project will be […]

Halil
Guest
Halil

Thank you for sharing…

anon
Guest
anon

Good info. Thanks