Any reasonably high-level programming language needs a compiler/interpreter. I wont' go into details of compilers/interpreters now. Rather, the focus is on ANT. Some most frequent tasks a programmer needs to do are clean up, rebuild, debug, run, test and deploy the code. The compilers/interpreters provide support for some of these tasks. Some of the tasks are dependent on the operational environment, for e.g. "rm" is used to remove files on unix where as it is "del" on windows. Let us call these "Developers utility tasks" or DUTs.
Writing a script to achieve these tasks is one way of solving this problem. ANT takes this approach further. ANT can be treated as a domain specific language intended todo these DUTs. Usage of ANT, for the most part, is declarative in nature. Essentially you dont' assign values to variables but state the task you want to achieve. It is kind of saying. "ANT, I want to clean up the mess" or "ANT, Can you compile these sources".
In an IDE driven era, most developers are not conversant with ANT. Those who do, don’t' make noise about it. I feel that, knowing how to write a good ANT script is a must for IDE independent development. Let us see a sample ANT example.
There are essentially three areas when it comes to using ANT.1. Having a clear idea of the directory structure. This is simple for most projects. The complex cases involve code level dependencies across projects. Things can be simplified by assuming presence of referred jar files rather than referred source folders. 2. Identifying and Understanding dependencies among tasks which we wish to have. 3. Writing an ANT file and populating the properties file. Properties file is a mechanism that changes a coding problem into a more manageable configuration problem. This simplifies maintenance.A sample directory structure would look like this• Project folder\ant\ • Project folder\lib\ • Project folder\src\ • Project folder\build\ • Project folder\dist\The "ant" folder is supposed to hold ant scripts. The "src" folder is self-explanatory. The "lib" folder is supposed to hold the jars on which the source files have dependency.The "build" folder contains the resultant files after compilation for e.g. ".class" files. The "dist" folder contains the packaged and ready to use bundles or jar files. "build" and "dist" can be generated by ANT and we do so in our exampleSample build.xml, copy it into your favorite editor and reformat it.##################################################
<?xml version="1.0" encoding="UTF-8"?
>
<project name="SomeProject" default="all" basedir=".."
>
<property file="ant\build.properties"/
>
<path id="classpath"
>
<fileset dir="${lib.dir}"/
>
</path
>
<target name="preconditons"
>
<echo
>Displaying properties...
</echo
>
<echo
>${jar.name}
</echo
>
<echo
>${basedir}
</echo
>
<echo
>------------------------------
</echo
>
</target
>
<target name="init" depends="preconditons"
>
<echo
>Intializing build process...
</echo
>
<mkdir dir="${build.dir}"
>
</mkdir
>
<mkdir dir="${dist.dir}"
>
</mkdir
>
</target
>
<target name="clean"
>
<echo
>Cleaning the ${build.dir}...
</echo
>
<delete dir="${build.dir}"/
>
<delete dir="${dist.dir}"/
>
</target
>
<target name="compile" depends="init"
>
<javac srcdir="${src.dir}"destdir="${build.dir}"compiler="${build.compiler}" source="${source.version}"fork="true" executable="${jdk.path}"
>
<classpath refid="classpath"/
>
</javac
>
</target
>
<target name="jar" depends="compile"
>
<jar destfile="${dist.dir}/${jar.name}" basedir="${build.dir}"
>
<manifest
>
<attribute name="Main-Class" value="${mainclass.name}"/
>
<attribute name="Class-Path" value="${manifest.classpath}"/
>
</manifest
>
</jar
>
</target
>
<target name="run" depends="jar"
>
<java jar="${dist.dir}/${jar.name}" fork="true"
>
</java
>
</target
>
<target name="clean-build" depends="clean,compile"
>
</target
>
<target name="dist" depends="jar"
>
<copy todir="${dist.dir}"
>
<fileset dir="${lib.dir}"
>
</fileset
>
</copy
>
</target
>
<target name="all" depends="clean,dist,run"
>
</target
>
</project
>##################################################The sample properties file, please reformat the same. Remember to escape spaces using "\"################################################### Properties file jar.name=RunThisClient.jar mainclass.name=client.Main manifest.classpath=Common.jar # Taken care by the project directory structure under base dirbase.dir=.. src.dir=src lib.dir=lib build.dir=build dist.dir=dist # Compiler related informationjdk.path=C:\\Program\ Files\\Java\\jdk1.5.0_10\\bin\\javac build.compiler=javac1.5 source.version=1.5###################################################Try using this as a template and you will see that ANT really doesn’t taunt
No comments:
Post a Comment