Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

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}" />
  <sequential>
     <mkdir dir="@{build.dir}" />
     <javac destdir="@{build.dir}" debug="${debug}"
       optimize="${optimize}">
        <classpath refid="@{classpath.id}" />
        <src path="@{src.dir}" />
     </javac>
  </sequential>

</macrodef>


IMPORT

<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.

USING A MACRODEF FROM AN ANT SCRIPT

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”>

  <m_compile/>

</target>

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/”
       classpath.id=”test.classpath.id”/>

</target>

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 />

</target>


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.

Copyright © Eclipse Foundation, Inc. All Rights Reserved.