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
|
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.
...
</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 .
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".
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
Advantages of Ant
|
|
|
||
Running Ant
|
|
||
Command
Line
If you've installed Ant as described in the Installing Ant section, running Ant from the command-line is simple: just type ant.
When no arguments are specified, Ant looks for a build.xml file in the current directory and, if found, uses that file as the build file and runs the target specified in the default attribute of the <project> tag. To make Ant use a build file other than build.xml, use the command-line option -buildfile file, where file is the name of the build file you want to use
If you use the -find [file] option, Ant will search for a build file first in the current directory, then in the parent directory, and so on, until either a build file is found or the root of the filesystem has been reached. By default, it will look for a build file called build.xml. To have it search for a build file other than build.xml, specify a file argument.
Note: If you include any other flags or arguments on the command line after the -find flag, you must include the file argument for the -find flag, even if the name of the build file you want to find is build.xml.
You can also set properties on the command line. This can be done with the -Dproperty=value option, where property is the name of the property, and value is the value for that property. If you specify a property that is also set in the build file (see the property task), the value specified on the command line will override the value specified in the build file. Defining properties on the command line can also be used to pass in the value of environment variables - just pass -DMYVAR=%MYVAR% (Windows) or -DMYVAR=$MYVAR (Unix) to Ant. You can then access these variables inside your build file as ${MYVAR}. You can also access environment variables using the property task's environment attribute.
If you've installed Ant as described in the Installing Ant section, running Ant from the command-line is simple: just type ant.
When no arguments are specified, Ant looks for a build.xml file in the current directory and, if found, uses that file as the build file and runs the target specified in the default attribute of the <project> tag. To make Ant use a build file other than build.xml, use the command-line option -buildfile file, where file is the name of the build file you want to use
If you use the -find [file] option, Ant will search for a build file first in the current directory, then in the parent directory, and so on, until either a build file is found or the root of the filesystem has been reached. By default, it will look for a build file called build.xml. To have it search for a build file other than build.xml, specify a file argument.
Note: If you include any other flags or arguments on the command line after the -find flag, you must include the file argument for the -find flag, even if the name of the build file you want to find is build.xml.
You can also set properties on the command line. This can be done with the -Dproperty=value option, where property is the name of the property, and value is the value for that property. If you specify a property that is also set in the build file (see the property task), the value specified on the command line will override the value specified in the build file. Defining properties on the command line can also be used to pass in the value of environment variables - just pass -DMYVAR=%MYVAR% (Windows) or -DMYVAR=$MYVAR (Unix) to Ant. You can then access these variables inside your build file as ${MYVAR}. You can also access environment variables using the property task's environment attribute.
Options
that affect the amount of logging output by Ant are:
-quiet, which instructs Ant to print less information to the console;
-verbose, which causes Ant to print additional information to the console;
-debug, which causes Ant to print considerably more additional information.
It is also possible to specify one or more targets that should be executed. When omitted, the target that is specified in the default attribute of the project tag is used.
The -projecthelp option prints out a list of the build file's targets. Targets that include a description attribute are listed as "Main targets", those without a description are listed as "Subtargets", then the "Default" target is listed.
Command-line Options Summary
ant [options] [target [target2 [target3] ...]]
Options:
-quiet, which instructs Ant to print less information to the console;
-verbose, which causes Ant to print additional information to the console;
-debug, which causes Ant to print considerably more additional information.
It is also possible to specify one or more targets that should be executed. When omitted, the target that is specified in the default attribute of the project tag is used.
The -projecthelp option prints out a list of the build file's targets. Targets that include a description attribute are listed as "Main targets", those without a description are listed as "Subtargets", then the "Default" target is listed.
Command-line Options Summary
ant [options] [target [target2 [target3] ...]]
Options:
-help,
-h
|
print
this message
|
-projecthelp,
-p
|
print
project help information
|
-version
|
print
the version information and exit
|
-diagnostics
|
print
information that might be helpful to diagnose or report problems.
|
-quiet,
-q
|
be
extra quiet
|
-verbose,
-v
|
be
extra verbose
|
-debug,
-d
|
print
debugging information
|
-emacs,
-e
|
produce
logging information without adornments
|
-lib
<path>
|
specifies
a path to search for jars and classes
|
-logfile
<file>
|
use
given file for log
|
-l
<file>
|
use
given file for log
|
-logger
<classname>
|
the
class which is to perform logging
|
-listener
<classname>
|
add
an instance of class as a project listener
|
-noinput
|
do
not allow interactive input
|
-buildfile
<file>
|
use
given buildfile
|
-file
<file>
|
use
given buildfile
|
-f
<file>
|
use
given buildfile
|
-D<property>=<value>
|
use
value for given property
|
-keep-going,
-k
|
execute
all targets that do not depend on failed target(s)
|
-propertyfile
<name>
|
load
all properties from file with -D properties taking precedence
|
-inputhandler
<class>
|
the
class which will handle input requests
|
-find
<file>
|
(s)earch
for buildfile towards the root of
|
-s
<file>
|
the
filesystem and use it
|
No comments:
Post a Comment