Automatic Build tool ant in-depth analysis (3) Ant target in-depth explanation, use ant's various tasks

Source: Internet
Author: User
Tags time and date zip archive file

Run ant from the command line:

To call ant from a command prompt, you just need to type a separate ant. If you do this, ant will use the default generated file. The default target specified in the generated file is the target to be generated by ant. You can also specify many command line options, followed by any number of generation targets. Ant will generate each of these targets in order and resolve all dependencies in this process.

 

Command line options:

By default, ant looks forBuild. xml. Therefore, if your generated file uses this name, you do not need to specify it on the command line. Of course, it is more convenient to generate files with other names. In that case, you need
-Buildfile <File> parameter (-F <File> is abbreviated)

Another useful option is-D, which is used to set the attributes that can be used later in the generated file. This is very useful for configuring the generation process that you want to start in some way. For example, to set the name attribute to a specific value, you use an option similar to the following:

-- Dmetal = beryllium

This function overwrites the initial attribute settings in the generated file. As mentioned above, the attribute value cannot be changed once set. -D indicates that an attribute is set before any information in the generated file is read. Because the assignment in the generated file falls after this initial assignment, it does not change its value.


Compile source code:
-<Javac srcdir = "src"/>

This label looks for all files with the. Java extension in the src directory and calls the javac compiler for them to generate class files in the same directory. Of course, placing class files in a separate directory structure is usually clearer. You can add the destdir attribute to enable ant to do this.

Other useful attributes include
-Classpath: equivalent to the-classpath option of javac.
-DEBUG = "true": indicates that the compiler should compile the source file with debugging information.

An important feature of a javac task is that it only compiles the source files that it deems necessary to compile. If a class file already exists and the corresponding source file has not changed since the class file is generated, the source file will not be re-compiled.

The javac task output shows the number of actually compiled source files.

This behavior depicts the characteristics of many ant tasks: If a task can determine that the requested operation does not need to be executed, the operation will be skipped.

Create a jar file:

After compiling the Java source file, the result file is usually packaged into a jar file, which is similar to a ZIP archive file. Each jar file contains a configuration file that specifies the attributes of the JAR file.

The following is a simple example of jar tasks in ant:
<Jar destfile = "package. Jar" basedir = "classes"/>

This will create a jar file named package. jar and add all the files in the classes directory to it (the JAR file can contain any type of files, not just class files ). The configuration file is not specified here, So Ant will provide a basic configuration file

The manifest attribute allows you to specify a file used as the list of jar files. You can also use the manifest task to specify the content of the configuration file in the generated file. This task can write a configuration file to the file system, or it can be actually nested in the jar to create the configuration file and JAR file at one time.

<jar destfile="package.jar" basedir="classes"><manifest><attribute name="Built-By" value="${user.name}"/><attribute name="Main-class" value="package.Main"/></manifest></jar>


Timestamp generation:

It is often desirable to use the current time and date in the Generation Environment to mark the output of a generated task in some way so as to record when it was generated. This may involve editing a file to insert a string to specify the date and time, or merge the information into the file name of the jar or ZIP file.

This need is solved through a simple but very useful tstamp task. This task is usually called at the beginning of a generation process, for example, in an init target. This task does not require attributes. In many cases, only <tstamp/> is required.

Tstamp does not generate any output; instead, it sets the ant attribute based on the current system time and date. The following is an example of some tstamp attributes, descriptions of each attribute, and values that can be set for these attributes:

After the tstamp task is called, we can name the JAR file by date, as shown below:

<Jar destfile = "package-$ {dstamp}. Jar" basedir = "classes"/>


File System operations:

Create and delete directories
-One of the most basic file system operations is to create a directory or folder. The task for this job is named mkdir.
-<Mkdir dir = "Archive/metals/Zinc"/>
-Another useful feature of the mkdir task is its ability to create them when the parent directory does not exist.
-If the target directory already exists, the mkdir task will not send an error message, but it is assumed that its work has been completed so that nothing can be done.

Delete directory
-<Delete dir = "Archive/metals/Zinc"/>
-This will delete the specified directory along with all its files and subdirectories. You can use the File Attribute instead of the Dir attribute to specify the file to be deleted.

Copy and move files and directories
<Copy file = "src/test. Java" tofile = "src/testcopy. Java"/>
You can also use move to rename objects instead of copying objects.
<Move file = "src/test. Java" tofile = "src/testcopy. Java"/>
Another common file system operation is to copy or move a file to another directory.
<Copy file = "src/test. Java" todir = "ARCHIVE"/>
<Move file = "src/test. Java" todir = "ARCHIVE"/>

By default, ant only outputs the summary of the moving and copying operations it performs, including information such as the number of moved or copied files. If you want to see more detailed information, including the involved file name, you can set the verbose attribute to true.

Create and decompress a zip file:

<Zip destfile = "output.zip" basedir = "output"/>

Decompress and Extract files:

<Unzip src = "output.tar.gz" DEST = "extractdir"/>
You can also include the overwrite attribute to control overwrite behaviors. The default setting is to overwrite all existing files that match the entries in the extracted archive files.


Replace the mark in the file:

The replace task that performs the search and replace operations in the file.
The token attribute specifies the string to be searched for, and the Value Attribute specifies a new string. All instances marked with the string are replaced with the new string. For example:
<Replace file = "input.txt" token = "old" value = "new"/>

The replacement operation will be performed in the appropriate location within the file itself. To provide more detailed output, you can set the summary attribute to true. This causes the task to output the number of tag string instances found and replaced.

Pattern Matching:

The directory execution mode can be matched. For example, the pattern SRC */*. Java matches all java files in any directory with the SRC prefix.
There is another pattern structure: **, which matches any number of directories. For example, the pattern **/*. Java matches all java files in the current directory structure.

<Copy todir = "ARCHIVE">
-<Fileset dir = "src">
<Include name = "*. Java"/>
-</Fileset>
</Copy>

By default, fileset contains all files in the specified src directory. Therefore, to select only java files, we use an include element for the mode. Similarly, we can add an exclude element to another mode to potentially exclude the matching items specified by include. You can even specify multiple include and exclude elements. In this way, a group of files and directories are obtained. They contain the union of all matching items in the include mode, but all matching items in the exclude mode are excluded.

Excluded by default:

List of built-in modes automatically excluded from the file set content. This list includes entries that match the directory named CVs, and ~ Files at the end of the string, which may be backup files. Generally, you do not want to include such files and directories in file system operations. Therefore, excluding these files is the default action. However, if you want to select all files and directories without exceptions, you can set the defaultexcludes attribute of the file set to No.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.