27 December 2010

ant: Java application build tool

Apache Ant is a Java library and command-line tool who's mission is to drive processes described in build files as targets and extension points dependent upon each other. The main known usage of Ant is the build of Java applications. Ant supplies a number of built-in tasks allowing to compile, assemble, test and run Java applications.

Installing ant:
  1. Download the binaries from http://jakarta.apache.org/ant/index.html, unzip them to a suitable directory.

  2. Append /path/to/ant/bin to the PATH environment variable.

  3. Append the .jar files in /path/to/ant/lib/ to the CLASSPATH environment variable. Set JAVA_HOME to point to the location of the JDK installation on the machine that the software is being installed on. Append /path/to/jdk/lib/* to the CLASSPATH environment variable.

Writing a sample build script

Sample Directory Structure:
`Application
|--- bin = Compiled byte code directory all the compiled file will be placed here
|--- src = Java application source files
|--- lib = External library files (eg. jar files )
|--- build.xml = build description file

Sample build.xml
<project name="Project Name" default="test" basedir=".">
<description> Project description </description>
<target name="clean">
<delete dir="bin"/>
</target>

<!-- Create the build directory structure used by compile -->
<target name="init" depends="clean">
<!-- Create the time stamp -->
<tstamp/>
<mkdir dir="bin"/>
</target>

<path id="compile.classpath">
<fileset dir="lib/">
<include name="**/*.jar"/>
</fileset>
<pathelement path ="bin/;${classpath}"/>
</path>

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

<target name="test" depends="compile" description="Execute class">
<java classname="JavaTestClassName" fork="true" failonerror="true" >
<classpath refid="compile.classpath"/>
</java>
</target>
</project>

No comments:

Post a Comment