What is "ant" ant?

Source: Internet
Author: User
Tags xml example
Ant Official Website: http://ant.apache.org/

Ant Introduction
Ant is a sub-project of the Apache project and a Java-based build tool. Ant is similar to the Make tool, but it does not have the disadvantages of the traditional make tool. Traditional make can only be used on a certain platform. ant itself is implemented using Java classes. The configuration file of the project to be built is described in XML format, which facilitates multi-platform compilation, it is ideal for building large projects.

1. install and configure ant:
Ant can be downloaded from the http://ant.apache.org. The latest version is 1.5.3 (this is an introduction to 1.5.2 ). Decompress the package directly after the download. The directory is as follows:
Ant
+ -- Bin // contains launcher scripts
|
+ -- Lib // contains ant jars plus necessary Dependencies
|
+ -- Docs // contains documentation
| + -- ANT2 // a brief description of ANT2 requirements
|
| + -- Images // various logos for HTML documentation
|
| + -- Manual // ant documentation (A must read)
|
+ -- Etc
Environment variables to be set:
Ant_home: ant installation directory
Java_home: JDK installation directory
Path: add the % ant_home %/bin directory to the PATH variable to run ant directly from the command line.

Assume that ant is installed in C:/ant JDK and D:/j2sdk1.4.0 is installed.
Run the following command on the command line:
Set ant_home = C:/ant
Set java_home = D:/j2sdk1.4.0
Set Path = % PATH %; C:/ANT/bin

When working at the Win2000 command prompt, the above settings must be performed each time. After exiting the command prompt, the values of these variables will be restored to their original form. To avoid these troubles, you can set them in Control Panel/system/advanced/environment variables.

After the preceding settings are complete, ant can be used.

2. Create the project description file build. xml
It is very convenient to compile a large project with ant. Each project corresponds to a build. xml file, which contains the path information and tasks related to the project. The following is an example of build. xml:


<project name="logon" basedir="." default="dist">

<property name="dist.name" value="struts_demo"/>
<property name="src" location="src"/>
<property name="build" location="WEB-INF/classes"/>
<property name="dist" location="D:/tomcat/webapps/dist"/>

<!-- Build working classpath -->
<path id="project.class.path">
<pathelement path ="WEB-INF/lib/struts.jar"/>
<pathelement path ="WEB-INF/classes"/>
<pathelement path ="${classpath}"/>
</path>

<target name="init">
<tstamp/>
<!-- Create the build directory structure used by compile -->
<delete dir="${dist}"/>
</target>

<target name="compile" depends="init">
<javac srcdir="${src}" destdir="${build}">
<classpath refid="project.class.path"/>
</javac>
</target>

<!-- Build binary distribution -->
<target name="dist" depends="compile"
description="Create binary distribution">
<mkdir dir="${dist}"/>
<war destfile="${dist}/${dist.name}.war"
webxml="WEB-INF/web.xml">
<lib dir="WEB-INF/lib"/>
<classes dir="WEB-INF/classes"/>
<fileset dir="${basedir}"/>
</war>
</target>

</project>

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 one project and at least one target. Target contains the task element, which is a piece of executable code. Each task element has an ID attribute for reference in files. Ant has a built-in task set available for use, such as the property, javac, and war used in the above file, to complete setting properties, compiling, and packaging tasks respectively. Of course, you can also write your own tasks if needed.

The root element of build. XML is progject, which has three attributes: Name default basedir, Where default is required. Name indicates the project name. basedir indicates the base path of the project and "." indicates the path where build. XML is located. Default indicates the default target. If no target is specified during ant running, the target specified by default is used.

A property task is used to set attributes. A project can set many attributes, including names and values. attributes can be referenced later.

<Property name = "Dist. name "value =" struts_demo "/> set a name to Dist. name attribute, whose value is struts_demo. $ {Dist will be used later. name} refers to the string struts_demo.

<Property name = "src" location = "src"/> set a property named SRC. Its value is a path and is set with location. If the location content starts with // or D:/C:/, it indicates the absolute path. Otherwise, it indicates the relative path, relative to the basedir set in the project.

You can use path or classpath to set the class path, and use the value set by ID for later reference.

The most common ant built-in tasks for building projects:
Mkdir: create directory, Dir = directory to be created
Delete: delete a file or folder dir = file or folder to be deleted
Javac: Compile the Java source file. Place the Java source file in the folder specified by srcdir. The generated. Class file organizes directories according to the package statement and stores them in the folder specified by destdir. Note that the directory organization of the source file must 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. <Fileset dir = "$ {basedir}"/> put all the files in the basedir directory in the package.

In the above Build. xml example, the attribute depends in target indicates the target that must be done before the target is executed,
For example, depends = compile of DIST indicates that compile must be used before packaging with Dist. In this way, compile is first executed when dist is executed.

3. Run ant:
Use ant. BAT can directly run ant. Without any parameters, ant will search for build in the current path. XML file. If it is found, run the target specified by default of the project. you can also select build with parameters. XML file and target to be run

For the above example, assuming that the directory where build. XML is located is D:/struts_demo/, the following three execution methods have the same effect:
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 execute ant-buildfile D:/struts_demo/build. xml compile, execute compile target

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.