Ant is a Java-based build tool. Theoretically, it is somewhat similar to make in (Unix) C, but without the bug of make.
Now that we've got make, Gnumake, NMAKE, jam, and other build tools, why do we need a new build tool? The limitations and inconveniences of these tools cannot be tolerated because Ant's original authors develop software on multiple (hardware) platforms. Tools like make are inherently shell-based: They compute dependencies and then execute commands (these commands are not much different from the commands you hit on the command line). This means that you can easily extend the tool by using OS-specific or writing new (command) programs; however, it also means that you are restricting yourself to a specific OS, or specific OS type, such as UNIX.
Makefile is also very hateful. Anyone who has ever used them has had a nasty tab problem. Ant's original author often asks himself: "Is my command not executed just because there is a space in front of my tab?!! ”。 The Jam-like tool handles this kind of problem well, but the user must remember and use a new format.
Ant is different. Unlike extended mode based on shell commands, Ant is extended with Java classes. (users) Do not have to write shell commands, the configuration file is XML-based, by invoking the target tree, you can execute various tasks. Each task is run by implementing an object that implements a specific task interface. (If you have no idea about ant, you may not be able to understand this section, it doesn't matter, the target,task will be described in detail later.) If you don't have a lot of time, you can even skip this section, and then come back and look at the introduction, then you'll understand. Also, if you are unfamiliar with tools such as make, the following introduction does not use the concept of make. )
It must be admitted that in doing so, you lose some unique expressive power when you construct a shell command. such as ' find '. -name foo-exec rm {} ', but it gives you the ability to cross-platform-you can work from anywhere. If you really need to execute some shell commands, Ant has a <exec> task that allows you to execute commands on a specific OS.
Ant's Concept
When a code project is large, every recompile, package, test, and so on will become very complex and repetitive, so there are make scripts in C to help with the batch completion of these tasks. The application in Java is platform-agnostic, and of course does not use the platform-related make script to complete these batch tasks, ant itself is such a process scripting engine, used to automate the calling program to complete the project compilation, packaging, testing and so on. In addition to being platform-agnostic based on Java, the script is XML-based, and is better maintained than the make script.
Each ant script (default called Build.xml) sets a series of tasks (target): For example, for a generic project, the following tasks may be required.
* Task 1:usage Print help information for this script (default)
* Task 2:clean <--init empty initialization environment
* Task 3:javadoc <--build <--init build Javadoc
* Task 4:jar <--build <--init build jar
* Task 5:all <--jar + Javadoc <--build <--init complete all of the above tasks: Jar Javadoc
Multiple tasks often involve a dependency relationship: for example, the entire Application Package task (jar) is dependent on the compilation task (build), and the compilation task depends on the Environment initialization task (INIT) and so on.
Note: I see the naming of many projects in the ant script is basically consistent, such as: Compile is generally called build or compile; Packaging is generally called a jar or war; The resulting document is generally named Javadoc or Javadocs; perform all tasks all. In each task, ant will invoke some external applications based on the configuration and execute them with the corresponding parameters. Although Ant can invoke a very rich range of external applications, but in fact the most commonly used is 2, 3: such as Javac Javadoc jar.
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 Development
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 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
The tag is used to create a directory that has a property, dir, to specify the name of the directory created, with the following code:
<mkdir dir= "${class.root}"/>
The above code creates a directory that 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 label >
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 whose properties are as follows.
(1)/file represents the file 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. The data type is contained in the Org.apache.tool.ant.types package. The following mirrors briefly describe some of the commonly used data types in the build file.
1. Argument type
Programs that are called by the ant build file can pass command-line arguments to it through the <arg> element, such as Apply,exec and Java.
can accept nested <arg> elements and specify parameters for each procedure call. The following are all the properties of <arg>.
(1). Values is a command parameter. Use this property if there are spaces in the argument, but you want to take 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 name that represents the value of the environment variable. 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 type
Filelist 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 used as several properties, 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 installation
After unpacking, add the path to the ant bin in the system executable path, for example, you can add the following configuration to the Gnu/linux on the/etc/profile:
Export Ant_home=/home/ant
Export java_home=/usr/java/j2sdk1.4.1
Export path= $PATH: $JAVA _home/bin: $ANT _home/bin
After Ant is executed, if you do not specify a profile ant will default to build.xml this profile, and according to the configuration file to perform tasks, the default task settings can point to the most commonly used tasks, such as: build, or point to print help information: usage, Tells the user that there are those scripting options that can be used.
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 root of the file system. 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 to execute the default target
Ant tool ant installation and configuration ant role