If you are a Java programmer, you should be familiar with properties which are used to maintain the list of key-value pairs. Ant properties too store key-value pairs.
Ant properties play an important role during the build as the placeholder for key/value pairs. To successfully run Ant build, some of the useful properties must be set first and then read it during appropriate targets. So you set it once and can use it multiple times during the build. You might need build number, app name etc during multiple targets (during generating jar, war or ear). Let's explore it:
Create Properties
Ant provides property task to create properties. Depending on the scope we can create them at the top of the build.xml file or in the specific target.
<!-- simple key value pairs -->
<property name="build.debug" value="true"/>
<property name="blog.name" value="geekrai"/>
<!-- location attribute converts relative path to an absolute path-->
<property name="release.dir" location="../release" />
In the second case, release.dir property stores platform agnostic path of the release directory. So, depending on underlying OS (Window/Unix/Mac) it will appropriately store the release directory path.
Reading Properties
Property value can be read by surrounding it with ${}.
So, quite logically the property needs to be defined first before reading its value. Here, Ant does an incredible job of providing some pre-populated in-build properties. So you can just use them without bothering for declaration.
Common inbuilt-properties
ant.file: Absolute path of build file
ant.home: Absolute path of Ant installation directory
ant.java.version: Ant detected JVM version
basedir: Absolute path of projects home directory
user.name: Username
This list is quite big, if you want to see the full list provided by Ant, use below target ($ant printAll).
<echo message="user.name = ${user.name}"/>
<echo>release.dir = ${release.dir}</echo>
Common inbuilt-properties
ant.file: Absolute path of build file
ant.home: Absolute path of Ant installation directory
ant.java.version: Ant detected JVM version
basedir: Absolute path of projects home directory
user.name: Username
This list is quite big, if you want to see the full list provided by Ant, use below target ($ant printAll).
<target name="printAll">
<echoproperties />
</target>
Loading from a Properties file
Properties by default are put in the build file, but you can organize them into a separate properties file as well. This helps in easy management of properties, especially if your build file is quite huge. So to achieve this, we can create build.properties in the root directory of the project.
#sample property from build.property file
app.name=AntDemo
build.debug=true
#end of file
It can be loaded using Ant property task as shown below:
<property file="build.properties">
But, do keep in mind that properties are loaded as simple string values so if you set relative file path, it stays as it (unlike location). Also, Ant uses java.util.Properites class to load properties so you should do proper escaping for backslash (\ into \\).
#sample property from build.property file
app.name=AntDemo
build.debug=true
#end of file
It can be loaded using Ant property task as shown below:
<property file="build.properties">
But, do keep in mind that properties are loaded as simple string values so if you set relative file path, it stays as it (unlike location). Also, Ant uses java.util.Properites class to load properties so you should do proper escaping for backslash (\ into \\).
Setting from Command Line
Until now, we discussed about setting properties in build file or separate file. Ant also allows to dynamically set the value of a property. If the value is already getting assigned in build file, then it will get overridden with this approach. Remember, the first assignment rules!
$ant -D<<propertyName=value>> <<target>>
$ant -Dapp.name=dummy build
So in above example, app.name value will become dummy. This always takes precedence. The reason for this is that, properties are immutable so once set it will not change during the course of that build process.
---
keep coding !!!
No comments:
Post a Comment