Ant Reading Notes

Source: Internet
Author: User
Tags zip archive file

Ant notes
(0) Overview:
Ant, which is similar to the original make document.
The main function is to facilitate the automatic deployment of projects. Now there is an updated meaven which can be used as a substitute for ant. However, ant is still widely used.

Ant can be understood as a script written based on XML, which is automatically executed to complete a series of tasks. This is similar to bat. In short, it is a very simple but useful thing.
Ant generally does not need to be set up and released as a project release. In fact, it is generally a release tool provided during project development and deployment. Using Ant simplifies many File Operations/compilation tasks that should have been manually completed.

I tried to find an ant ebook without more than 30 pages. Let's take a rough look and verify that my guess on ant is correct. Then, let's take a systematic look at ant's ideas and implementations.

Ant needs to be installed and set the PATH environment variable so that the commands published by ant can be executed in the command line environment. This is the same as the commands such as JDK Java and javac.

Ant has an important extension point. We can easily compile custom ant tasks.

-- In addition, this tutorial was written by an ibm uk engineer.
-- The abbreviation of ant is actually another neat tool (another smart tool )....

Ant has a cross-platform feature. Of course, this feature is inherited from Java.

 

(1) Basic Structure of ant script (XML:

-Project
|
|-Target1
|-TARGET2
|-Target3

The target is a step-by-step abstraction.
Target can contain any number of operations.
The operations in these targets are executed in a certain order, and the tasks of the entire project are implemented. This project can also be understood as an ant project.

 

(2) attributes of ant:
Similar to variables in programming languages.
Name and key-Value Pair abstraction.
Similar to the following, You can reference the defined attributes elsewhere in the script.
<Property name = "Metal" value = "beryllium"/>
Reference Syntax:
$ {Metal}

Ant has many predefined attributes, such as Java environment settings.
For example: $ {user. Home}
Attributes are often used to reference files or directories on the file system.
If the attribute is to define the system path, it is better to use location than to use value.

<Property name = "srcpath" location = "Archive/databases/"/>
Better:
<Property name = "srcpath" value = "Archive/databases/"/>

For different operating systems, "\" is used as the path separator, and "/" is used as the path separator.
With location, ant will automatically convert the path to a valid path name of the operating system. This increases the portability of ant scripts.
Alternatively, you can maintain both the win_build.xml and unix_build.xml.

(3) dependency (depends)
<Target> may depend on other targets.
This statement indicates that to complete a target, the other target must be completed first.
For example:
<Target name = "init"/>
<Target name = "preprocess" depends = "init"/>
<Target name = "compile" depends = "init, preprocess"/>
<Target name = "package" depends = "compile"/>

If we want ant to execute the package, it will automatically execute init, preprocess, compile, and Package first.
This dependency is well understood, but it is important for ant. I think dependency is the core of ant's thinking.
In this way, I can only execute compile. Then the operation sequence will only be executed to compile, and no package will be executed.

The execution sequence of the target in ant is irrelevant to the position where the target appears in the script.
The execution of target is determined by the unique dependency.

 

(4) Run ant
Ant runs in eclipse (or other IDE. Only the command line is concerned. IDE only uses the UI to replace the command line call.
Ant-file build_prop.xml
Run ant to write a bat program.

(5) generate a project
<Compile task>:
Ant's main goal is to generate a Java application, which internally and well supports calling the javac compiler and
Other Java-related tasks are not surprising. The following describes how to compile Java code tasks:
<Javac srcdir = "src"/>
This label looks for all the files with the. Java extension in the src directory, and calls the javac compiler
Generate class files in the same directory. Of course, it is usually clearer to put class files in a separate directory structure. You can add
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.

<Jar task>
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, which can specify 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 (Jar
Files can contain any type of files, not just class files ). The inventory file is not specified here, So Ant will provide
Basic configuration file.
The manifest attribute allows you to specify a file used as the list of jar files. Manifest can also be used for the contents of the configuration file.
The task is specified in the generated file. This task can write a list file like a file system, or it can be actually nested in the jar
. For example:
<Jar destfile = "package. Jar" basedir = "classes">
<Manifest>
<Attribute name = "built-by" value = "$ {user. name}"/>
<Attribute name = "Main-class" value = "package. Main"/>
</Manifest>
</Jar>

(6) file system operations
<Create/delete a directory>
One of the most basic file system operations is to create a directory or folder. The task for this job is named mkdir. It is not surprising that
Similar to Windows and Unix/Linux commands with the same name.
<Mkdir dir = "Archive/metals/Zinc"/>

Note that/is used as the directory separator, which is a common practice of UNIX and Linux. You may think this is not platform-independent,
But ant knows how to handle it and does the right thing for the platform where it runs, which is based on the location defined above
The method is the same. We can also easily use \, regardless of what the platform is -- ant can handle
Or even two forms of mixing.
Another useful feature of the mkdir task is its ability to create them when the parent directory does not exist. Consider the preceding clear
Single, imagine that the archive directory exists, but the metals directory does not exist. If you use the mkdir command of the underlying platform, you must
You must first explicitly create the metals directory, and then call the mkdir command for the second time to create the zinc directory. However, ant tasks
Ant User Guide
Java enthusiast 20th page http://www.javafan.net
More intelligent than this, it can create these two directories at a time. Similarly, if the target directory already exists, the mkdir task does not
An error message is sent, but it is assumed that the work has been completed, so that nothing can be done.
It is also easy to delete directories:
<Delete dir = "Archive/metals/Zinc"/>
This will delete the specified directory along with all its files and subdirectories. You can specify the File Attribute instead of the Dir attribute.
A single file to be deleted.

<Copy/move a file/directory>
It is easy to copy a file in ant. For example:
<Copy file = "src/test. Java" tofile = "src/testcopy. Java"/>
You can also use move to rename objects rather than copy 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. The ant syntax for this job is also very simple.
Ticket:
<Copy file = "src/test. Java" todir = "ARCHIVE"/>
<Move file = "src/test. Java" todir = "ARCHIVE"/>

<Compression/Decompression>
<Zip destfile = "output.zip" basedir = "output"/>
Ant User Guide
Java enthusiast 21st page http://www.javafan.net
The same syntax can also be used to create a tar file. You can also use gzip and bzip tasks to compress files. For example:
<Gzip src = "output.tar" zipfile = "output.tar.gz"/>
Decompression and file extraction are also simple:
<Unzip src = "output.tar.gz" DEST = "extractdir"/>
You can also include the overwrite attribute to control overwrite behaviors. The default setting is to overwrite the entries in the archive file being extracted.
Match all existing files. The related task names are untar, unjar, gunzip, and bunzip2.

<Replace the mark in the File>
The last file system operation we will examine in this section is the replace task, which executes the search and replace operations in the file.
The token attribute specifies the string to be searched, the Value Attribute specifies a new string, and all the real
In this example, all strings are replaced with the new string. For example:
<Replace file = "input.txt" token = "old" value = "new"/>

(7) important pattern matching and file Selector
When we checked file system tasks, we only used individually named files and directories. However, a single execution of a group of Files
These operations are often useful-for example, performing operations on all files ending with. Java in a given directory. Just as equivalent
DOS and Unix Commands provide the same functions as ant. This is done using wildcard characters
Of: *, it matches zero or multiple characters; and ?, It only matches one character. Therefore, all files ending with. Java are matched.
The mode is only *. java.
You can also match the directory execution mode. For example, the pattern SRC */*. Java will match all
Java file. There is another pattern structure: **, which matches any number of directories. For example, the pattern **/*. Java will match when
All java files in the directory structure.
You can use modes for file system tasks in a fairly consistent manner, such as nested fileset elements. Previously, we used
Task to copy a single file:
<Copy file = "src/test. Java" todir = "ARCHIVE"/>
If you want to use a mode, you can replace the file attribute with a fileset element, as shown below:
<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
Use an include element. Similarly, we can add an exclude element to another mode to potentially exclude
Include the specified match. You can even specify multiple include and exclude elements. In this way, a group of files and directories are obtained,
They include the union of all matching items in the include mode, but exclude all matching items in the exclude mode.
Note that there is also a file set feature that is usually useful, but for those who are not aware of it, this feature occasionally produces mixed
. This feature is called default exclusion: A list of built-in modes automatically excluded from the file set content. This list includes
And ~ Files at the end of the string, which may be backup files. You usually do not want to operate in the file system
This class of files and directories are included, so excluding these files is the default action. However, if you want to select all
Set the defaultexcludes attribute of the file set to No.

As we can see, the file set is used to specify a group of files, and the content of this group can use include and exclude.
Mode. You can also use include and exclude in combination with special elements called selector to select files. Below is
List of available core selectors for ant:
· Size: This selector is used to select a file based on the file's byte size (unless the units attribute is used to specify different
Unit ). The When attribute is used to set the comparison properties (less, more, or equal). The Value Attribute defines each
Files will be compared with the target size.
· Contains: Only files containing the given text string (specified by the text attribute) match this selector. Default
In this case, the search operation is case sensitive. Adding casesensitive = "no" can change the default setting.
· Filename: Name attribute specifies the pattern in which the file name matches. It is essentially the same as the include element, and
The exclude element is the same as the exclude element when negate = "yes" is specified.
· Present: select the following files from the current directory structure: they are consistent with the files in the specified targetdir directory
The same name and relative directory structure.
· Depend: This selector has the same effect as the present selector, except that the matching files are restricted
For objects in the targetdir location, the files that have recently been modified.
· Date: This selector selects a file based on its last modification date. The When attribute specifies that the comparative nature is before,
After or equal. The datetime attribute specifies the date and time to be compared.
Fixed format: mm/DD/YYYY hh: Mm am_or_pm. Note that there is a built-in 2-second offset on Windows,
To allow the underlying file system to be inaccurate-this may cause the number of matched files to exceed expectations. Allowed maneuver time
You can use the granularity attribute to change the inter-volume (in milliseconds ).
· Depth: This Selector checks the number of directory levels of each file. Min and/or Max attributes are used to select
The number of directory levels.
Ant User Guide
Java enthusiast 24th page http://www.javafan.net
You can also combine selectors by embedding one or more selectors in a selector container. The most common selector container and
Select only the files selected by all the selectors contained in the file. Other options include or, not, none, and majority.
The following is an example of a file set. It only selects files larger than 512 bytes that contain the string "hello.
<Fileset dir = "dir">
<And>
<Contains text = "hello"/>
<Size value = "512" When = "more"/>
</And>
</Fileset>

(
The file links and custom ant tasks are not viewed.
The specific application can be analyzed in detail. With ant's capabilities, you can imagine the release process of a Project:
(1) Call the CVS task to download the project from the server with a read-only permission.
(2) Call the javac task to compile the project.
(3) call a file system task (including pattern matching and file selector) to deploy generated. class files and resource files.
(4) call a file system task to deploy the configuration file.
....
The entire one-stop service...
)

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.