15 April 2009
0 ANT BASICS
Each 'Project' has a Build File, Default build file name is 'build.xml', Can Specify any name with '-buildfile' command line option, Ant's buildfiles are written in XML.
The first or root element of any buildfile is always the <project> tag. No buildfile can be without one nor can it have more than one.
<project name="MyProject" default="all" basedir=".">
...
</project>
The <project> tag has three attributes: name, default, and basedir.
|
Each buildfile contains one project and at least one (default) target. Examples are: 'compile', 'test', 'install', 'clean', etc.
<target name="dosomething"> | |
| <task1 param1="value1" param2="value2"> |
| <task2 param3="value3" > |
| ... |
</target> |
The <target> tag has three attributes: name, depends, if, unless, descriptiondefault, and basedir.
|
Targets must have a name and may have several additional attributes that determine when and if the target actually gets executed. The target is made up of one or more Tasks like invoke a command or another program. Targets can have Dependencies, examples: 'install' depends on 'compile', Targets can handle cascading dependencies, each Dependency is handled only once, dependency executed only if required.
It should be noted, however, that Ant's depends attribute only specifies the order in which targets should be executed - it does not affect whether the target that specifies the dependency(s) gets executed if the dependent target(s) did not (need to) run.
Ant tries to execute the targets in the depends attribute in the order they appear (from left to right). Keep in mind that it is possible that a target can get executed earlier when an earlier target depends on it.A target gets executed only once, even when more than one target depends on it.
A <task> is a piece of code that can be executed.
A task can have multiple attributes (or arguments, if you prefer). The value of an attribute might contain references to a property. These references will be resolved before the task is executed. Tasks have a common structure:
<name attribute1="value1" attribute2="value2" ... />
where name is the name of the task, attributeN is the attribute name, and valueN is the value for this attribute.
Each task element of the buildfile can have an id attribute and can later be referred to by the value supplied to this. The value has to be unique. Each Task is bound to a Java class file that Ant executes, passing to it any arguments or sub-elements defined with that task. The Ant tool is extensible and it allows you to create your own tasks
Typical build.xml Tasks
init, sets properties, prepare, creates directories, build, builds the system, package, creates jar file, install, installs an application to Tomcat or other engine, deploy, deploy a WAR engine, reload, update previously installed application engine, redeploy.
Properties
A project can have a set of properties. These might be set in the buildfile by the property task, or might be set outside Ant. A property has a name and a value; the name is case-sensitive. Properties may be used in the value of task attributes. This is done by placing the property name between "${" and "}" in the attribute value. For example, if there is a "builddir" property with the value "build", then this could be used in an attribute like this: ${builddir}/classes. This is resolved at run-time as build/classes.
Built-in Properties
Ant provides access to all system properties as if they had been defined using a <property> task. For example, ${os.name} expands to the name of the operating system. For a list of system properties see the Javadoc of System.getProperties.
In addition, Ant has some built-in properties:
basedir the absolute path of the project's basedir (as set with the basedir attribute of <project>).
ant.file the absolute path of the buildfile.
ant.version the version of Ant
ant.project.name the name of the project that is currently executing; it is set in the name attribute of <project>.
ant.java.version the JVM version Ant detected; currently it can hold
the values "1.1", "1.2", "1.3" and "1.4".
Using Ant
Ant is a Java based build tool, similar to make, but with better support for the cross platform issues involved with developing Java applications. Ant is the build tool of choice for all Java projects at Apache and many other Open Source Java projects. Ant can be configured to compile your java source code files, build your deployment JAR and WAR files, unit-test code and create projects javadoc documentation.
Ant 1.6.0 adds a lot of new features, most prominently support for XML namespaces as well as a new concept of Ant libraries that makes use of namespaces to avoid name clashes of custom tasks.
Advantages of Ant
- Build IN Java, USING Java, and FOR Java
- Supports Java Tools (javac, javadoc, etc.)
- XML Build File is Easier to Build, Read, and Maintain than MAKE file
- Easier to Extend
- Supports Cross Platform Java Development
- Ant is much faster than using MAKE ? Each command is a new process
- Ant runs within the JVM
- Each command is executed from within JVM
- Tools like javac are new threads – not new process
- Compiling large number of java source files is MUCH,MUCH faster with Ant
- Ant's Debug Options are very helpful
- XML much easier to read than MAKEFILE
- User does not need to know all command line interface options to tools that can be called programmatically
- Even OS-Specific commands can be setup in 'TaskDefs' and/or included from other sources
0 comments:
Please give your valuable comments.