Ant – Java Development Automation

In java programming, we knew ANT(Another Neat Tool). Ant is a tool in java programming where compile, deploy, and packaging matter, become automatic. Ant known as an “easy-to-use XML based syntax”. Just like Java, Ant also can run in cross platform. In Ant we also familiar with a file called build.xml. This file is central file of all automate process of a Java Development.

Practically, ant used by java developer as a tool to compile, package, and deploy after the source code have been made. To use ant you can download it here.

This time we will discuss about using ant as a tool to automate process java development after source code have been made. Let’s see a simple example of a build.xml file that ant can executing.

<project name="Blog" default="compile" >
  <target name="prepare">
    <mkdir dir="G:\Pribadi\MyProgramming\Java\JavaProject\Blog\output"/>
</target>
<target name="compile"  depends="prepared">
      <javac srcdir="G:\Pribadi\MyProgramming\Java\JavaProject\Blog\src"
                  destdir="G:\Pribadi\MyProgramming\Java\JavaProject\Blog\output"/>
  </target>
</project>

This code is a simple example of a build.xml file. From those lines we can see that there’s two task that is ant can call which is : prepare and compile. On prepare task, we see that there’s a directory that need to create with the mkdir command, where this directory is have to be located on G:\Pribadi\MyProgramming\Java\JavaProject\Blog path and the name of the directory is output. While in compile task, we can see that this task is depends on the previous task which is prepare task, mean if prepare task is failed to execute, and then this compile task wouldn’t never be executed. In the compile task, we also can see that the main function of the task is compiling all source code that located on G:\Pribadi\MyProgramming\Java\JavaProject\Blog\src.

Ant is also known property. In the build.xml file, property noted with the property syntax. Property is used in this build.xml file, as a variable of a value that might be taking space that too long, such as a path. On the lines above, we can change the directory name into a property, then the build.xml file will be like this:

<project name="Blog" default="compile" basedir=".">
<!--Define Params-->
<property name="src" value="${basedir}/src"/>
<property name="output" value="${basedir}/output"/>

<target name="prepare">
    <mkdir dir="${output}"/>
</target>

<target name="compile" depends="prepare">
    <javac srcdir="${src}" destdir="${output}"/>
</target>

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

On those lines, we can see that we can simplify the directory by adding property lines on the header of the syntax, and then on the prepare task and the compile task, we call those properties and turn it into a variable which has a directory path as a value. On those lines we also add a new task that can be executed by ant, which is clean task. This task is used to delete the directory that declared.

In the distribute issue of our hand made application which we have made with Java Programming Language, it’s highly suggested, that we have to pack our application on a jar file format. Especially if our hand made application contains a lot of classes. With this jar file, the class files are easier to maintain, because it’s only on a single jar file. Actually jar file is a zip file of java, even the class is on a package, but the classes inside of this jar file can be executed. When we execute a jar file that contains a lot of classes, we have to make an information on that jar file. This Information will tell the executable command, which is the main class of the jar file. This information is put on a file called manifest.mf on META-INF folder. In this manifest file also wrote some information of the application dependency to some libraries that we might be use on our application.

Please see the syntax of this build.xml file,below:

<project name="Blog" default="compile" basedir=".">
 <!--Define Params-->
 <property name="src" value="${basedir}/src"/>
 <property name="output" value="${basedir}/output"/>
 <property name="lib" value="${basedir}/lib"/>
 <property name="dist" value="${basedir}/dist"/>

 <target name="prepare">
     <mkdir dir="${output}"/>
 </target>

 <target name="make_jar” depends="compile”>
        <jar jarfile="${dist}/test_blog.jar">
               <fileset dir="${build}">
     <include name="**"/>
     </fileset>
                <manifest>
          <attribute name="Class-Path" value="lib/log4j-1.2.8.jar lib/commons-httpclient-2.0-rc2.jar  lib/jodd.jar ."/>
                </manifest>
    </jar>
</target>

 <target name="compile" depends="prepare">
     <javac srcdir="${src}" destdir="${output}">
 <classpath>
     <pathelement location="${lib}/ log4j-1.2.8.jar "/>
                          <pathelement location="${lib}/commons-httpclient-2.0-rc2.jar"/>
     <pathelement location="${lib}/jodd.jar"/>
           </classpath>
    </javac>
</target>

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

On the above code we can see there are a few things that we added. The first adding is seen on the property lines. We added new property here that represent lib directory. The next adding is wrote on compile task code. We can add the some lines in this context if our hand made source code is also used other libraries. Lines added on this compile task, is used to import class-path of libraries when the compiling of the source code is run on the compile task.

In above code, we also add a new task which is make_jar task, the function of this task is creating a java archive file called test_blog.jar. This jar file contains all the class files that produced by a compile task. When we want to add files on a jar file we can use the fileset tag on that make_jar task. We also able to restrict the file that we will add to jar file by changing the value of include name tag and then we fill the value only with the list of files that we need. Remarks: include name tag is allowed to create more that one lines but the tag have to be inside the fileset tag.

And then we also could see in the make_jar task, there’s a line added which is manifest tag, this is the tag that write an information on a jar file. All of these information will we written on a file called manifest.mf and put it on a single folder that located on the distributed jar file.

That’s all, little discuss about the syntax on a file called build.xml, that can be executed by the Antoher Neat Tool (Ant). There’s still a lot of tags that we can use, so please try it with your own creativity, you can read the API of the ant it self for some references.

I hope this usefull, thanks

 

Menteng, Nov 30, 2008

josescalia 

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s