Introduction to Ant

xiaoxiao2021-03-06  59

What is ANT?

2. Install ANT

3. Run Ant

4. Write build.xml

5. Built-in Task (Internet)

6. Ear Task (Internet)

7. War Task (Internet)

8. JUnit Task (Internet)

-------------------------------------------------- ------------------------------

What is it? -------------------------------------------------- ------------------------------

ANT is a BUILD tool based on Java. In theory, it is some similar (unix) Make, but there is no Make's defect.

Since we already have make, gnumake, nmake, jam, and other build tools, why do you have a new build tool? Because Ant is developing software in a variety of (hardware) platforms, they cannot endure the limitations and inconveniences of these tools. Tools similar to MAKE is essentially a shell (language): they calculate dependencies, then execute commands (these commands are not very different from the command you are on command.). This means that you can easily extend the tool by using the OS unique or writing new (command) programs; however, this also means you limit yourself to a specific OS, or on a specific OS type, such as UNIX.

Makefile is also very disgustable. Anyone who has used their people have encountered a variable Tab problem. Ant's original author often asked himself: "Is my order not executing just because there is a space before my tab ?!!". Tools similar to JAM handle such problems well, but (users) must remember and use a new format.

Ant is different. Unlike the extended mode based on the shell command, ANT expands with Java classes. (Users) Don't have to write the shell command, the configuration file is based on XML, and all kinds of TASK can be performed by calling the Target tree. Each Task is run by an object that implements a specific TASK interface. (If you have no words to Ant, you may not understand this section, there is no relationship, you will introduce the Target, Task. You can even skip this section without too much time, then then When I came back, I would understand it here. Similarly, if you are not familiar with the tools such as Make, the following introduction will not use the concept in Make.)

It must be admitted that this will lose some unique expressive capabilities when constructing the shell command. Such as `find. -Name foo -exec rm {}`, but gives you a cross-platform ability - you can work anywhere. If you really need to perform some shell commands, Ant has a Task, which allows the command on a specific OS.

2. Install ANT ---------------------------------- ----------------------------------

Since Ant is an Open source software, there are two ways to install Ant, one is to install Ant with a compiled binary file, and the other is the source code you build Ant.

The binary form in the form of Ant can be downloaded from http://jakarta.apache.org/builds/ant/release/v1.4.1/bin. If you want you to compile Ant, you can get from http://jakarta.apache.org/builds/ant/release/v1.4.1/src. Note that the listed connections are the latest release of Ant. If you read this article, found that there is already an updated version, then use the new version. If you are a crazy technical pursuit, you can also download the latest version from Ant CVS Repository. system requirement

Want to think about Build Ant. You need a JAXP-compatible XML parser (PARSER) in your ClassPath system variable.

The binary form of Ant includes the latest version of the Apache Crimson XML parser. You can get more information about JAXP from http://java.sun.com/xml/. If you want to use other JAXP compatible parsers. You have to delete JAXP. JAR and CRIMSON.jar from the LIB directory of Ant. Then you can put your loved parser JAR file in the Ant's lib directory or put it in your ClassPath system variable.

Install ANT

Binary version of Ant includes three directories: BIN, DOCS and LIB. Only the bin and lib directory are required to run Ant. To install Ant, select a directory and copy the release of the release to this directory. This directory is called Ant_Home.

Do some configuration work before you run Ant.

Add a bin directory to the PATH environment variable.

Set the ANT_HOME environment variable to point to the directory where you install Ant. On some OS, Ant's script can guess Ant_Home (UNIX and WindOS NT / 2000) - but it is best not to rely on this feature.

Alternatively, the Java_Home environment variable (refer to the following advanced section), which should point to the directory of the JDK installed.

Note: Do not put the Ant.jar files of the Ant to the lib / ext directory of JDK / JRE. Ant is an application, and the lib / EXT directory is used for JDK extension (such as JCE, JSSE extension). And there will be security restrictions by extension.

Optional Task

Ant supports some optional TASK. An optional Task generally requires an additional library to work. Optional Task is separated from the built-in Task of Ant, packaged separately. This optional package can be downloaded from the same place you download from Ant. The JAR file name called optional Task is called Jakarta-Ant-1.4.1-optional.jar. This JAR file should be placed in the lib directory of the Ant installation directory.

The external library required for each optional Task can be referring to the dependent library section. These external libraries can be placed in the LIB directory of Ant, so Ant can be automatically loaded, or put it in an environment variable.

Windows

Assume that Ant is installed in a C: / Ant / directory. Below is a command to set the environment:

SET ANT_HOME = C: / Ant

SET JAVA_HOME = C: /JDK1.2.2

SET PATH =% PATH%;% Ant_Home% / BIN

UNIX (Bash)

Assume that Ant is installed in the / usr / local / Ant directory. Below is a command to set the environment:

Export Ant_Home = / usr / local / Ant

Export java_home = / usr / local / jdk-1.2.2

Export Path = $ {Path}: $ {ant_home} / bin

advanced

To run Ant, you must use a lot of variables. You need at least reference to the following:

Ant's classpath must contain Ant.jar and the JAR file that you choose JAXP-compatible XML parser.

When you need JDK's function (such as Javac or RMIC Task), for JDK 1.1, JDK's classs.zip file must be placed in ClassPath; for JDK 1.2 or JDK 1.3, you must join Tools.jar. If the correct Java_Home environment variable is set, the script belongs in Ant, in the bin directory, automatically add the desired JDK class. When you perform a specific platform program (such as Exec Task or CVS Task), you must set the Ant.home property to point to the installation directory of Ant. Similarly, the scripts belled by the ANT use the ANT_HOME environment variable to automatically set this property.

Building Ant

To want to install the ANT source code release from the source code, you have to install the ANT source code or from the CVS in the CVS.

After installing the source code, enter the installation directory.

Set the Java_Home environment variable points to the JDK installation directory. If you want to know how to do, please refer to install the ANT section.

Make sure you have downloaded any auxiliary JAR file so that builds you are interested in Task. These JAR files can be placed in the classpath or placed in the lib / optional directory. See those JAR files in the dependent Task that can be seen from the Library section. Note that these JAR files are just used as build Ant. To run Ant, you have to set these JAR files as you do in the ANT section.

Now you can build Ant:

Build -ddist.dir = DIST (Windows)

Build.sh -ddist.dir = DIST (UNIX)

This will create a binary version in the directory you specify.

The above command executes the following action:

If there is a need to bootstrap Ant's code. Bootstrap includes manually editing some ANT code to run Ant. Bootstrap is used in the build step below.

Pass parameters to the build script to call bootstrap Ant. The parameter defines the attribute values ​​of Ant and specifies the "dist" Target of Ant's own build.xml file.

In most cases, you don't have to directly bootstrap Ant, because the Build script is done for you. Run bootstrap.bat (windows) or bootstrap.sh (unix) can build a new Bootstrap version Ant.

If you want to install Ant into the Ant_Home directory, you can use:

Build Install (Windows)

Build.sh Install (UNIX)

If you want to skip the lengthy Javadoc steps, you can use:

Build Install-Lite (Windows)

Build.sh Install-Lite (UNIX)

This will only install the bin and lib directory.

Note INSTALL and Install-Lite will override the current Ant version in Ant_home.

Dependency

If you need to perform a specific Task, you need to put the corresponding library into the ClassPath or put it in the LIB directory of the Ant installation directory. Note You only need a regexp library when using Mapper. At the same time, you have to install an optional JAR package of Ant, which contains the definition of Task. Refer to the above installation ANT section.

Jar Name Needed for Available At

An Xsl Transformer Like Xalan Or XSL: P Style Task http://xml.apache.org/xalan-j/index.html or http://www.clc-marketing.com/xslp/jakarta-regExp-1.2.jar Regexp Type with mappers jakarta.apache.org/regexp/

Jakarta-oro-2.0.1.jar regexp type with mappers and the perforce tasks jakarta.apache.org/oro/

Junit.jar Junit Tasks www.junit.org

STYLEBOOK.JAR STYLEBOOK TASK CVS repository of xml.apache.org

Testlet.jar test task java.apache.org/framework

ANTLR. Jar Antlr Task www.antlr.org

Bsf.jar script task oss.software.ibm.com/developerWorks/Projects/BSF

NetRexx.jar NetRexx Task www2.hursley.ibm.com/netRexx

Rhino.jar JavaScript with script task www.mozilla.org

Jpython.jar python with script task www.jpython.org

Netcomponents.jar ftp and telnet tasks www.savarese.org/oro/downloads

3. Run Ant -------------------------------------------------------------------------------------------------------------------------- ----------------------------------

Running Ant is very simple, when you correctly install Ant, just enter Ant.

When you do not specify any parameters, Ant will query the build.xml file in the current directory. If you find it, use this file as buildfile. If you use the -find option. Ant will look for BuildFile in the superiors until the root of the file system is reached. To allow Ant to use other buildfiles, you can use the parameter -Buildfile file, where the file specifies the buildfile you want to use.

You can also set some properties to override the property value specified in the buildfile (see Property Task). You can use the -Dproperty = value option, where Property is the name of the attribute, and the value is the value of the attribute. This approach can also be used to specify the value of some environment variables. You can also use Property Task to access environment variables. Just pass -dmyvar =% myvar% (Windows) or -dmyvar = $ myvar (unix) to Ant - you can access these environment variables with $ {myvar} in your buildfile.

There are two option -quite, telling Ant run only a small amount of necessary information. And -verbose tells Ant to output more information.

You can specify one or more Targets. When Target is omitted, the ANT uses the target attribute specified by the Default property of the label .

If there is, the -projecthelp option outputs a list of description information and item Target. The Targets that are described and then described are not described.

Command line option summary:

Ant [options] [target [target2 [target3] ...]]]]]]]

Options:

-Help Print this Message

-ProjectHelp Print Project Help Information

-version print the version information and exit

-quiet be extra quiet

-verbose be extra verbose

-debug print debugging information

-emacs produpe logging information without adornments

-logfile file use Given File for log output

-logger classname the class thing is to perform logging

-Listener ClassName Add an Instance of Class As a Project Listener

-Buildfile File Use Specified BuildFile

-find file search for buildfile touth the root of the filesystem and use the first one found

-Dproperty = Value Set Property to Value

example

Ant

Use the build.xml in the current directory to run Ant, perform the default target.

Ant -buildfile Test.xml

Use the Test.xml in the current directory to run Ant, perform the default target.

Ant -Buildfile Test.xml Dist

Use the Test.xml under the current directory to run Ant, perform a target called DIST.

ANT -BUILDFILE TEST.XML -DBUILD = Build / Classes Dist

Use the Test.xml in the current directory to run Ant, perform a target called Dist, and set the value of the build property of the build / class.

file

On UNIX, the execution scripts of Ant will source before doing anything (read and calculate values) ~ / .antrc files; on Windows, Ant's batch file call% home% / antc_pre.bat at the beginning, Call% Home% / ANTRC_POST.BAT at the end. You can use these files to configure or cancel some environment variables you need when you run Ant. Look at the example below.

Environment variable

Wrapper scripts use the following environment variables (if any):

JavaCMD Java executable absolute path. Use this value to specify a JVM different from java_home / bin / java (.exe).

Ant_opts passed to the JVM command line variable - for example, you can define the attribute or set the maximum of Java stacks.

Manual running Ant

If you do your own (DIY) Ant, you can start Ant with the following command:

Java-Dant.home = C: / Ant Org.apache.tools.ant.main [options] [target]

This command is the same as the front Ant command. Options and Targets are also the same as when using an ANT command. This example assumes that your classpath contains:

Ant.jar

Jars / Classes for your XML Parser

THE JDK's Required Jar / Zip Files

4. Write build.xml ---------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------

Ant's buildfile is written with XML. Each buildfile contains a Project. Each Task element in BuildFile can have an ID attribute that can be used to reference the specified task with this ID value. This value must be unique. (For details, please refer to the Task section below)

Projects

Project has the following properties:

Attribute Description Required

Name project name. no

Default uses the default Target Yes used when you do not specify Target

BaseDIR is used to calculate the base path of all other paths. This property can be overwritten by BaseDir Property. This property is ignored when it is overwritten. If the attributes are not set, the parent directory of the buildfile file is used in BUILDIR PROPERTY. NO

The description of the project appears in the form of a top element (see Description Section).

A project can define one or more Targets. A Target is a series of what you want to do. When performing Ant, you can choose to execute that Target. When there is no Target, use the Target determined by the Project's Default property.

Targets

A Target can rely on other Targets. For example, you may have a Target for compiler, a Target to generate an executable. You must compile the pass before generating an executable, so the target that generates executable files depends on compiling Targets. Ant will deal with this dependency.

However, it should be noted that Ant's Depends attribute specifies the order of Target should be executed - if the dependent target cannot be run, this depends has no effect on target specified dependencies.

Ant will perform each target in order (from left to right) in the order of Target in the Depends property. However, it is necessary to remember that as long as a Target relies on a Target, the latter will be executed first.

Assume that we have to perform Target D. From its dependency properties, you may think that C, then B, and final A is executed. Wrong, C depends on B, B depends on A, so before the A, then B, then C, and final D is executed.

A Target can only be executed once, and there are multiple Targets depending on it (see the above example).

If (or if not) certain attributes are set, a Target is executed. In this way, the process of Build is allowed to better control the process of the system according to the status of the system (Java Version, OS, command line attribute, etc.). To make a target element, you should join the IF (or UNSS) property in the Target element, with the target of the determined attribute. E.g:

If there is no IF or UNLESS property, Target will always be executed.

Optional Description attributes can be used to provide a line description for Target, which can be output by the -projecthelp command line option.

Well your TSTamp Task is a good practice in a so-called initial Target, and other Targets rely on this initial Target. To ensure that the initialization Target is the first Target that appears in other Target rerequisites. Most of the initial Target in this manual is "init".

Target has the following properties:

Attribute Description Required

Name Target's name YES

Depends is a list of names of the Target separated by a comma, that is, the dependent table. NO

IF performs the name of the attribute name you need to be set. NO

Unless executes the TARGET to clear the set properties name. NO

Description Brief Description About Target features. NO

Tasks

A Task is an executable code.

A Task can have multiple properties (if you like, you can call it as a variable). Attributes may only contain references to Property. These references will be parsed before TASK execution.

Below is a general constructor of Task:

Here Name is the name of Task, attributen is the property name, VALUEN is attribute value.

There is a built-in Task, as well as some optional Task, but you can also write your own task.

All Task has a Task name property. ANT uses attribute values ​​to generate log information.

You can assign a ID attribute to Task:

Here TaskName is the name of Task, and Taskid is the unique identifier of this Task. With this identifier, you can reference the corresponding Task in the script. For example, you can do this in the script: