Getting started with Ant in JAVA, The Apache ANT Project

ANT

the apache ant project logo Getting started with Ant in JAVA, The Apache ANT Project

The main purpose of this article is to show Ant Java developers who do not yet know this tool. To reveal the main contributions of Ant, we will compare the creation of an online project of classical control with the creation of same project with Ant.

  • Introduction
  • Building the project from command line
    • Compilation
    • Creating executable jar file
    • Project Execution
  • Building the project with Ant
    • The target clean
    • The target compile
    • The target jar
    • Project Execution
    • Dependencies between targets
    • Summary
    • Improved build.xml
  • Comparison build.xml and the command line
  • More Tool
    • Maven
  • Links
  • Download sources
  • Conclusion
  • Acknowledgments

1. Introduction

Ant is an open source project of the Apache foundation written in Java that aims to develop software to automate repetitive operations throughout the software development cycle. It is downloadable at

http://ant.apache.org.

Ant could be compared to the famous Unix tool make. It was developed to provide a tool for building independent of any platform. This is particularly useful for projects developed by and for multiple systems or to migrate projects from one system to another. It is also very effective for small developments.

Ant is an XML configuration file that describes the various tasks to be performed by the tool. Ant provides a number of common tasks that are coded as Java classes. These tasks are independent of the system on which they are executed. Moreover, it is possible to add these specific tasks by writing

javac -sourcepath src -d bin\ src\org\sdf\HelloWorld.java

new Java classes within certain

specifications.

Ant’s popularity is increasing day by day. Its flexibility, its power and simplicity have made it one of the most popular in the world of Java.

The integrated development environments often offer a tool for building owner who’s generally less flexible and less powerful than Ant. So plug-ins have been developed for the majority of them (JBuilder, Forte, Visual Age …) to enable them to use Ant, become a de facto standard.

The Netbeans IDE Ant incorporated since version 4.0, it provides a build.xml for all its projects.

for download the latest version

The only IDE you need

download frontpage Getting started with Ant in JAVA, The Apache ANT Project


2 Building the project from command line

In this tutorial, we want to separate source files compiled, so our source files (. Java) will be placed in the src directory.

All files (. Class) generated must be placed in the bin directory and then the jar file of our project will go into the exe directory.

We used the syntax of the command line of windows. Linux is the md and mkdir replaced by the backslash (\) by a single slash (/). Other commands should all work without problem.

We will have the following directory structure:

ant1 Getting started with Ant in JAVA, The Apache ANT Project

Compilation

It creates only the src. Here is the command to:

md src

The following java class displays “Hello world” to standard output

Helloworld.java

public class HelloWorld {

public static void main(String[] args) {
System.out.println(" Hello   World ");
}
}

You must put this code in a file named Helloworld.java. This file must be in the src / org / sdf. We will have this:

ant2 Getting started with Ant in JAVA, The Apache ANT Project

Now we will try to compile and run our project.

md bin

javac -sourcepath src -d bin\ src\org\sdf\HelloWorld.java

java -cp bin org.sdf.HelloWorld

Which will result in this:

Hello World.

After compiling the source file, you will have the following tree:

ant3 Getting started with Ant in JAVA, The Apache ANT Project

Creating executable jar file

Creating the jar file is not very difficult. But make it executable requires one more step:

1. Creating a manifest file containing the name of the main class.
2. Creating directory exe.
3. Creating the jar file.

All this can be done in command line as follows:

echo Main-Class: org.sdf.HelloWorld>MonManifest md exe jar cfm exe\HelloWorld.jar MonManifest -C bin .

After executing these three lines, we have the following file tree:

ant4 Getting started with Ant in JAVA, The Apache ANT Project

Project Execution

We will run our project with the command line:

java -jar exe\HelloWorld.jar

3. Building the project with Ant

We will now try to create a build.xml Ant. For our project, build.xml contains 4 targets: clean, compile, jar and run.

The target clean

This entry removes all files generated during compilation and the file was created. In general, It is a good habit to have a clean target.

[sourcecode language='xml]

< target name = ” clean ” >

< delete dir = ” bin ” / >

< delete dir = ” exe ” / >

< / target >

[/sourcecode]]

Our target clean is very simple, it will call the delete task twice, once to remove the class files generated, and another to remove the jar file.

The target compile

This entry creates a directory named bin and then compile the source files from src and put the files generated in the bin directory.

[sourcecode language='xml]

< target name = ” compile ” >

< mkdir dir = ” bin ” / >

< javac srcdir = ” src ” destdir = ” bin ” / >

< / target >

[/sourcecode]

The creation of the bin directory is done with the mkdir task. The compilation is the javac task simply.

The target jar

This entry creates a directory called exe and create the executable jar file of our project in that directory.

[sourcecode language='xml]

< target name = ” jar ” >

< mkdir dir = ” exe ” / >

< jar destfile = ” exe/HelloWorld.jar ” basedir = ” bin ” >

< manifest >

< attribute name = ” Main-Class ” value = ” org.sdf.HelloWorld ” / >

< / manifest >

< / jar >

< / target >

[/sourcecode]

Creating directory exe is done with the mkdir task. Creating the jar file is executable with the task jar. The manifest file is created very easily with the manifest element.

Project Execution

This entry allows you to run the executable jar file generated during the previous step.

[sourcecode language='xml]

< target name = ” run ” >

< java jar = ” exe/HelloWorld.jar ” fork = ” true ” / >

< / target >

[/sourcecode]

The task can run java jar file. Fork = “true” allows you to run the project in a new Java Virtual Machine.

Dependencies between targets

The execution of a target (target) may depend on the performance of other targets. In our example, the target depends on jar target compiles. There is no logic to generate a jar file without the source being compiled.

Ant provides a mechanism allowing length to order specific performance targets of build.xml. This mechanism is implemented with attribute depends.

Example:

[sourcecode language='xml]

< target name = ” jar ” depends = ” compile ” >

< mkdir dir = ” exe ” / >

< jar destfile = ” exe/HelloWorld.jar ” basedir = ” bin ” >

< manifest >

< attribute name = ” Main-Class ” value = ” org.sdf.HelloWorld ” / >

< / manifest >

< / jar >

< / target >

[/sourcecode]

The implementation of this target requires the execution of the compile target. Ant take care of everything. If the compile target depends also another target, so Ant will enforce dependencies recursively.

Summary

After writing the main target of our project, we will now build our build.xml which will be used by Ant to perform different tasks. The build.xml file should have as a root element called project. This element may have the following attributes:

Attribute
Description
Compulsory

name
The project name
no

default
the default target to use when no target is specified no

basedir
the directory of the project
no


< project   default = " run " >

< target   name = " clean " >

< delete   dir = " bin " / >

< delete   dir = " exe " / >

< / target >

< target   name = " compile "   depends = " clean " >

< mkdir   dir = " bin " / >

< javac   srcdir = " src "   destdir = " bin " / >

< / target >

< target   name = " jar "   depends = " compile " >

< mkdir   dir = " exe " / >

< jar   destfile = " exe/HelloWorld.jar "   basedir = " bin " >

< manifest >

< attribute   name = " Main-Class "   value = " org.sdf.HelloWorld " / >

< / manifest >

< / jar >

< / target >

< target   name = " run "   depends = " jar " >

< java   jar = " exe/HelloWorld.jar "   fork = " true " / >

< / target >

< / project >

Now we can compile, store and execute our project with the commands:

ant clean

ant compile

ant jar

ant run

And since run depends jar that depends on who compiles depends on clean, can be done simply:

ant run

Or better yet, since the default target of the project is run:
ant

Improved build.xml

Now that we have our build.xml file, we can do some optimizations. We note that used several times the same directory names. A problem would arise if we wanted to change the directory where generated files will be compiled: should change all occurrences of bin in the file.

To circumvent this problem, Ant provides the mechanism properties. This is to declare properties that have values, and retrieve its value anywhere in our build.xml file.

Moreover, ANT provides us the opportunity to keep track of the performance of build.xml by using the echo task. This can be very useful in some cases.

build.xml

[sourcecode language='xml]

< project default = ” run ” >

< property name = ” src.dir ” value = ” src ” / >

< property name = ” bin.dir ” value = ” bin ” / >

< property name = ” jar.dir ” value = ” exe ” / >

< property name = ” main-class ” value = ” org.sdf.HelloWorld ” / >

< target name = ” clean ” >

< delete dir = ” ${bin.dir} ” / >

< delete dir = ” ${jar.dir} ” / >

< echo message = ” nettoyage termine ” / >

< / target >

< target name = ” compile ” depends = ” clean ” >

< mkdir dir = ” ${bin.dir} ” / >

< javac srcdir = ” ${src.dir} ” destdir = ” ${bin.dir} ” / >

< echo message = ” compilation terminee ” / >

< / target >

< target name = ” jar ” depends = ” compile ” >

< mkdir dir = ” ${jar.dir} ” / >

< jar destfile = ” ${jar.dir}/sdf.jar ” basedir = ” ${bin.dir} ” >

< manifest >

< attribute name = ” Main-Class ” value = ” ${main-class} ” / >

< / manifest >

< / jar >

< echo message = ” Creation du fichier Jar terminee ” / >

< / target >

< target name = ” run ” depends = ” jar ” >

< java jar = ” ${jar.dir}/sdf.jar ” fork = ” true ” / >

< / target >

< / project >

[/sourcecode]

Now, if we want to change the directory where are placed the compiled files, simply change the value of the property bin.dir.

4. Comparison build.xml and the command line

We tried to establish a comparison table between the compilation, storage and execution command line and Ant. This is summarized in the following table:

Command Line
build.xml

[sourcecode language='xml]

® bin
javac-sourcepath src-d bin \ src \ org \ sdf \ Helloworld.java <target name=”compile”>
<mkdir dir=”bin”/>
<javac srcdir=”src” destdir=”bin”/> </ target>
echo Main-Class: org.sdf.HelloWorld> MonManifest
md exe
jar cfm exe \ HelloWorld.jar MonManifest-C bin. <target name=”jar”>
<mkdir dir=”exe”/>
<jar destfile=”exe/HelloWorld.jar” basedir=”bin”>
<manifest>
<attribute name=”Main-Class” value=”org.sdf.HelloWorld”/>
</ manifest>
</ jar>
</ target>

java-jar exe \ HelloWorld.jar
<target name=”run”>
<java jar=”exe/HelloWorld.jar” fork=”true”/>
</ target>

[/sourcecode]

5. More Tools

Maven

Apache Maven is a free software tool for management and production automation projects Java. The goal is comparable to the Unix Make: produce a software from source, optimizing the tasks performed to this end and ensure the proper production order.

It is similar to the Ant tool, but provides means for simple configuration, also based on XML. Maven is managed by the Apache Software Foundation. Maven was previously a branch of the Jakarta Project.

6. Conclusion

By structuring an XML file, Ant provides greater visibility of operations can be executed on a project. The main advantage is that you must perform more routine tasks (boring!) Compilation, archiving and performance every time you change one line in a project source: Ant take care of do all this for us ..

Tags: , , , , , ,

About eadoking

Ead AlDoukanje - EADOKING - 3edoOo ( عيد الدكنجي Studied Informatics Engineering at Tishreen University ( Software Engineering Department ) Work as freelancer Web Developer / Software Developer You can contact me to me@eadoking.com mobile : ( +963 ) 0932728084 Programming Skills : Java, C# , C++ , Java Server Pages (JSP ) with SERVLETS, PHP ,HTML , CSS ,JAVA SCRIPT with Ajax , ASP.NET , Web Services .

Stay in touch

subscribe with us using social media or email to stay updated
you can also contact us if you need some request or any thing to ,
http://me.eadoking.com/contact-me

Trackbacks/Pingbacks

  1. Ead AlDoukanje -3edo - January 18, 2010

    RT @Eadoking: Getting started with Ant in JAVA, The Apache ANT Project http://bit.ly/7BU3XZ

Leave a Reply


*