Sunday, January 17, 2010

Importance of a Build Utility-2

In this part, I'll be covering the various aspects of building an application in the java platform. I am specific about this because of a large developer base on this platform (It has more than 20 Million developers currently).

In this platform too, building applications is not a breeze as you need to do a lot of things. All this requires a lot of forethought and a nice build routine to smooth things as we go on. Think of a build utility as an investment that gives a rich dividend once your application increases in size and complexity.

By and large, there is a single build utility in java, known as ant [ Another Neat Tool ] ( This is an open-source xml based building utility that frees you from the burden of performing complicated and repetitive steps. It is also the build utility involved behind the scenes in IDEs like Netbeans and Eclipse. It is quite easy to learn and use, as it doesn’t requires more than one day to learn this. Apart from the quintessential hello world application types, it helps you a lot in your application automation.

Note that the root tag is the project and other activities are targets. Inside each target, are the actual works to be performed in the form of tasks. There are various tasks inbuilt in ant distribution as well as the third-party tasks are there, downloadable from the internet. Also you can create your own custom tasks (although I've never seen many of them).

Example (build.xml):

< project name="Custom" basedir="." default="run">

< property name="src" value="src" />
< property name="build" value="build"/>
< property name="dist" value="dist" />
< property name="jarfile" value="${dist}/Custom.jar" />

< target name="init">
< delete dir="${build}"/>
< delete dir="${dist}"/>
< mkdir dir="${build}" />
< mkdir dir="${dist}" />
< /target>

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

< manifest file="">
< attribute name="Main-class" value="Test" />
< /manifest>
< jar destfile="${jarfile}" basedir="${build}"
manifest="" />
< /target>
< target name="run" depends="package">
< java jar="${jarfile}" fork="true"/>
< /target>
< /project>

Assumptions :
jdk1.2+ is installed
ant is installed (in the same manner as your jdk)
The application is a JavaSE application that has a main class as Test

Here, on typing ant on the current directory, the ant would delete the build and dist directories, then compile all the source files. After this, it packages the application in a jar file and lastly runs it to test our application.
In Netbeans, you can view the ant file created in your project directory as nbproject/build-impl.xml and as notified, you can add your own customizations in a child build file to alter your project build routines as the needs arise.

Ant is quite flexible and you can perform almost all actions that you require. This article just scratched the surface of the build utility, you can search for more comprehensive tutorials as well as different tasks online.

In large projects, even ant has quite a few shortcomings, which is further automated by newer technologies like Maven. I'll be covering out that in my next article.

No comments: