Use of Apache Ant

1. What is Ant

The following introduction is from Baidu Encyclopedia:
Apache Ant, is a tool that links software compilation, testing, deployment and other steps together to automate it, and is mostly used for software development in the Java environment. Provided by the Apache Software Foundation.
When we use Eclipse to develop Android applications, the project building tool Eclipse uses is Ant.
User group: Most Java designs are used to manage a large amount of information flow. For example, New York State uses Apache Ant to manage the largest youth program in the United States, which can update the records of more than 250,000 students in real time every day.
As long as the readers who have used the Linux system 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 the functions implemented by Ant are similar. Compilation tools like make, gnumake, and nmake have certain flaws, but Ant has overcome the flaws of these tools. Initially, when Ant developers were developing cross-platform applications, they also made better designs for Ant based on these flaws.
Second, the advantages of Ant

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 pure Java language, so it has a good cross-platform.
easy to use. Ant is composed of a built-in task and optional tasks. An XML file (build file) is required for Ant to run. Ant can execute various tasks by calling the target tree. Each task implements a specific interface object. Since the Ant build file is in XML format, it is easy to maintain and write, and the structure is very clear. Ant can be integrated into the development environment. Due to its cross-platform and simple operation characteristics, Ant is easy to integrate into some development environments.
Three, the basic use of Ant

The following describes the basic usage of Ant:
1. First, you need to download apache ant from the official website, the address is: http://ant.apache.org/bindownload.cgi
2. The downloaded compressed file is directly decompressed and placed in the hard disk directory Just do it, and then configure the Ant environment variables. Because we need to use the ant command under the command line, we need to add the ant directory to the path environment variable. The steps are as follows (windows10 system):
(1) Right-click the desktop "this computer" ", select properties, and then select "Advanced System Settings" on the left side of the window that appears, and select "Environment Variables" in the pop-up window
(2) Add a new variable named "User Variables" in the environment variable configuration window ANT_HOME, the value is the directory after Ant decompression, as shown in the following figure:
Insert picture description here
Then find the PATH variable under "User Variables", if not, add a PATH variable, if there is, add a new value to the PATH variable directly, the value is "%ANT_HOME%\bin"
After the above 2 steps, the Ant environment variables are configured. After the ant -version command is executed in cmd, the configuration is successful as shown in the following figure:
Insert picture description here
3. Start using Ant.
The core of Ant is the configuration file build.xml. After configuring the related tasks in the build.xml file, the ant command can be used to automatically execute, so we need to master the related configuration of ant. Here, create a new directory TestAnt on the desktop, and then enter In this directory, create a new build.xml file and add the following content:
Insert picture description here
In the command line, we enter the directory where the above build.xml is located, and then execute the ant command, the result shown in the following figure will appear:
Insert picture description here
Let’s explain the above build The configuration of the .xml file:
(1) Build.xml needs to follow a certain format, so that the ant command can be executed correctly. A build.xml file is based on the label as the root node. The name attribute can be specified in the node to indicate the name of the project. Basedir represents the root directory of the project. , Default represents the default task name of the project, where the default attribute value is test, when the ant command is executed, the target corresponding to default will be found for execution (if the ant command does not specify a task name).
(2) A label is defined in the label. The label represents a task. The name attribute in the label represents the task name. We can directly use ant + task name to perform a specific task under the command line, such as the above In the example, we can use the ant test command to execute the target task whose name is test. Since we have configured the default attribute as test in the label, we only need to execute the ant command on the command line to run the test task.
(3) The label means output under the command line, similar to System.out.println() in java. After configuring the message attribute in the label, the value of the attribute can be printed under the command line. As shown in the figure above, [echo] is the value of the message attribute we configured.
(4) Use ${} to get the value of a certain variable. The variable name is in curly braces. As shown in the figure above, C:\Users\yubo7\Desktop\TestAnt is the value of basedir, because we are in the label The value of the basedir attribute is empty, so the value of basedir defaults to the path where build.xml is located.
The above example is the most basic structure of the build.xml file. There are many configuration items in the build.xml file. Here are a few configuration items:
tags, used to declare key-value pairs:

Insert picture description here

After executing the ant test-property command, the result is as follows: The
Insert picture description here
label is used to declare a property, where name is the property name, value is the property value, and ${property name} is used when accessing the property value.
Label, used for file copy: After
Insert picture description here
executing the ant test-copy command, the result is shown in the following figure:

Insert picture description here
The label indicates the copy of a file or folder. In the above configuration, the file attribute indicates the path of the file to be copied, and the tofile attribute indicates the path of the destination file to be copied. If the file to be copied does not exist, the command execution will fail. If the destination file path does not exist, the directory will be created automatically when the command is executed. If you want to copy the entire directory, you need the following configuration:

Insert picture description here
After executing the ant test-copy-dir command, the result is shown in the following figure:
Insert picture description here
Similar to the copy of a single file, if the directory to be copied does not exist, the command execution will fail. If the destination path does not exist, the command will be created automatically when the command is executed table of Contents.
Label, used to delete files or folders: After
Insert picture description here
executing the ant test-del-file and ant test-del-dir commands, the results are shown in the following figure:

Insert picture description here
Label, used for directory creation:

Insert picture description here
After executing the ant test-mkdir command, the result is as follows:

Insert picture description here
After successful execution, the test directory appears in the project directory.
The above several are the basic usage of ant operating files, but the usage of ant is not only that simple, ant can also compile java files, the usage is as follows:
tags, used to compile .java files into .class files:

Insert picture description here
srcdir specifies the path where the java source file is located, and destdir specifies the path where the compiled class file is stored. Here is a simple test. We create a new Test.java file in the src directory with the following code:
Insert picture description here
Then execute ant The result of the test-compile command is as follows: When
Insert picture description here
the ant test-compile command is executed for the first time, the compilation is not successful because the destdir directory does not exist. After the build/classes/ directory is created, the ant test-compile is executed again, and the command is executed successfully , And the Test.class file appears in the build/classes/ directory.
Ant can compile java files, and naturally run java programs. The usage is as follows:
tag, used to execute .class file:
Insert picture description here
the classname attribute in the tag specifies the name of the main class to be executed, and the tag in the tag specifies the class file path.
After executing the ant test-run-java command, the result is as follows:
Insert picture description herethe destfile attribute in the label indicates the jar file to be generated, basedir specifies the path of the class file, the label specifies the attributes in the MANIFEST.MF file in the jar package, and the configuration is inside Main-class, that is, the main class name is "Add".
Task dependency:
When using ant to execute tasks, we will definitely not execute them one by one. Sometimes we will execute multiple tasks through one command, such as the following process:
create a new directory -> move a file to this directory -> delete the original file
This process is actually three independent tasks. In order to perform these three tasks at once, the depends attribute in the label needs to be used. First, add the code:

Insert picture description here
There are three tasks in the above configuration file. The copy-file task and the delete-file task both contain the depends attribute, which indicates that the task in execution depends on the task specified in depends (the task specified in depends will be executed first ), the depends attribute value is a task name, so the configuration file above indicates that the delete-file task needs to rely on the copy-file task, and the copy-file task needs to rely on the mkdir task. The default value in the label is delete- file, when we execute the ant command, we will find the delete-file task, but if this task depends on other tasks, the dependent task will be executed first, so the order of execution of the above configuration file is: mkdir–>copy-file –>delete-file.

For more usage of Ant, please refer to the official document: http://ant.apache.org/manual/index.html

The above is the basic usage of ant. The next article will record the process of executing tasks when Eclipse uses ant to build a project.


Author: yuxiyu!
Source: CSDN
Original: https://blog.csdn.net/yubo_725/article/details/52326746
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!


Author: yuxiyu!
Source: CSDN
Original: https://blog.csdn.net/yubo_725/article/details/52326746
Copyright: This article is a blogger original article, reproduced, please attach Bowen link!

Guess you like

Origin blog.csdn.net/Love_Polaris/article/details/95763606
Recommended