Jump to: navigation, search

User talk:Gaurangtpatel.gmail.com

Revision as of 14:44, 3 April 2009 by Gaurangtpatel.gmail.com (Talk | contribs) (Challenges and Risks)

Proposal: Ant buildfile refactorings

Ant macros can be used to achieve re usability of scripts:

A macrodef for compiling Java code using the javac Ant task could look like this:

<macrodef name="m_compile">

  <attribute name="build.dir" default="${build.dir}" />
  <attribute name="classpath.id" default="default.classpath" />
  <attribute name="src.dir" default="${src.java.dir}" />
     <mkdir dir="@{build.dir}" />
     <javac destdir="@{build.dir}" debug="${debug}"
        <classpath refid="@{classpath.id}" />
        <src path="@{src.dir}" />



<import file="${buildfiles.dir}/macro/compile.xml" />

This statement imports the XML from the compile.xml file into the place where the import statement is.


When you have imported a macrodef into your build script using an import statement like above, the macro can be used in standard Ant targets like this:

<target name=”compile”>



That’s all, assuming that the macrodef has some valid default attributes. If you need to pass in some attributes to the macrodef which are different from the defaults, it can look like this:

<target name=”compile”>

  <m_compile src.dir=”src/test” build.dir=”build/testclasses/”


Note that you can use standard Ant properties as input, so instead of “src/test” you could use “${src.test.dir}”.

Now let’s take a look at how a complete, working build script could be implemented using macrodef and imports.

<project name="hibernatespring" basedir="." default="continuous">

   <property file="build.properties"/>
   <property file="c:/j2ee/cb/buildfiles/build.properties"/>
   <import file="c:/j2ee/cb/buildfiles/default-build.xml"/>
 <target name="continuous" depends="clean, info, init, compile, test-compile, test, jdepend, checkstyle" description="Target for use from CC - build and generate reports"/>
  <target name="dev" depends="info, init, compile" description="Used for local development build"/>
  <target name="dev-clean" depends="clean, dev" description="Dev but cleaning out old code first"/>
  <target name="dev-test" depends="dev, test-compile, test"

description="Compile and run local unit tests"/> </project>

What I’ve done here is to create a default-build.xml file which contains the standard functionality of a typical project using the Hibernate and Spring frameworks. I have a local build.properties file that defines needed properties for this particular project, and I have a central build.properties file that defines the directory structure of a typical project, and other generic properties that rarely change.

My default-build.xml contains loads of macrodef imports, definitions of typical classpaths and other needed properties, and Ant target definition like this one:

<target name="checkstyle">

  <m_checkstyle />


Challenges and Risks

It will be interesting to see how deep I can go into developing this enhancement to platform ant. Risks: I don't affevt any of the current functionalities.