[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
ANT
Although ANT is a stand alone tool it has become the defacto standard java build tool both in stand alone forms and integrated in the top IDEs. Thus it is important for beginning java programmers to get a good grasp of how to use the ANT java build tool in their projects.
In this example I will show you how to write your first ANT Build File. First lets make sure you have ANT installed.If you have a modern IDE such as Jbuilder, NetBeans, Sun Studio One, Eclipse, WASD, or etc than your version of ANt is already installed as a plugin for that IDE.
If not then more than likely you have to install a copy of ANT on your devloper machine. Once you install it according to the directions for your platform OSmake sure to set your system environmental to ANT_HOME with the location of the ANT directory.
ANT uses a modified xml format, modified meaning that you do not actually see the DTD as a small one is use dot load the ant core tasks. Thus, each of your tag or tags have the normal xml begining and ending syntax.
The top most tag in your build file will be the project tag which can look like this:
The default attribute tells ANT that the default task that should be run if you just click on the build.xml file is the dist task. The basedir attribute tells ANT that the base directory is the directory that the build.xml is found in at this moment.
Okay lets add some other stuff to this example:
For each project and task you should always include a description of that project or task and thus you see a description block for our project. Also notice that you can use the normal <!-- --> comment tags to set of comments about the build file.
Normally no matter what area of java you are programming in you will need 3 subfolders for the build; src, build, and dist. You are going to use the property tags to set a property name of the subfolder with the location to use and refer to in the build file.
Property tasks are the only ones that can be placed outside of targets. The property name is refered to in upcoming parts of build file as ${propertyname}.
Now lets add the tasks that are needed:
Typing -projecthelp as an ANT command line option will display the decriptions of the public targets and that is one reason we put those descriptions there as added information to the user of the build file.
ANT is not a normal declarative srcipt so we use specific atttributes to route the logiic of the build file completion. One of these is the depends attribute.
The depends attribute in the target tells ANT to go do the noted target before startign that target. For example the compile target actually completes the init target before even starting on the compile target.
You might be asking at this poitn but what about using 3rd party libraries where we have to have the location added to the classapth? You use something like this:
You would then in places such as the comile task where you need to reference this added to classpath use the ${base.path} variable name. You can put this path setup on the same level as targets say at the beginning of the build file right after rest of the directory properties have been defined.
You will also find in using ANT that several companies and developers have extended ANT through use of external tools and libraries consisting of new ANT tasks. These external tools and tasks are often added by putting the library in the ANT libary folder.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
ANT
What is Apache's ANT
Apache's ANT is a java build tool that replaced using modifications of make early in the history of java programming. Most Modern IDE's either integrate with using ANT or have a plugin for using ANT as your Java Build Tool.Short History of ANT
Jame Duncan Davidson started creating ANT in early 1999 to solve a problem that he had in devloping Tomcat. He was using shell scripts to compile that were introducing errors in the build process. Thus he wrote the ANT tool to control the build process from an xml script and this tool became an Apache project in 2000.Although ANT is a stand alone tool it has become the defacto standard java build tool both in stand alone forms and integrated in the top IDEs. Thus it is important for beginning java programmers to get a good grasp of how to use the ANT java build tool in their projects.
How To Write an ANT build file
As with most modern OOP languages, Java Programming requires you to use accessory tools that make it easy to reach your full programming potential. The ANT make tool is one of these tools.In this example I will show you how to write your first ANT Build File. First lets make sure you have ANT installed.If you have a modern IDE such as Jbuilder, NetBeans, Sun Studio One, Eclipse, WASD, or etc than your version of ANt is already installed as a plugin for that IDE.
If not then more than likely you have to install a copy of ANT on your devloper machine. Once you install it according to the directions for your platform OSmake sure to set your system environmental to ANT_HOME with the location of the ANT directory.
ANT uses a modified xml format, modified meaning that you do not actually see the DTD as a small one is use dot load the ant core tasks. Thus, each of your tag or tags have the normal xml begining and ending syntax.
The top most tag in your build file will be the project tag which can look like this:
<project name="MyFirstJavaProject" default="dist" basedir="."> </project>
The default attribute tells ANT that the default task that should be run if you just click on the build.xml file is the dist task. The basedir attribute tells ANT that the base directory is the directory that the build.xml is found in at this moment.
Okay lets add some other stuff to this example:
<project name="MyFirstJavaProject" default="dist" basedir=".">
<description>
Simple example build file
</description>
<!-- Set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist""/>
</project>
For each project and task you should always include a description of that project or task and thus you see a description block for our project. Also notice that you can use the normal <!-- --> comment tags to set of comments about the build file.
Normally no matter what area of java you are programming in you will need 3 subfolders for the build; src, build, and dist. You are going to use the property tags to set a property name of the subfolder with the location to use and refer to in the build file.
Property tasks are the only ones that can be placed outside of targets. The property name is refered to in upcoming parts of build file as ${propertyname}.
Now lets add the tasks that are needed:
<project name="MyFirstJavaProject" default="dist" basedir=".">
<description>
Simple example build file
</description>
<!-- Set global properties for this build -->
<property name="src" location="src"/>
<property name="build" location="build"/>
<property name="dist" location="dist""/>
<target name="init" description="initialize">
<!-- Create the time stamp -->
<tstamp/>
<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>
<target name="compile" depends="init" descrition="compile the source">
<!-- Compile the java code from ${src} into ${build} -->
<javac src dir="${src}" destdir="${build}"/>
</target>
<target name="dist" depends="compile" description="generate the distribution">
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the
Project-${DSTAMP}.jar file
-->
<jar jarfile="${dist}/lib/MyFirstJavaProject-${DSTAMP}.jar"
basedir="${build}"/>
</target>
<target name="clean" description="clean up" >
<!-- Delete the ${build} and ${dist} directory trees -->
<delete dir="${build}"/>
<delete dir="${dist}/>
</target>
</project>
Typing -projecthelp as an ANT command line option will display the decriptions of the public targets and that is one reason we put those descriptions there as added information to the user of the build file.
ANT is not a normal declarative srcipt so we use specific atttributes to route the logiic of the build file completion. One of these is the depends attribute.
The depends attribute in the target tells ANT to go do the noted target before startign that target. For example the compile target actually completes the init target before even starting on the compile target.
You might be asking at this poitn but what about using 3rd party libraries where we have to have the location added to the classapth? You use something like this:
<path id="base.path">
<pathelement path="${classpath}"/>
<fileset dir="lib">
<include name="**/*.jar"/>
</fileset>
<pathelement location="classes"/>
</path>
You would then in places such as the comile task where you need to reference this added to classpath use the ${base.path} variable name. You can put this path setup on the same level as targets say at the beginning of the build file right after rest of the directory properties have been defined.
You will also find in using ANT that several companies and developers have extended ANT through use of external tools and libraries consisting of new ANT tasks. These external tools and tasks are often added by putting the library in the ANT libary folder.
Resources
- Apache's ANT Offical Website --The location to download ANT and view the User's Manual.
- The Offical ANT Wiki --A good resource for everything that is ANTas a Wiki page.
- ANT in ANGER The Guide to Correctly Using ANT syntax.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
