Thursday, October 12, 2017

What are builtin Properties in ANT >> Tutorial Example

This is the fourth article of series "ant tutorial for beginners" here I have included some more fundamental concept in form of Q/A to provide you clear and concise description and situation where you apply these concept. Please let me know if you have any questions , comments I will be happy to reply those .

What are built in properties available in ant ?
Built in properties are those which ant provides by default , you need not have to declare these property you can just use it. You can divide them in two parts

1)     System properties
2)     Properties related to ant

Ant by default provide access to all system property , so you can access them as you do via System.getProperty(). E.g. for accessing file separator you can use ${file.separator} , for getting Operating system name you can use ${} etc.

Apart from system property , ant has its own set of properties e.g. , which is name of project we defining in project tag e.g.

If you have build.xml file with entry

<project name="test" default="all" basedir=".">

You can access project name as ${} , here are some of the important builtin properties which you need to aware

basedir             absolute path of the project's basedir ,defined in project tag.
ant.file            absolute path of the buildfile.
ant.version         version of Ant    name of the current project running, defined in project tag.    java version ant is used for running

normally we use ${} as root directory and specify location of other directory e.g. buld, src related to this.

What are the ways for declaring properties in ANT ?
There are two ways you can declare properties one directly in build.xml and using properties file like I would suggest if you have more than 10 properties think using which is better in terms of maintenance point of view because all properties rely on one location, if you don’t have much property you can still declare them in build.xml file for quick reference. Below are example for both the cases

Declaring and accessing property in build.xml
<property name="build" value="builddir"/>  to access value use ${build}
<property name="build.version">130</property>  to access value use ${build.version}

Its convention to use “.” Dot to join two words e.g. build and version in above example.

Loading property from property file
<property file=""/>
<property resource=""/>
<property name="build.file" location="" relative="true" basedir=".."/>

We can also load property form an url but generally we don’t use it so I am not explaining here.

Can I have two target of same name in build.xml ?
ant command tutorial for beginnersNo you can not , ant will throw error stating target already defined in build.xml file.

Meaning of common symbols used in ant ?
When I started reading my first build.xml it seems to me little crypting because of extensive use of relative path, using filesets and using these specific symbols , later I discovered these are essential for creating a relative and flexible build environment. “fileset” provides grouping of files using these wild cards.

.  (dot) represent current directory
.. (double dot ) represent parent directory
*.jar used to represent all files with the .jar extension in the top directory and
**/*.jsp to represent all files in the entire directory tree with the .jsp extension.

here are some of examples

<property name="cvsroot"   value="../../../../"/>
<property name="src"        value="./src"/>
<path id="classpath">
<fileset dir="${log4j.lib.dir}">
<include name="**/*.jar"/>

How do I fail my build ?
Many times it requires if certain condition is not set then abort the build completely. To achieve this you can use task called <fail> ,it abort current build by thrwoing BuildException. It has two conditional attribute “if” and “unless” which can be used to see if a property is available in build environment or not.

Here are some examples
<fail message="Aborting build."/>
This will abort current build and print message “Aborting build” in console. You can use fail without message attribute as well e.g. <fail/>

<fail unless="build.version" message="build.version is not set aborting build"/>
this will make build to fail if we don’t define property called “build.version” key thing to not here is just to use name of property not value e.g. ${}
here is out put when we don’t have build.version property define in project.

build.xml:10: build.version is not set aborting build

“if” is opposite to “unless” it will abort the build if property is present in build env. E.g. <fail if="build.exit" message="build.exit is set abort the build"/> will abort the build in case we have property “build.exit” available.

Please comment if you like this sort of Q/A explanation or do you prefer concept oriented article.

Further Learning
Maven Fundamentals by Bryan Hansen
Java Web Fundamentals
Apache Ant Fundamentals By Rusty Lowrey

Some other Tutorial you may like


Computerjnkman said...

I hope that the backlinks article helped. If you have any questions, go ahead and fire me a message. I can't say I understand what your blog is about so much...But It looks good, and it looks like your adding lots of content. Keep it up and start generating some backlinks that are focused on specific keywords that people would use in the search engines and you should see your traffic increase.

Anonymous said...

very effective

tushar4evr said...

thanx for the explanation...its very easy and good to understand..Can you please also give a detailed example on "fail" using "if-unless"?? Just to make the things clearer for me

Post a Comment