Saturday 20 February 2016

Ant - Build.xml example on Mac OS X

In today's post guys, we'll cover the topic of building a very simple Java based project using Apache Ant. We'll learn how to write a basic build.xml file for building it.  Let's first look into the code for build.xml and then we'll breakdown the code with the explanation.

build.xml :

<?xml version="1.0" encoding="UTF-8"?>

<project name="AntBuildTest" default="create-jar" basedir=".">

<property name="build.home" value="${basedir}" />

<target name="clean">
<delete dir="${build.home}/src" />
<delete dir="${build.home}/bin" />
<delete dir="${build.home}/lib" />
<delete dir="${build.home}/test" />
<delete dir="${build.home}/dist" />
</target>

<target name="createdir" depends="clean">
<mkdir dir="${build.home}/src" />
<mkdir dir="${build.home}/bin" />
<mkdir dir="${build.home}/lib" />
<mkdir dir="${build.home}/test" />
<mkdir dir="${build.home}/dist" />
</target>

<path id="classpath">
<pathelement location="${build.home}/bin"></pathelement>
</path>

<target name="copydir" depends="createdir">
<copydir src="/Users/ironcladzone/Documents/workspace/AntBuildTest/src"
dest="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"> </copydir>
</target>

<target name="compile" depends="copydir">
<javac classpathref="classpath" includeantruntime="false"
srcdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src"
destdir="/Users/ironcladzone/Documents/workspace/AntBuildTest/build/bin"
includes="**/*.java"></javac>
</target>

<target name="create-jar" depends="compile">
<jar basedir="${build.home}/bin" destfile="${build.home}/dist/TestJar.jar"
includes="**/*.class"></jar>
</target>


</project>

Now let's look into the folder structure in the Eclipse Package Explorer :


Now, let's get down to the explanation guys :

1. First of all, AntBuildTest is my project workspace which has the source code in it.

2. Next I created a build directory under which I created the above build.xml file.

3. Now in the builds.xml I have defined various steps/tasks also known as targets viz : clean, createdir, copydir, compile, create-jar. Note that a build.xml will still successfully build even if it doesn't have a single target defined.

4. For every build.xml file, we first have to define the <project></project> tag, in which we mention the project name, basedir and default target to be executed.

5. Basedir is the location of the current root directory in which build.xml exists i.e the absolute path

6. The default target that we define, will be the task/target which will be executed. Now the target can have dependencies on other targets.

7. Note that target can have one or many dependencies on multiple targets.

8. The 'clean' target is the target to delete all sub-directories that we create in the 'createdir' target. So every time we perform a clean build, the directories created in the last build will be deleted.

9. In the 'createdir' target, we make directories namely : src, bin, lib, test, dist.

10. We now want to copy the source code from our local workspace to the 'src 'directory that we create in the 'createdir' target,. So in the 'copydir' target we define the step to copy the source code.

11. Now once we have the source code, we need to compile it right. So we define the 'compile' target wherein we define 'srcdir' i.e the location where we have our java files. Also we mention the 'destdir' as the bin folder i.e this is where we want our .class files to be saved.

12. During compilation, we need to specifically tell the xml where the .class files would be. So we need to define the classpath. 

13. We define the classpath i.e the bin directory which will have the .class files.

<pathelement location="${build.home}/bin"></pathelement>

14. After compilation, we define the 'create-jar' target, in which we create the jar package - TestJar.jar. Make sure not to miss the .jar file extension. Note that we have mentioned the include parameter for including the **/*.class files that need to be included in it.

15. We have also defined the 'destfile' parameter i.e the location where we want the jar file to be saved i.e the 'dist' directory for distribution.

16. Oh and I missed the 'lib' directory, which will basically hold the external jar files required for our project.

17. Well, I hope I covered the simple basic fundamental points surrounding an Ant build.  Let me know if I missed any point guys. If I did, I'll try to cover them in detail in an another upcoming post covering an advanced scenario. Till then, ciao!

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker