Rabu, 12 Februari 2014

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 command tutorial for beginners

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 ${os.name} etc.





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





If you have build.xml file with entry










You can access project name as ${ant.project.name} , 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


ant.project.name    name of the current project running, defined in project tag.


ant.java.version    java version ant is used for running








normally we use ${ant.project.name} 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 build.properties. I would suggest if you have more than 10 properties think using build.properties 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


  to access value use ${build}

130  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











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 ?


No 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






















-->

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 ,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




This will abort current build and print message “Aborting build” in console. You can use fail without message attribute as well e.g.







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. ${build.properties}

here is out put when we don’t have build.version property define in project.



BUILD FAILED


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. 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.





Some other Tutorial you may like






































Source:http://javarevisited.blogspot.com/2010/10/ant-tutorial-part-4.html

Tidak ada komentar:

Posting Komentar