Implement automatic building and deployment of Java projects with Ant __java

Source: Internet
Author: User
Tags echo message parent directory

Http://www.blogjava.net/amigoxie/archive/2007/11/09/159413.html


Original address: http://tech.it168.com/j/2007-11-09/200711091344781.shtml


Ant is a Cross-platform component tool under the Apache Foundation, which enables the automatic construction and deployment of projects. In this article, the reader is primarily familiar with how to apply ant to a Java project to simplify build and deploy operations.

one. Installation and Configuration

Download Address: http://ant.apache.org/, downloaded in this article is version 1.7.0. Extract to a directory (for example, E: "apache-ant-1.7.0") to use.

Add system environment variable: Ant_home, which points to the root of ANT decompression, this is e: "apache-ant-1.7.0."

Once installed and configured, the reader can test whether ant is available, first into Ant's Bin directory, Run command ant–version, and if the installation and configuration succeeds, the ant version information is displayed, as shown in the following illustration:

As you can see, when the reader runs Ant's command, it needs to go into Ant's Bin directory and how to get the system to automatically find Ant. The reader is then required to add ant's Bin directory to the System environment variable path. Once the setup is complete, we can enter the ant command in any directory (such as the C: "Documents and Settings" Amigoxie directory) to get the result of the command running.

two. Ant's key element

Ant's widget files are written based on XML, and the default name is Build.xml. To get a better idea of ant, write a simple ant program here to show Ant's functionality and give the reader a rudimentary understanding of ant. First, create a build.xml file under E disk, which reads as follows:
? XML version= "1.0"?>
< project name = "HelloWorld" >
< target name = "SayHelloWorld" >
< echo message = "Hello,amigo"/>
</Target >
</Project >

The reader can go to e disk and run Ant SayHelloWorld to see the following results:

Where SayHelloWorld is the name of the task that needs to be performed. If the file name is not Build.xml and is hello.xml, when the reader runs the same command, the following error occurs in the command window:

Buildfile:build.xml does not exist!

Build failed

The error prompted by the above command shows that the ant command is looking for the Build.xml file by default. If the file name is Hello.xml, the reader will also need to make a slight change to the order, instead: Ant–f hello.xml sayhelloworld, ant–buildfile hello.xml SayHelloWorld or Ant–file Hello.xml SayHelloWorld.

Let's begin by explaining the focus of this section to your audience: key elements of Ant project, target, property, and task.

1. Project Elements

The project element is the root element of the ant widget file, and the ant widget file should contain at least one project element, or an error will occur. Under each project element, you can include multiple target elements. Next, show the reader the properties of the project element.

1) Name Property

Use to specify the name of a project element.

2) Default property

Specifies the name of the target to execute when project defaults.

3) Basedir Property

is used to specify the location of the base path. When this property is not specified, use the attached directory of ant's widget file as the base directory.

Here's a simple example to illustrate the use of project elements. Modify e: "Build.xml file, the revised contents are as follows: XML version= "1.0"?>
< project name = "Projectstudy" default = "Saybasedir" Basedir = "E:" apache-ant-1.7.0 ">
< target name = "Saybasedir" >
< echo message = "The Base dir: ${basedir}"/>
</Target >
</Project >

As we can see from the above, the value of the default property is defined here as Saybasedir, that is, when the ant command is run, the target saybasedir is executed by default when the target is executed, and the value of the Basedir property is also defined as E : "apache-ant-1.7.0, run the ant command after entering e disk, and you can see the results of the run, as shown in the following illustration:

Because the value of the Basedir is set, the value of the Basedir property becomes the value that the reader sets. The reader can remove the Basedir attribute of the project element and then run ant to see the results, at which point the Basedir value becomes E: "The parent directory of the ant component file."

Sometimes, readers may have the need to get all the target names under a project, and the reader can do that by adding-proecthelp to the ant command. For example, we run Ant–projecthelp for the above example, and the output is as follows:

Buildfile:build.xml

Main Targets:

Other targets:

Saybasedir

Default Target:saybasedir

2. Target element

It is the basic execution unit of ant, which can contain one or more specific tasks. Multiple target can have interdependencies. It has the following properties:

1) Name Property

Specifies the name of the target element, which is unique within a project element. We can specify a target by specifying the name of the target element.

2) Depends property

Used to describe the dependencies between target and if there is a dependency on multiple target, the "," interval is required. Ant executes each target sequentially according to the order in which target appears in the depends attribute. The target being relied on will execute first.

3) If property

Used to verify that the specified property exists, and if it does not exist, Target will not be executed.

4) Unless property

The function of this property is the opposite of the function of the If property, and it is also used to verify that the specified property exists, and if it does not exist, the target will be executed.

5) Description Property

This property is a short description and description of the target feature.

Here's a guide to the reader to see an example of a comprehensive use of each attribute. Modify e: "Build.xml file, the revised content is as follows:
? XML version= "1.0"?>
< project name = "Targetstudy" >
< target name = "Targeta" if = "ant.java.version" >
< echo message = "Java Version: ${ant.java.version}"/>
</Target >
< target name = "Targetb" depends = "targeta" unless = "amigo" >
< description >
A depend example!
</Description >
< echo message = "The Base dir: ${basedir}"/>
</Target >
</Project >

When you run Ant Targetb after entering e disk, you can see the results of the operation as shown in the following illustration:

Related Article

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.