Wednesday, February 22, 2012

How to automate project builds through apache Ant

ANT (Yet Another Tool) is a simple build by Apache. It is used for automating build and releases software projects. I'm going to show how quickly you can can get started with Ant and automate the build processes in Windows platform.
  • Download the latest stable version of Ant from Apache download page and place it in your local file system, for example D:\Apache-ant-1.8.2.
  • Set environment variable ANT_HOME to the path where you placed the Ant tool
  • Set PATH environment variable to point to %ANT_HOME%\bin. Verify if the path is set by typing 'ant' in your command prompt. If you see the message Buildfile: build.xml does not exist! Build failed, the ant is installed correctly. Ant by default searches for a file called build.xml, that's the reason you see above message.
  • Create a build.xml (you can name it the way you want like ant-build.xml etc, if you do so, you need to tell ant what is the build xml file name while running the ant targets) in the same place as that of your source folder under your java project. A very basic build.xml looks as shown below.                               
    <project name="MyJavaProject" default="dist" basedir=".">
      <!-- set global properties for this build -->
      <property name="src" location="src"/>
      <property name="build" location="build"/>
      <property name="dist"  location="dist"/>
      <!-- root directory for the example source code: -->
      <property name="src.dir" value="${basedir}/src" />
      <target name="init">
        <!-- Create the time stamp -->
        <tstamp/>
        <!-- Create the build directory structure used by compile -->
        <mkdir dir="${build}"/>
      </target>
      <target name="compile" depends="init"
            description="compile the source " >
        <!-- Compile the java code from ${src} into ${build} -->
        <javac srcdir="${src}" destdir="${build}"/>
      </target>
      <target name="dist" depends="compile"
            description="generate the distribution" >
        <!-- Create the output jar having compiled classes directory -->
        <mkdir dir="${dist}/lib"/>
        <!-- Put everything in ${build} into the MyJavaProject-${DSTAMP}.jar file -->
        <jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
      </target>
      <target name="clean"
            description="clean up" >
        <!-- Delete the ${build} and ${dist} directory trees -->
        <delete dir="${build}"/>
        <delete dir="${dist}"/>
      </target>
    </project>

  •  From the run menu, open the command prompt, navigate to your project directory and type >ant compile, you will see following output.  
    D:\WorkSpace\MyJavaProject>ant compile
    Buildfile: build.xml
    init:
    compile:
        [javac] Compiling 1 source file to D:\SpyderWorkSpace\SimpleJavaProject\build
    BUILD SUCCESSFUL
    Total time: 2 seconds
      
  • If you use different name than build.xml for example ant-build.xml, the syntax of running ant target will be >ant -f ant-build.xml compile.
  • You can also define the properties in a separate file called build.properties and include that in your build.xml.
Ant target here just compiles your source, execute 'dist' target to create your distribution file, the compiled binary jar file. This way for all your project modules ant can be used. To put simple, anything that you do on command prompt can be done in Ant. Take a look at Apache Ant Documentation to learn Ant in full. If you are wanting to automate the software release process, i recommend Maven tool.

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...