Monday, October 13, 2014

Apache Ant Tutorial:

In this tutorial you will learn about Apache Ant, Ant is a useful tools for compile and build your java program, most of IDE supports Ant either by opening Ant project or use Ant as it's build and compile code. This tutorial is very basic which let you get start using Ant, through this tutorial you will learn What is Ant, Why should you learn it, How to install it, How to create your fist Ant project, and How to open it by IDE.

What is Apache Ant?

Apache Ant is a java and command lines tools aim to build your java code with libraries and associated files as independent of any IDE. You can download Ant from Apache Ant website http://ant.apache.org/

Why should I learn Ant?

Programmers write their java code, usually with IDE "Integrated Development Environment" such as, NetBeans, Eclipse, or InteIIiJ IDEA.  Imagine a numbers of programmer would like to build a project together and use a git repository to push their changes. Supposing three programers, each prefers a different IDE. Ant isolate the code from the IDE,  and when programers push their changes all files associate with IDE will not be included in the git repository.



In short programers learn Ant for the following reasons:
  1. Not everyone in your team like the same IDE, somebody might prefers a different IDE.
  2. Settings might vary from one IDE to another.
  3. Build your target Jar with it's dependencies automatically
  4. You can automate the complete deployment : clean, build,  jar,  generate the documentation, copy to some directory, tune some properties depending on the environment, .... etc.
  5. Ant build and compile dependencies between targets.

Install Ant in your machine ?

1- Download ant from Apache Ant website  http://ant.apache.org/
2- Follow the instruction in http://ant.apache.org/manual/index.html
3- Don't worry if the instruction is confusing you. You can simply do the following steps 
4- copy ant to your /usr/bin/ directory 
5- copy ant libraries into /user/lib/ directory

How to build my program using Ant? 

Every project contains the following basic folders:
1- src - This folder contains all your package and source code
2: lib - contains all the externals libraries you used in your code




Now you need to create a build.xml file that contains instruction of your ant.  Here is a basic template I use for most of my project, you might need to manipulate it a little bit by changing the project name, software, and main class. I colored those properties in blue.

=================   build.xml  ====================
 
<?xml version="1.0"?>
<project name="ProjectName" default="main" basedir=".">
    <!-- Sets variables which can later be used. -->
    <!-- The value of a property is accessed via ${} -->
    <property name="src.dir" location="src" />
    <property name="build.dir" location="bin" />
    <property name="dist.dir" location="dist" />
    <property name="docs.dir" location="docs" />
    <property name="lib.dir" location="lib" />
    <property name="software" value="JavaSfotwre" />
    <property name="version" value="0" />
    <property name="mainClass" value="package.org.Main" />
   
    <!-- Create a classpath container which can be later used in the ant task -->
    <path id="build.classpath">
        <fileset dir="${lib.dir}">
            <include name="*.jar" />
        </fileset>
    </path>
   
    <!-- Deletes the existing build, docs and dist directory-->
    <target name="clean">
        <delete dir="${build.dir}" />
        <delete dir="${docs.dir}" />
        <delete dir="${dist.dir}" />
    </target>

    <!-- Creates the  build, docs and dist directory-->
    <target name="makedir">
        <mkdir dir="${build.dir}" />
        <mkdir dir="${docs.dir}" />
        <mkdir dir="${dist.dir}" />
    </target>

    <!-- Compiles the java code (including the usage of library for JUnit -->
    <target name="compile" depends="clean, makedir">
        <javac srcdir="${src.dir}" destdir="${build.dir}" classpathref="build.classpath">
        </javac>

    </target>

    <!-- Creates Javadoc -->
    <target name="docs" depends="compile">
        <javadoc packagenames="src" sourcepath="${src.dir}" destdir="${docs.dir}">
            <!-- Define which files / directory should get included, we include all -->
            <fileset dir="${src.dir}">
                <include name="**" />
            </fileset>
        </javadoc>
    </target>
   
    <!--Creates the deployable jar file -->
    <target name="jar" depends="compile" description="Create one big jarfile.">
        <jar jarfile="${dist.dir}/${software}-${version}.jar">
            <zipgroupfileset dir="${lib.dir}">
                <include name="**/*.jar" />
            </zipgroupfileset>
        </jar>
        <sleep seconds="1" />
        <jar jarfile="${dist.dir}/${software}-${version}-withlib.jar" basedir="${build.dir}" >
                <zipfileset src="${dist.dir}/${software}-${version}.jar" excludes="META-INF/*.SF" />
                <manifest>
                    <attribute name="Main-Class" value="${mainClass}" />
            </manifest>
        </jar>
    </target>

    <target name="main" depends="compile, jar">
                <description>Main target</description>
        </target>

</project>

===========================================

 

Compile and Build with Ant

Now open the terminal and type the following under the project folder 
$ cd <dir>/Myproject
$ ant 

You should see this in terminal otherwise there is something wrong in your code. 
BUILD SUCCESSFUL
Total time: 1 secon

 

  Open Ant project using eclipse IDE?

1-Open New project
2- Select Java Project from Existing Ant Buildfile
3- Select build.xml file