Ant introduction and basic use guide

xiaoxiao2021-03-06  62

Ant introduction and basic use guide

Ant is a project of famous Java open source organization Apache, is a Java-based build tool. It allows you to copy your project to a directory through the Ant scripting language, publish the project, or generate some code, and execute the SQL language. In short, it can help you complete most of the auxiliary work other than the development code in the project development. Why do you need to write Ant scripts manually? Many IDE tools will generate ANT scripts. Some people will ask this. To know something that is automatically generated by IDE, there is always such shortcomings, and insufficient, he can't fully meet your needs. If there is a problem, you have to manually modify the ANT script, this time if you don't understand Ant, then you will fight. So in order to make your own project more specialized, or if you want to show in front of the leader (sometimes it is useful). Still find out Ant.

Ant installation use

installation

Ant installation is very simple, as long as you download his ZIP package from the Apache website, then decompress the value of the PATH in the environment variable to any local disk, you can add% Ant_Home% / bin, which is in the DOS prompt You can use Ant this command. Others you need to set up Ant_home this environment variable for you to decompress the directory. Also, you have to check your JDK installation directory java_home is not adding to your environment variable.

use

Overview

It is also very simple to use Ant. The usual approach is to build an XML file in your project, and the name can usually take build.xml. Of course you can take any name you like. This file is a batch file you need Ant to make some auxiliary work to your project. His format may be like this. We don't specifically say Ant markers, first have a sense of understanding, it will get started.

example:

This build.xml file is a project from Apache. You can see there is Project and Target labels. Some projects of Build.xml may not start with

The following is a typical example.

The Target A can be executed independently, but B, C, D can do in other Targets, that is, executing D, Ant will perform other Targets before the order of A- B- C.

There is a default = "all" in the Project tag, which means that the ALL TARGET will be executed by default.

Define variables

Didn't see the property definition in this file, in fact, if you want to use the defined variable, you can define such a label under the Project tag.

This way you can use the shape when you are quoted, such as $ {build}

......

......

Such variables, save yourself to modify the place where the variable needs to be used

Add directory operation

This is the variable we define with $ {}, etc.

Compile the operation of the Java file

Say here Javac this task tag

It sees the properties such as Srcdir, Destdir, Debug, Encoding, as well as a subtrack of ClassPath. Srcdir is the target source, and the source file that needs to be compiled. Destdir is the destination, the stored Class storage. The debug parameter is indicating that the source is needed to compile the debug information. If this parameter is not added equal to the back of the command line, the parameter is added. Encoding This parameter indicates which coding method is encoded in your Source file, which is important for the code with Chinese text.

ClassPath indicates that the JAR package you need to apply, or the location of other Class files, this is also a very important option. There are several ways in use.

.......

......

As a pre-defined JAR packet position, RefID indicates a reference variable. It is generally used in Real-World. Simple use is like this:

destdir = "$ {build}"

Classpath = "xyz.jar" debug = "on"

/>

This is simple.

Playing JAR package operation

This is the ANT script that makes compiled good files into the JAR package, like Javac above, can be placed anywhere. Obviously Destfile is the package you want to be, based on your target class file, other complex parameter manuals, you can take photos.

Excludes = "** / Test.class"

/>

The above script is also very easy to understand, that is, in addition to the test.class file, put a source of the resource directory, into the app.jar package along with the compiled Class script.

Copy file operation

The above script is easy to understand. TODIR specifies the location that needs to be copied. Does OverWrite need to overwrite, whether Flatten ignores the destination directory structure, only the directory, copy file directly to the destination, discard its structure.

Select all the JAR package below the package defined by the j2ee.jar package

Other copy styles

Single file copy

Document to the directory copy

Directory to copy

More useful command

Copy the SR_DIR directory to the Backup / Dir directory and replace the @ title @ of all files into Foo Bar. There are still many usage, you can check the manual when you want to use.

Delete operation

The above operation is also very easy to understand.

Execute an Ant script

It is possible to use Ant under the directory prompt with build.xml. If you are other names, you can use the ANT -BUILDFILE .

Others are some of the relatively classic operations of Real-World during the specific development process. For example, use the XDoclet to automatically generate the Hibernate configuration file, perform SQL scripts, which will be described in detail in the subsequent introduction. Mastering them, you will greatly improve work efficiency during the production process, others will also think about you, don't forget to share with your colleagues.

Author: Spike Spiegel,

Blog: http://blog.9cbs.net/HK

2000C

/

About Copyright: Welcome to reprint, please indicate the author.

Reference

Apache Ant Main Station: http://ant.apache.org/

转载请注明原文地址:https://www.9cbs.com/read-85070.html

New Post(0)