Ant's Build.xml detailed

Source: Internet
Author: User

Original address: http://www.cnblogs.com/clarkchen/archive/2011/03/10/1980194.html

Ant's Build.xml detailed

Keyword: ant build.xml

Ant's Concept
Some readers may not be connected to what ant is and can use it, but as long as the reader using the Linux system is used, the make command should be known. 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. When the ant developers started developing cross-platform applications, they also made a better design for ant based on these flaws.

Ant and Makefile
Makefile has some shortcomings, such as annoying tab problems that many people will encounter. The original ant developer repeatedly stressed that "I just put a space in front of the tab, so my command can't be executed." There are some tools to solve this problem to some extent, but there are a lot of other problems. Ant is different from the General Command-based tool, which is an extension of the Java class. Ant runs a file that requires an XML format that is not a shell command file. It is made up of a project, and a project can be divided into multiple target,target and subdivided into tasks, each of which is done through a Java class that implements a particular interface.

Benefits of Ant
Ant is a subproject in the Apache Software Foundation Jakarta Directory, which has the following advantages. Cross-platform. Ant is written in the Java language and is shown to have 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 ant builds files 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 DevelopmentAnt'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 keep the project concise and clear by placing it in the top-level directory of the project. 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. Can be elected pragmatically from a third-party task, so an additional jar file is required.
(3) User-defined tasks. User-defined tasks real users develop their own tasks.

1.<project> Label
Each build file corresponds to one project. <project> tag is the root tag of the build file. It can have multiple intrinsic properties, as shown in the code, and the meanings of each of its properties are as follows.
(1) default indicates the run target, and this property is required.
(2) Basedir represents the base directory of the project.
(3) name indicates the project name.
(4) Description represents the description of the project.
Each build file corresponds to a project, but a large project often contains a large number of sub-projects, each of which can have its own build file.

2.<target> Label
You can have one or more target tags under a project tab. A target tag can depend on other target tags. For example, there is a target for compiling a program, and another target for claiming an executable file. The file must be compiled before the executable can be generated, because the target of the executable file depends on the target of the compiler. All properties of target are as follows.
(1) name indicates that this attribute is required.
(2) depends indicates the target of dependence.
(3) If indicates that the property is set only when it is executed.
(4) unless indicates that the property does not execute when it is not set.
(5) Description represents the description of the project.
The ant's Depends property specifies the order in which target is executed. Ant executes each target sequentially, in the order in which the target appears in the Depends property. Before executing, you first need to execute the target that it depends on. The depends property of the target named run in the program is compile, and the depends property of the target named compile is prepare, so the order of these target executions is prepare->compile- >run. A target can only be executed once, even if there are multiple target dependencies on it. If there is no if or unless attribute, target is always executed.

3.<mkdir> Label
This tag is used to create a directory, which has a property dir is used to specify the name of the directory created, and its code is as follows: <mkdir dir= "${class.root}"/> created a directory with the above code, which has been specified by the previous property tag.

4.<jar> Label
This tag is used to generate a jar file with the following properties.
(1) DestFile represents the jar file name.
(2) basedir indicates the file name to be archived.
(3) includes indicates that the file mode is not archived.
(4) exchudes indicates the excluded file mode.

5. <javac> Tags
This tag is used to compile one or a set of Java files with the following properties:
(1). Srcdir represents the directory of the source program.
(2). Destdir represents the output directory of the class file.
(3). Include indicates the mode of the compiled file.
(4). Excludes indicates the mode of the excluded files.
(5). Classpath represents the classpath used.
(6). Debug represents the debug information contained.
(7). Optimize indicates whether optimization is used.
(8). Verbose indicates that detailed output information is provided.
(9). Fileonerror means that it stops automatically when it encounters an error.

6. <java> Tags
The tag is used to perform a compile-generated. class file with the following properties.
(1). classname represents the class name that will be executed.
(2). jar represents the jar file name that contains the class.
(3). The classpath used by the. Classpath.
(4). Fork indicates that the class is running in a new virtual machine.
(5). FailOnError indicates that it stops automatically when an error occurs.
(6). Output indicates the export file.
(7). Append indicates that the default file is appended or overwritten.

7.<delete> Label
This tag is used to delete a file or a set of files, go to properties as follows:
(1). File indicates the files to be deleted.
(2). Dir indicates the directory to be deleted.
(3). Includeemptydirs Indicates whether you want to delete the empty directory, and the default value is delete.
(4). FailOnError Indicates whether the default value is stopped automatically when an error is encountered.
(5). Verbose indicates whether the deleted file is listed, and the default value is not listed.

8.<copy> Label
This tag is used for a copy of a file or set of files with the following properties.
(1). file represents the source file.
(2). ToFile represents the target file.
(3). Todir represents the target directory.
(4). Overwrite indicates whether the destination file is overwritten, and the default value is no overwrite.
(5). Includeemptydirs indicates whether to copy an empty directory, and the default value is copy.
(6). FailOnError indicates that the default value is stop if the target is not found to stop automatically.
(7). Verbose Indicates whether detailed information is displayed and the default value is not displayed.

ant's Data type
In a build file, you often need to use data types in order to identify files or filegroups. Data types are included in the
Org.apache.tool.ant.types in the package. The following is a brief introduction to some common data types in the build file.

1. Argument type
Programs that are called by the ant build file can pass command-line arguments to them through the <arg> element, such as apply,exec and Java tasks, which accept nested <arg> elements, and can specify parameters for their own procedure calls. The following are all the properties of <arg>.
(1). Values is a command parameter. This property is used if the parameter type has a space but wants to use it as a single value.
(2). file represents the filename of a parameter. In the build file, this file name is relative to the current working directory.
(3). Line denotes multiple parameter lists separated by spaces.
(4). path represents the path.

2.ervironment Type
External commands or programs called by the ant build file the,<env> element has developed which environment variables to pass to the executing system command,<env> element can accept the following properties.
(1). File indicates that the environment variable is worth the file name. This file name is converted to an absolute path of bits.
(2). path represents the path to the environment variable. Ant will convert it to a local convention.
(3). value represents a direct variable of an environment variable.
(4). Key indicates the name of the environment variable.
Note that file path or value can only take one.

3.filelist TypeFilelist is a data type that supports named file lists, and files contained in a Filelist type are not necessarily files that exist. The following are all of its properties.
(1). Dir is the directory used to calculate the absolute file name.
(2). Files is a comma-delimited list of file names.
(3). refID is a reference to a <filelist> that is defined somewhere.
Note that Dir and files are necessary unless REFID is specified (in this case, dir and files are not allowed).

4.fileset Type
The Fileset data type defines a set of files and is typically represented as a <fileset> element. However, many ant tasks are built into implicit fileset, which means they support all fileset attributes and nested elements. The following is a list of properties for Fileset.
(1). dir represents the base directory for Fileset.
(2). If the value of CaseSensitive is False, Fileset is not case-sensitive when matching the file name, and its default value is true.
(3). Defaultexcludes is used to determine whether to use the default exclusion mode, which is true by default.
(4). Excludes is a comma-delimited list of file patterns that need to be dispatched.
(5). Excludesfile indicates the file name of a file containing one exclusion pattern per line.
(6). Includes is a comma-delimited list of file patterns that need to be included.
(7). Includesfile indicates that each row includes a file name that contains a pattern.

5.patternset Type
Fileset is a grouping of files, and Patternset is a grouping of patterns, and they are closely related concepts. <patternset> supports 4 properties: Includes Excludex includexfile and Excludesfile, same as Fileset. Patternset also allows the following nested elements: Include,exclude,includefile and Excludesfile.

6.filterset Type
Filterset defines a set of filters that will replace the text of a file when the file is moved or copied.
The main attributes are as follows:
(1). Begintoken represents the token that the nested filter searches for, which is the string that identifies its start.
(2). Endtoken represents a token that is searched by a nested filter This is the string that identifies its end.
(3). ID is the unique identifier of the filter.
(4). refID is a reference to a filter defined somewhere in the build file.

7.Path Type
The path element is used to represent a classpath, but it can also be used to represent other paths. When using Zuoyi attributes, the items in the path are separated by semicolons or colons. At the time of construction, this delimiter replaces all the path separators in the current platform with the following properties.
(1). location represents a file or directory. Ant expands this to an absolute path internally.
(2). refID is a reference to a path defined somewhere in the current build file.
(3). path represents a list of files or path names.

8.mapper Type
The mapper type defines the relationship between a set of input files and a set of output files, with the following properties.
(1). classname represents the class name that implements the Mapper class. Used to create custom mapper when the built-in mapper do not meet the requirements.
(2). classpath represents the type path used when looking for a custom mapper.
(3). Classpathref is a reference to a class path defined somewhere.
(4). The meaning of the From property depends on the mapper used.
(5). The meaning of the to property depends on the mapper used.
(6). The value of the Type property is one of the Identity,flatten glob merge RegExp, which defines the type of built-in mapper to use.

Ant's Run
After installing Ant and Configuring the path, switch to the directory in the build file on the command line and enter the ant command to run ant. If no parameters are specified, ant queries the Build.xml file in the current directory. Use this file as the build file if it is found. If the –find option is used, ANT will find the build file in the parent directory until it reaches the file system with the directory. If the name of the build file is not Build.xml, you can use –buildfile file when Ant is running, where file specifies the name of the build file to use, as shown in the following example:
Ant shows that the build file that represents the current directory is the default target for Build.xml to run Ant.
Ant–buildfile Test.xml run ant with the Test.xml file in the current directory, execute the default target

Ant's Build.xml detailed

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.