(Abstract) * Java * ANT-build.xml file details

Source: Internet
Author: User

 

ANT-build.xml documentation


Http://www.diybl.com/course/3_program/java/javajs/20081122/152210.html #


Ant Concept
Some readers may not understand what ant is and can use it in the input, but they should know the make command as long as they use the Linux system. When editing
This command is often used when translating the Linux kernel and some software source programs. The make command is actually a project management tool, and ant Implements similar functions. Image
Make, gnumake, and nmake compilation tools all have some defects, but ant has overcome these defects. At first, when ant developers develop cross-platform applications
Ant is better designed based on these defects.

Ant and makefile
Makefile has some shortcomings. For example, many people may encounter
Annoying tab issues. At first, ant developers repeatedly stressed that "I only added a space in front of the tab, so my command cannot be executed ". Some tools have solved this problem to some extent,
But there are still many other problems. Ant is different from common command-based tools. It is an extension of Java classes. The XML file required for ant running is not a shell command file. It is
A project can be divided into multiple targets, and the target can be subdivided into multiple tasks. Each task is implemented through an Implementation feature.
Set the Java class of the interface.

Ant advantages

Ant is a sub-project in the Jakarta directory of the Apache Software Foundation. It has the following advantages.
Cross-platform. Ant is written in Java and has good cross-platform performance.
Easy to operate. Ant consists of a built-in task and an optional task. An XML file (Build File) is required for ant runtime ).
Ant can execute various tasks by calling the target tree. Each task implements a specific interface object. Ant builds files in XML format, so it is easy to maintain and write, and the structure is very clear.
Ant can be integrated into the development environment. Ant is easy to integrate into some development environments due to its cross-platform nature and simple operation.

Ant development

Ant Build File
When
When starting a new project, you should first compile the ant build file. The build file defines the build process and is used by everyone in the Team's development. The ant build file is named build. xml by default.
You can use another name. The name is passed to ant as a parameter during running. Build files can be placed anywhere. The general practice is to put it in the top-level directory of the project to keep the project simple.
Clean and clear. The following is a typical project hierarchy.
(1) SRC stores files.
(2) Class stores compiled files.
(3) lib stores third-party jar packages.
(4) store and package the DIST and release the code later.
The ant build file is an XML file. Each build file defines a unique project (project element ). Each project can define many targets (target elements), which can be dependent on each other. When executing such goals, you need to execute the goals they depend on.
Multiple tasks can be defined in each target, and the task sequence to be executed is also defined in the target. Ant must call the defined task when building the target. The task defines the commands actually executed by ant. The task in ant can be of three types.
(1) core tasks. The core task is the task that comes with ant.
(2) Optional tasks. Optional tasks come from third-party tasks. Therefore, an additional JAR file is required.
(3) custom tasks. A task developed by a user-defined task utility.

1. <project> label
Each build file corresponds to a project. <Project> Create the root tag of the file during tag creation. It can have multiple internal attributes, as shown in the Code. The meanings of each attribute are as follows.
(1) default indicates the default running target. This attribute is required.
(2) basedir indicates the baseline directory of the project.
(3) name indicates the project name.
(4) description indicates 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 sub-project can have its own build file.

2. <target> tag
A project tag can contain one or more target tags. A target tag can depend on other target tags.
For example, one target is used to compile the program, and the other target is used to claim executable files. Compile the file before generating an executable file, because the target of the executable file depends on the target of the Compilation Program. All properties of target are as follows.
(1). name indicates that this attribute is required.
(2). Depends indicates the target of the dependency.
(3) If indicates that it is executed only when the attribute is set.
(4) unless: it is executed only when the property is not set.
(5) description indicates the description of the project.
Ant
The depends attribute specifies the execution sequence of the target. Ant will execute each target in sequence according to the Appearance Order of the target in the depends attribute. Before execution
Target on which it depends. The depends attribute compile of the target named run in the program, and
The depends attribute is prepare, so the execution sequence of these targets is prepare-> compile-> Run.
A target can only be executed once, even if multiple targets depend on it. If there is no if or unless attribute, the target will always be executed.

3. <mkdir> label
This label is used to create a directory. It has the Dir attribute to specify the name of the Created directory. The Code is as follows:
<Mkdir dir = "$ {class. Root}"/>
The code above creates a directory that has been specified by the previous property tag.

4 <jar> labels
This label is used to generate a jar file. Its Attributes are as follows.
(1) destfile indicates the JAR file name.
(2) basedir indicates the file name to be archived.
(3) des indicates the file mode for non-archiving.
(4) exchudes indicates the excluded file mode.

5. <javac tag>
This label is used to compile one or more java files. Its Attributes are as follows.
(1). srcdir indicates the directory of the source program.
(2). destdir indicates the output directory of the class file.
(3). Include indicates the mode of the compiled file.
(4). Excludes indicates the mode of excluded files.
(5). classpath indicates the class path used.
(6). debug indicates the debugging information contained.
(7). Optimize indicates whether to use optimization.
(8). verbose indicates providing detailed output information.
(9). fileonerror indicates that it is automatically stopped when an error occurs.

6. <Java> labels
This label is used to execute the compiled. Class file. Its Attributes are as follows.
(1). classname indicates the name of the class to be executed.
(2). jar indicates the JAR file name containing the class.
(3). classpath indicates the class path used.
(4). Fork indicates that the class is run in a new virtual machine.
(5). failonerror indicates automatic stop when an error occurs.
(6). Output indicates the output file.
(7). append indicates appending or overwriting the default file.

7. <Delete> tag
This label is used to delete a file or a group of files. Its Attributes are as follows.
(1)/file indicates the file to be deleted.
(2). dir indicates the directory to be deleted.
(3). includeemptydirs indicates whether to delete an empty directory. The default value is Delete.
(4). failonerror indicates whether to stop an error. The default value is automatic stop.
(5). verbose indicates whether to list the deleted files. The default value is not listed.

8. <copy> label
This label is used to copy a file or file set. Its Attributes are as follows.
(1). file indicates the source file.
(2). tofile indicates the target file.
(3). todir indicates the target directory.
(4) overwrite indicates whether to overwrite the target file. The default value is not overwrite.
(5). includeemptydirs indicates whether to copy an empty directory. The default value is copy.
(6). failonerror indicates whether to stop automatically if the target is not found. The default value is stop.
(7). verbose indicates whether to display details. The default value is not.

Ant Data Type
Data types are often used in build files to identify files or file groups. The data type is included in the org. Apache. tool. Ant. types package. The following is a brief introduction to some common data types in build files.

1. argument type
Programs called by files built by ant can pass command line parameters to them through the <Arg> element, such as apply, exec, and Java

The nested <Arg> element can be accepted, and parameters can be specified for each process call. The following are all attributes of <Arg>.
(1). values is a command parameter. This attribute is used if the parameter contains spaces but you want to use it as a separate value.
(2). file indicates the file name of a parameter. In the build file, the file name is relative to the current working directory.
(3). line indicates multiple parameter lists separated by spaces.
(4). Path indicates the path.

2. ervironment type
The <env> element specifies the environment variables to be passed to the system commands being executed. The <env> element can accept the following attributes.
(1). file indicates the file name of the environment variable value. The file name must be converted to an absolute path.
(2). Path indicates the path of the environment variable. Ant converts it to a local convention.
(3). value indicates a direct variable of the environment variable.
(4). Key indicates the environment variable name.
Note that only one file path or value can be used.

3. filelist type
Filelist is a data type that supports named file lists. files contained in a filelist type are not necessarily existing files. All its attributes are listed below.
(1). DIR is the directory used to calculate the absolute file name.
(2). Files is a list of file names separated by commas.
(3). refID is a reference to a specified <filelist>.
Note that both 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 group of files, which are usually expressed as <fileset> elements. However, many ant tasks are built into an implicit fileset, which means they support all fileset attributes and nested elements. The following is a list of fileset attributes.
(1). dir indicates the base Directory of fileset.
(2) If the casesensitive value is false, when the file name is matched, fileset is not case-sensitive and its default value is true.
(3) defaultexcludes is used to determine whether to use the default exclusion mode. The default value is true.
(4). Excludes is a comma-separated list of file modes to be dispatched.
(5). excludesfile indicates the file name of each row containing an exclusive mode file.
(6). separated by commas (,). List of file modes to be included.
(7). includesfile indicates that each line contains a file name containing the mode.

5. patternset type
Fileset
Is a group of files, while patternset is a group of patterns. They are closely related concepts. <Patternset> four attributes are supported: supported des
Excludex export dexfile and excludesfile, which are the same as fileset. Patternset
The following nested elements are also allowed: Include, exclude, includefile, and excludesfile.

6. filterset type
Filterset defines a set of filters that replace the text of a file when it is moved or copied.
The main attributes are as follows:
(1). begintoken indicates the mark searched by the nested filter, which is the string that marks its start.
(2). endtoken indicates the mark searched by the nested filter. This is the string that marks its end.
(3). ID is the unique identifier of the filter.
(4). refID is a reference to defining a filter somewhere in the build file.

7. Path type
The path element is used to represent a class path, but it can also be used to represent other paths. When used as several attributes, the items in the path are separated by semicolons or colons. During construction, this separator replaces all path delimiters on the current platform. Its Attributes are as follows.
(1). Location indicates a file or directory. Ant expands this field to an absolute path.
(2). refID is a reference to a path defined somewhere in the current build file.
(3). Path indicates a list of files or path names.

8. mapper type
The Mapper type defines the relationship between a group of input files and a group of output files. Its Attributes are as follows.
(1). classname indicates the class name that implements the ER er Class. When the built-in mapper does not meet the requirements, it is used to create a custom mapper.
(2). classpath indicates the type path used to find a custom mapper.
(3). classpathref is a reference to a class path defined in a certain place.
(4). The meaning of the from attribute depends on the ER er used.
(5) The meaning of the. To attribute depends on the ER er used.
(6) The value of the type attribute is "Identity" and "flatten glob merge Regexp". It defines the built-in mapper type.

Ant running
Security
After installing ant and configuring the path, switch to the directory of the build file in the command line and enter the ant command to run ant. If no parameter is specified, ant will query it in the current directory.
Build. xml file. If yes, use this file as the Build File. If the-find option is used, ant
The Build File is located in the parent directory until it reaches the root directory of the file system. If the name of the build file is not build. XML, you can use
-Buildfile file: file specifies the name of the build file to be used, for example:
Ant indicates that the build file in the current directory is the default target for ant running in build. xml.
Ant-buildfile test. xml
Use the test. xml file in the current directory to run ant and execute the default 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.