Introduction to ANT

xiaoxiao2021-03-06  39

Introduction to ANT

Ant is a sub-project of Apache Engineering is a Java-based build tool. ANT is similar to the Make tool, but there is no shortcoming of traditional Make tools. Traditional MAKE often can only be used on a platform. Ant itself implements the Java class. The project's profile to be constructed is described in XML format. It can easily implement multi-platform compilation, which is ideal for Build Large Works.

1. Install configuration ANT:

Ant can be from

Http://ant.apache.org download, the latest version is 1.5.2. After the download is complete, the direct decompression is as follows:

Ant

- BIN // Contains Launcher Scripts

|

- LIB // Contains Ant Jars Plus Necessary Dependencies

|

- DOCS // Contains Documentation

| - ANT2 // a brief description of ant2 request of Ant2 Requirements

| | |

| - Image // Various logos for HTML Documentation

| | |

| - Manual // Ant Documentation (a Must Read)

|

- ETC

Environment variables that need to be set:

ANT_HOME: Ant installation directory

Java_Home: JDK installation directory

PATH: Plug the% Ant_HOME% / bin directory to the PATH variable so that you can run Ant directly from the command line

Assume that ANT is installed in C: / Ant JDK D: /J2SDK1.4.0

The following command is executed under the command line:

SET ANT_HOME = C: / Ant

SET JAVA_HOME = D: /J2SDK1.4.0

SET PATH =% PATH%; C: / Ant / bin

When the Win2000 command prompt is working, the above setting must be performed each time, after exiting the command prompt, the values ​​of these variables will return to the original look. To avoid these troubles, you can set in the control panel / system / advanced / environment variable.

Once the above setting is complete, Ant can be used.

2. Establish an engineering description file build.xml

It is very convenient to build a large-scale project with ANT. Each project corresponds to a build.xml file, which contains path information and tasks related to this project. Below is an example of build.xml:

Description = "CREATE BINARY Distribution">

WebXML = "Web-Inf / Web.xml">

The project directory to be built is as follows:

Struts_Demo

- JSP

|

- SRC

|

- Web-INF

| - Classes

| | |

| - LIB

| | |

| - Web.xml

|

- build.xml

Each build.xml file contains a Project and at least one target. Target contains task elements, tasks are an executable code, and each task element has an ID property to facilitate reference in the file. Ant has built-in task sets available, such as Property, Javac, and War used in the above file, complete setting properties, compile and package tasks, respectively. Of course, if you need it, you can also write your own task.

Build.xml's root element is Progject, which has three attributes name default basedir, where default is required. Name Specifies the name of the project, basedir represents the base path of the project, set to "." indicates the path where build.xml is located. Default represents the default target, if you do not specify a target when running Ant, use the target specified by default.

The Property task is used to set the properties, and a project can set a lot of properties. The properties are named and values. After the property is set, they can be referenced later. Set the attribute of a name to dist.name, its value is struts_demo, and use $ {dist.name} reference when used, indicates the string struts_deemo.

sets the properties of a name to src, and its value is a path, set with location. If the location content begins in / or / or d: / c: /, the absolute path indicates the absolute path, otherwise the relative path is indicated, and the basedir set in the Project.

Use PATH or CLASSPATH to set the path of the class, with the value set with the ID when referenced

Build the most common ANT built-in task:

MKDir: Create a directory, Dir = directory to create

Delete: Delete file or folder DIR = file or folder to delete

Javac: Compile the Java source file, the Java source file is placed in the folder specified by the srcdir, and the generated .class file is stored in the folder specified in the DESTDIR in accordance with the Package statement. Pay attention to the directory organization of the source file to be consistent with the package statement

WAR: Package the web application, Destfile specifies the file name generated after packaging, and WebXML specifies the web.xml file used. put all the files under the BaseDir directory in the package

In the above build.xml example, the property depends in the target of Target means that the target must be done before the Target is executed.

For example, DIST's Depends = Compile means that you must compile with Compile before packaging with a Dist. This first executes compile when executing a DIST

3. Run Ant:

Use ant.bat to run Ant directly, if you do not bring any parameters, Ant will search for build.xml files in the current path. If you are found, run the target specified by Project's default. You can also use parameters to choose build.xml files and Target to run

For the example above, assume that the directory of Build.xml is D: / struts_demo /, then the following three implementation mode is the same:

1. CD D: / struts_demo

Ant

2. Ant -buildfile d: /struts_demo/build.xml

3. Ant -buildfile d: /struts_demo/build.xml dist

If you do Ant -BuildFile D: /Struts_Demo/Build.xml Compile, execute compile target

About ANT use and build.xml file content, there is a detailed description in the Manual of Ant.

转载请注明原文地址:https://www.9cbs.com/read-75813.html

New Post(0)