What is Ant?
Apache Ant is a Java-based generation tool. According to original founder James Duncan Davidson, the name of this tool is the acronym of another neat tool.
What Ant does:
Generation tools are used in software development to convert source code and other input files into executable files (and possibly to installable product image formats). As the application generation process becomes more complex, it becomes even more important to ensure that the exact same generation steps are used during each generation, while achieving as much automation as possible to generate consistent generation versions in a timely manner.
Advantages of Ant:
Ant is a Java-based build tool. In theory, it is somewhat similar to make in (Unix)C, but has no flaws in make. The latest version is: Ant 1.8.4. Since we already have make, gnumake, nmake, jam and other build tools, why do we need a new build tool? Because Ant's original author cannot tolerate the limitations and inconveniences of these tools when developing software on multiple (hardware) platforms. Make-like tools are essentially shell-based: they calculate dependencies and then execute commands (these commands are not much different from the commands you type 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 you limit yourself to specific OS, or specific OS types, such as Unix.
Ant is different. Unlike the extension mode based on shell commands, Ant uses Java classes to extend. (User) There is no need to write shell commands. The configuration file is based on XML. Various tasks can be executed by calling the target tree. Each task is run by an object that implements a specific Task interface.
Ant defines the dependencies between generated files, which uses cross-platform Java classes. With Ant, you can write a single generator file that operates consistently on any Java platform (because Ant itself is implemented in the Java language), which is the biggest advantage of Ant
Ant generates file analysis:
Ant does not define its own custom syntax; instead, its generated files are written in XML. There is a set of predefined XML elements that Ant can understand, and new elements can also be defined to extend Ant's functionality. Each build file consists of a single project element, which in turn contains one or more target elements. A target is a defined step in the generation process, which performs any number of operations, such as compiling a set of source files. And these operations themselves are performed by other dedicated task tags and then these tasks will be grouped into individual target elements as needed. All operations required for a single generation process can be placed in a single target element, but that reduces flexibility. It is usually preferable to divide those operations into logical generation steps, each step contained in its own target element. This allows individual parts of the overall generation process to be performed without necessarily executing other parts.
For example, by calling only certain targets, you can compile the source code of your project without having to create an installable project file. The top-level project element needs to contain a default attribute, which specifies the target to be executed if Ant is called. Then you need to use the target element to define the target itself.
Here is a basic generated file:
<?xml version="1.0"?> <project default="init"> <target name="init"> </target> </project>
Basic usage of Ant:
1. Configure environment variables:
ANT_HOME: C:/ant-1.8 -----> Ant's installation/decompression directory path
Append to PATH: C:/ant-1.8/bin ----->BIN directory path in Ant
2. Confirm whether the environment variable configuration successfully opens the CMD window, and then enter the command: ant:
See the following display:
Since Ant needs to have a build.xml file by default when building, there is the above prompt, which means that Ant's environment has been successfully configured.
3. Create a folder called HelloWorld using Ant:
First, you need to edit build.xml:
<?xml version="1.0"?> <project default="init"> <target name="init"> <span style="color:#FF0000;"><mkdir dir="HelloWorld"></span> </target> </project>
Then switch to the directory where the build.xml file is located, enter ant, and if there is a prompt as follows, the folder creation will be successful:
(the init part is equivalent to the output of the log)
4. You can also use ant to create multi-level nested file directories that only need to be modified in the build.xml file:
<?xml version="1.0"?> <project default="init"> <target name="init"> <span style="color:#FF0000;"> <mkdir dir="HelloWorld/a/b/c"/></span> </target> </project>
5. Delete the multi-level directory as above:
<?xml version="1.0"?> <project default="init"> <target name="init"> <span style="color:#FF0000;"><delete dir="HelloWorld"/></span> </target> </project>
Note: Here the path only needs to enter the highest-level directory path, which is exactly the power of the ANT tool:
If you want to delete a directory in Java, you can delete it unless the directory is empty, otherwise you have to gradually delete it.
Using the Ant tool, you can directly delete folders containing subdirectories.
Let's look at another sample XML file:
<?xml version="1.0"?> <project default="init" name="Project"> <description> A simple project introducing the use of description tags in Ant build files. </description> <!-- XML comments can also be used --> <target name="init" description="Initialize Argon database"> <!-- perform initialization steps here --> </target> </project>
As can be seen, XML comments can be used throughout the build file for clarity. Moreover, Ant defines its own description element and description attributes, which can be used to provide more structured annotations.
Ant attribute:
Properties in Ant are similar to variables in programming languages, and they all have names and values. However, unlike usual variables, once set, properties in Ant are not changed; they are immutable, just like String objects in Java language. This may seem restrictive at first, but it is to follow Ant's simple principle: After all, it is a generator tool, not a programming language. If you try to assign a new value to an existing property, this will not be considered an error, but the property will still retain its existing value
Definition and use properties:
<property name="metal" value="beryllium"/>
To reference this property in other parts of the generated file, use the following syntax:
Copy the code as follows: ${metal}
For example, to use such a value that is part of the value of another property, write the label as follows
<property name="metal-database" value="${metal}.db"/>
location attribute:
Attributes are often used to reference files or directories on file systems, but for platforms that use different path separators (for example, / and /), this can cause problems when spanning different platforms. Ant's location attribute is specifically designed to include file system paths in a platform-independent manner. Use location instead of value like this:
<property name="database-file" location="archive/databases/${metal}.db"/> The path-separated characters used for the location attribute will be converted to the correct format of the current platform; and since the file name is relative, it is considered to be relative to the base directory of the project. We can also easily write it as follows:
<property name="database-file" location="archive/databases/${metal}.db"/>Both versions of this tag will have the same behavior on different platforms.
Defining dependencies:
Generating a project generally requires many steps - for example, first you need to compile the source code and then package it into a Java Archive File (JAR). Many of these steps have a clearly defined order - for example, you cannot package the class file until the compiler generates it from the source code. Unlike sequentially specifying targets, Ant takes a more flexible approach to defining dependencies. Each goal is defined based on all other goals that must be accomplished before it can be executed. This is achieved using the depends attribute of the target element
<target name="init"/> <target name="preprocess" depends="init"/><target name="compile" depends="init,preprocess"/><target name="package" depends="compile"/>
This method allows you to perform the generation process at any stage of the project; Ant will first execute the defined prerequisite phase. In the above example, if Ant completes the compile step, it will determine that the two goals of init and preprocess need to be executed first. The init target does not depend on any other target, so it will be executed first. Ant then checks the preprocessstarget and finds that it depends on the init target; since the latter has been executed, Ant will not execute it again, so he starts executing the preprocess target. Finally, the compile task itself can be executed.
Note that the order in which the targets appear in the generated file is not important: the order in which the execution is determined uniquely by the depends attribute.