Wednesday 24 February 2016

Build properties in Ant - An Introduction

Guys, in our previous post we saw a basic build.xml file with some basic target definitions. Note that we mentioned a property or two in it. But have you ever wondered, what happens if you have multiple properties to define? Yea that's when we use a build.properties file.

Let's create a build.properties file and we define all the properties inside it. The advantage of using a properties file is that whenever you want to modify/update the value of any property you only have to do it once in this build.properties. You don't have to look into the build.xml to see when and where you want to modify things. This makes life a lot easier, which otherwise would have been cumbersome.

Ok so I create a new build.properties with the following updated folder structure :


You must have observed that in our older post we included some hardcoded directory paths directly in the build.xml. That's a quite crude way to define the paths and is not advisable. Hardcoded paths should be avoided as much as possible. So what we do is create some properties and define their values equal to the directory paths. Something like this :

versionnum=1.3
workspace.src=/Users/ironcladzone/Documents/workspace/AntBuildTest/src
src.dir=/Users/ironcladzone/Documents/workspace/AntBuildTest/build/src
bin.dir=/Users/ironcladzone/Documents/workspace/AntBuildTest/build/bin

Note that we have also included an additional property named "versionnum" to assign a certain version number to the jar package that we created.

Now we only need to provide a reference to these properties in our build.xml file. Take a look at the new updated build.xml below :

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

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

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

<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="${workspace.src}"
dest="${src.dir}"></copydir>
</target>

<target name="compile" depends="copydir, clean">
<javac classpathref="classpath" includeantruntime="false"
srcdir="${src.dir}"
destdir="${bin.dir}"
includes="**/*.java"></javac>
</target>

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

</project>

Don't you think the new build.xml looks much cleaner now? It does! Right? Okay so with today's post I've introduced you to the concept of properties. In the next post, we'll try to refine the script and introduce some new features into it. Keep visiting folks.

BONUS TIP : Note that properties are immutable. So once a build starts, the value assigned to the properties remain constant. However, if am not wrong, we can update the properties at build time using the propertyFile task. Note that this is different than the <property file> tag. Notice there is no space there in the propertyFile task.

Ok guys, signing out for the day. Let's meet up again tomorrow. Till then, ciao!

No comments:

Post a Comment

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