Women in Technology

Hear us Roar



Article:
  Top 15 Ant Best Practices
Subject:   Best practices I follow
Date:   2005-04-18 06:35:23
From:   chinmayas
Say I have few modules common, ejb1, webapp1, ear
I would create build file as follows
<project name="project1" default="build" basedir=".">


<target name="init">
read props file
init all the properties like dir, jar names
</target>


<target name="common" depends="init,compile_common,jar_common">
<target name="compile_common" depends="init">
compile common code
</target>
<target name="jar_common" depends="init">
jar common code
</target>


<target name="ejb1" depends="init,common,compile_ejb1,jar_ejb1">
<target name="compile_ejb1" depends="init">
compile ejb1 code
run appc
</target>
<target name="jar_ejb1" depends="init">
jar ejb1 code
</target>


<target name="webapp1" depends="init,common,ejb1,compile_webapp1,war_webapp1">
<target name="compile_webapp1" depends="init">
compile webapp1 code
run jspc
</target>
<target name="war_webapp1" depends="init">
create webapp war
</target>


<target name="ear_app" depends="init,common,ejb1,webapp1,ear_only">
build ear - executed all dependencies
</target>
<target name="ear_only" depends="init">
build only ear
</target>


<target name="build" depends="init,common,ejb1,webapp1,ear_app">
build all
</target>


<target name="deploy" depends="init,build,deploy_only">
run dependencies and deploy
</target>
<target name="deploy_only" depends="init">
deploy to server
</target>
<target name="undeploy" depends="init">
undeploy ear
</target>
<target name="clean" depends="init">
delete build, classes, generated dirs
</target>


</project>



To just build
$ ant


To build and deploy
$ ant deploy


If modifying common and want to deploy
add following one-liner target
<target name="common_deploy" depends="init,common,ear_only,deploy_only"/>
now run
$ ant common_deploy


similarly you can have war_deploy, ejb1_deploy etc.


Having fine grained targets helps to compose utility targets like above. These


improves the productivity of the developers. Developer do not have to wait for the


whole product/project to build, just compile changed module and re-jar it and deploy.



Note: In the above build file, you might notice in 'build' target is calling common,


ejb1, webapp1 and ear_app. And ear_app is also calling common, ejb1, webapp1 - this is


not going to call these targets twice. Ant does not call the dependency target twice.


regards
chinmaya