Ant build. xml keywords: ant build. xml
Ant Concept Some readers may not connect to ant and can use it. However, as long as they use the Linux system, they should know the make command. This command is often used when compiling the Linux kernel and some software source programs. The make command is actually a project management tool, and ant Implements similar functions. Compilation tools such as make, gnumake, and nmake have some defects, but ant has overcome these defects. At first, ant developers made better designs for ant when developing cross-platform applications based on these defects.
Ant and makefile Makefile has some shortcomings, such as the annoying tab problem that many people encounter. 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 solve 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 composed of a project, and a project can be divided into multiple targets. The target is subdivided into many tasks, and each task is completed by a Java class that implements a specific 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 developmentAnt build file when you start 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. By default, the ant build file is named build. XML, and other names can also be used. 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, so that the project can be concise 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 dependency target.
(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.
The depends attribute of ant 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, you must first execute the target on which it depends. In the program, the depends attribute of target named run is compile, And the depends attribute of target named compile is prepare. Therefore, 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} "/> A directory is created using the above Code, which has been specified by the previous property tag.
4. <jar> label 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> label 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. The 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. Data types are included in
Org. Apache. tool. Ant. types package. The following describes some common data types in the build file.
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 tasks can accept nested <Arg> elements, you can specify parameters for each process call. The following are all attributes of <Arg>.
(1). values is a command parameter. This attribute is used if the parameter has a space and 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 that the environment variable is worth the file name. 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 typeFilelist 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 objects, while patternset is a group of patterns. They are closely related concepts. <Patternset> supports four attributes: Includes excludex includexfile and excludesfile, which are the 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 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 being used as an attribute, 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 After ant is installed and the path is configured, 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 queries build in the current directory. XML file. If yes, use this file as the Build File. If the-find option is used, ant will find the build file in the parent directory until it reaches the file system and directory. If the name of the build file is not build. XML, you can use-buildfile when ant runs. Here, 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 under the current directory to run ant, execute the default target Article Source: http://www.cnblogs.com/clarkchen/archive/2011/03/10/1980194.html
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.