Ant Concept
Some readers may not be connected to Ant or can use it, but as long as they use Linux, they should know
Make command. This command is often used when compiling the Linux kernel and some software source programs. The Make command is actually
Is a project management tool, and Ant Implements similar functions. Compilation tools such as make, gnumake, and nmake all have
But Ant has overcome the defects of these tools. At first, when Ant developers develop cross-platform applications
Ant is designed 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 the 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.
Exhibition. The XML file required for Ant running is not a Shell command file. It is composed of a Project, and
The Project can be divided into multiple targets, and the target can be subdivided into multiple tasks.
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. Because Ant builds a file
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's cross-platform and simple operation features make it easy to integrate into some development environment
Go.
Ant development
Ant 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 developed by the team.
. By default, the Ant build file is named build. xml, and other names can also be used. However
Pass the name as a parameter to Ant. 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 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 ). It can be set for each project
Target elements, which can be dependent on each other. When executing such goals, you need to execute
The target of the dependency.
Multiple tasks can be defined in each target, and the task sequence to be executed is also defined in the target. Ant must be called when building the target
Use the defined task. 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
Build your own files.
2. <target> tag
A project tag can contain one or more target tags. A target tag can depend on other target tags. Example
For example, one target is used to compile the program, and the other target is used to claim executable files. Before being generated into an executable file
Compile the file first 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.
The depends attribute of Ant specifies the execution sequence of the target. Ant will execute the command in sequence according to the Appearance Order of the target in the depends attribute.
Each target. Before execution, you must first execute the target on which it depends. Target named run in the program
The depends attribute is compile, And the depends attribute of target named compile is prepare.
The row order 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
Will 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. 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 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 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 executing system.
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 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
Implicit fileset, which indicates that 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. Its default value is
The 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 consistent with 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 a token attribute
Are separated by semicolons or colons. During construction, this separator replaces all path separators on the current platform.
Some 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, flatten glob merge regexp, one of which defines
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 the build. xml file in the current directory. Use this file
Build a 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 file when Ant is running.
File specifies the name of the build file to be used. The example is as follows:
Ant
The following shows 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