Apache Ant Detailed

Source: Internet
Author: User
Tags echo message

This article is reproduced from: Apache ant usagefirst, what is AntThe following introduction from the Baidu Encyclopedia: Apache Ant, is a software compilation, testing, deployment, and other steps to connect to automate a tool, mostly for the Java environment in software development. Provided by the Apache Software Foundation.
When we developed an Android app using Eclipse, the project building tool that Eclipse uses is ant. User base: Most Java designs are used to manage large volumes of information, such as New York State's use of Apache Ant to manage America's largest youth program, which can update more than 250,000 students ' records in real time every day.
As long as you have used Linux system readers, you should know make this command. This command is often used when compiling the Linux kernel and some of the software's source programs. The make command is actually a project management tool, and ant implements the same functionality. The compiler tools like Make,gnumake and NMAKE have some flaws, but ant overcomes the pitfalls of these tools. Initially, ant developers developed cross-platform applications that were also designed to be better for ant based on these flaws.
second, Ant's AdvantagesAnt is a sub-project in the Apache Software Foundation Jakarta Directory, which has the following advantages: cross-platform.       Ant is written in the pure Java language, so it has a good cross-platform nature. Simple to operate. Ant is made up of a built-in task and optional tasks. The ant runtime requires an XML file (build file). Ant can execute various tasks by invoking the target tree. Each task implements a specific interface object. Because the ant build file is a file in XML format, it is easy to maintain and write, and the structure is clear. Ant can be integrated into the development environment. Because of its cross-platform and easy-to-operate nature, ant is easily integrated into a number of development environments.
Ant's Build file when you start a new project, you should first write the ant build file. The build file defines the build process and is used by everyone in the team development. The ant build file is named Build.xml by default, or it can take other names. Just pass the name as a parameter to ant at run time. The build file can be placed in any location. The general practice is to put it in the top-level directory of the project. This will keep the project concise and clear. The following is a typical project hierarchy. (1) src deposit file. (2) class holds the compiled file. (3) Lib holds third-party jar packages.  (4) Dist Storage package, post code. The ant build file is an XML file. Each build file defines a unique project (project Element). Many goals (target elements) can be defined under each project, and there can be dependencies between those goals. When such goals are implemented, the goals that they depend on are enforced. Multiple tasks can be defined in each target, and the task sequence to be executed is also defined in the target. Ant must invoke the defined task when building the target. The task defines the command that ant actually executes. The tasks in ant can be 3 classes. (1) Core tasks. The core task is Ant's own task. (2) Optional tasks. An optional task is a task from a third party, so an additional jar file is required. (3) User-defined tasks. User-defined tasks are tasks that users develop themselves.three, Ant's Basic use methodThe following is the basic use of ant: 1, the first need to download Apache ant from the official website, the address is: http://ant.apache.org/bindownload.cgi 2, after the download of compressed files directly extracted into the hard disk directory on the line, and then a Ant's environment variable, because we need to use the ant command at the command line, so we need to add the ant directory to the PATH environment variable, the following steps (WINDOWS10 system): (1) Right click on the desktop "This computer", select the properties, and then on the left side of the window that appears, select "Advanced System Settings" , select "Environment Variables" (2) in the pop-up window to add a variable named Ant_home to the "user variable" in the Environment Variable Configuration window, and the value is the ant extracted directory, as shown in the following figure:
Then find the path variable under User variables, and if not, add a path variable, and if so, add a new value directly to the path variable with the value "%ant_home%\bin". After you have configured the ANT environment variable in 2 steps above, after executing the ant-version command in CMD, the following illustration shows that the configuration was successful:
3. Start using Ant. Ant's core is the configuration file Build.xml, in the Build.xml file to configure the relevant tasks, the use of ant command can be automated, so we need to master the relevant ant configuration, here first create a new directory on the desktop testant, then enter the directory, create a new Build.xml file, and add In the following sections:

[HTML]View plain copy <?xml version= "1.0"?> <project name= "HelloWorld" default= "Test" basedir= "" > <target Name= "Test" > <echo message= "Test echo messsage, Basedir=${basedir}"/> </target> </pr oject> below the command line we enter the directory where Build.xml is located, and then execute the ant command, as shown in the following figure:
      below to explain the configuration of the Build.xml file above:       (1) Build.xml need to follow a certain format, so that the ant command to execute correctly, a build.xml file is the <project> tag as the root node of the,<project> node can specify the Name property, the project's names , Basedir represents the root directory of the project, default indicates the project defaults to the task name, and in this case the value test is called, and the default corresponding target is executed when the ant command is executed (if the ant command does not specify a task name).       (2) <project> the tag defines an <target> tag that represents a task the name attribute in the,<target> tag represents the task name, We can use the Ant + task name directly under the command line to perform a specific task, such as the example above, we can use the Ant Test command to execute the target task with the name value test, because we are in <project> The default property is configured as test in the tag, so you can run the test task with the ant command only at the command line.       (3) <echo> tags indicate output at the command line, similar to SYSTEM.OUT.PRINTLN () in Java, in <echo> Label, you can print the value of the property on the command line after you configure the message property. As shown in the previous figure [echo], the value of the message property configured for us.       (4) Use ${} to get the value of a variable, the name of the variable in curly brackets, as in the figure above, which is the value of Basedir, as in <project > tag we give the Basedir property a null value, so the value of Basedir defaults to the path where Build.xml is located.       The above example is the structure of a most basic Build.xml file, and the Build.xml file has a number of configuration items, the following are examples of several configuration items:

     <property> tags for declaring key-value pairs: [HTML]  view plain copy <?xml  Version= "1.0"?>   <project name= "HelloWorld"  default= "Test"  basedir= "" >        <target name= "Test-property" >            <property name= "name"  value= "Zhangsan"  />            <property name= "Age"  value= " />  "

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.