Ant practice

zhaozj2021-02-12  179

Ant practice

eric

Contents 1. What is ANT? 2. Install ANT3. Run ANT4. Write build.xml5. Built-in Task 6. Ear Task (Internet) 7. War Task 8. JUnit Task (Internet)

What is ANT?

ANT is a BUILD tool based on Java. In theory, it is some similar (unix) Make, but there is no Make's defect. Since we already have make, gnumake, nmake, jam, and other build tools, why do you have a new build tool? Because Ant is developing software in a variety of (hardware) platforms, they cannot endure the limitations and inconveniences of these tools. Tools similar to MAKE is essentially a shell (language): they calculate dependencies, then execute commands (these commands are not very different from the command you are on command.). This means that you can easily extend the tool by using the OS unique or writing new (command) programs; however, this also means you limit yourself to a specific OS, or on a specific OS type, such as UNIX. Makefile is also very disgustable. Anyone who has used their people have encountered a variable Tab problem. Ant's original author often asked himself: "Is my order not executing just because there is a space before my tab ?!!". Tools similar to JAM handle such problems well, but (users) must remember and use a new format. Ant is different. Unlike the extended mode based on the shell command, ANT expands with Java classes. (Users) Don't have to write the shell command, the configuration file is based on XML, and all kinds of TASK can be performed by calling the Target tree. Each Task is run by an object that implements a specific TASK interface. (If you have no words to Ant, you may not understand this section, there is no relationship, you will introduce the Target, Task. You can even skip this section without too much time, then then Go back to browse the introduction here, then you will understand. Similarly, if you are not familiar with the tools such as Make, the following introduction will not use the concept in Make.) Must admit Some unique expressive capabilities will be lost when constructing the shell command. Such as `find. -Name foo -exec rm {}`, but gives you a cross-platform ability - you can work anywhere. If you really need to perform some shell commands, Ant has a Task, which allows the command on a specific OS. return

2. Install ANT

Since Ant is an Open source software, there are two ways to install Ant, one is to install Ant with a compiled binary file, and the other is the source code you build Ant. The binary form in the form of Ant can be downloaded from http://jakarta.apache.org/builds/ant/release/v1.4.1/bin. If you want you to compile Ant, you can get from http://jakarta.apache.org/builds/ant/release/v1.4.1/src. Note that the listed connections are the latest release of Ant. If you read this article, found that there is already an updated version, then use the new version. If you are a crazy technical pursuit, you can also download the latest version from Ant CVS Repository. System requirements want yourself build Ant. You need a JAXP-compatible XML parser (PARSER) in your ClassPath system variable. The binary form of Ant includes the latest version of the Apache Crimson XML parser. You can get more information about JAXP from http://java.sun.com/xml/. If you want to use other JAXP compatible parsers. You have to delete JAXP. JAR and CRIMSON.jar from the LIB directory of Ant. Then you can put your loved parser JAR file in the Ant's lib directory or put it in your ClassPath system variable. For the current version of Ant, you need JDK, 1.1 or higher in your system. The future Ant version will require JDK 1.2 or higher. Ant installed Antbinary version includes three directories: BIN, DOCS, and LIB. Only the bin and lib directory are required to run Ant. To install Ant, select a directory and copy the release of the release to this directory. This directory is called Ant_Home. Do some configuration work before you run Ant. Add a bin directory to the PATH environment variable. Set the ANT_HOME environment variable to point to the directory where you install Ant. On some OS, Ant's script can guess Ant_Home (UNIX and WindOS NT / 2000) - but it is best not to rely on this feature. Alternatively, the Java_Home environment variable (refer to the following advanced section), which should point to the directory of the JDK installed.

Note: Do not put the Ant.jar files of the Ant to the lib / ext directory of JDK / JRE. Ant is an application, and the lib / EXT directory is used for JDK extension (such as JCE, JSSE extension). And there will be security restrictions by extension. Optional Taskant supports some optional TASK. An optional Task generally requires an additional library to work. Optional Task is separated from the built-in Task of Ant, packaged separately. This optional package can be downloaded from the same place you download from Ant. The JAR file name called optional Task is called Jakarta-Ant-1.4.1-optional.jar. This JAR file should be placed in the lib directory of the Ant installation directory. The external library required for each optional Task can be referring to the dependent library section. These external libraries can be placed in the LIB directory of Ant, so Ant can be automatically loaded, or put it in an environment variable. Windows assumes that Ant is installed in a C: / Ant / directory. Below is a command to set the environment:

SET ANT_HOME = C: / ANTSET JAVA_HOME = C: /JDK1.2.2Set Path =% PATH%;% Ant_Home% / Bin

Unix (bash) assumes that Ant is installed under the / usr / local / ant directory. Below is a command to set the environment:

Export Ant_Home = / usr / local / antExport java_home = / usr / local / jdk-1.2.2export path = $ {path}: $ {ant_home} / bin Advanced To run Ant Must use a lot of variables. You need at least reference to the following:

Ant's classpath must contain Ant.jar and the JAR file that you choose JAXP-compatible XML parser. When you need JDK's function (such as Javac or RMIC Task), for JDK 1.1, JDK's classs.zip file must be placed in ClassPath; for JDK 1.2 or JDK 1.3, you must join Tools.jar. If the correct Java_Home environment variable is set, the script belongs in Ant, in the bin directory, automatically add the desired JDK class. When you perform a specific platform program (such as Exec Task or CVS Task), you must set the Ant.home property to point to the installation directory of Ant. Similarly, the scripts belled by the ANT use the ANT_HOME environment variable to automatically set this property.

Building Ant If you want to install the Ant source code release or from the CVS in the CVS in the CVS. After installing the source code, enter the installation directory. Set the Java_Home environment variable points to the JDK installation directory. If you want to know how to do, please refer to install the ANT section. Make sure you have downloaded any auxiliary JAR file so that builds you are interested in Task. These JAR files can be placed in the classpath or placed in the lib / optional directory. See those JAR files in the dependent Task that can be seen from the Library section. Note that these JAR files are just used as build Ant. To run Ant, you have to set these JAR files as you do in the ANT section. Now you can build Ant:

Build -ddist.dir = dist (windows) build.sh -ddist.dir = dist (UNIX)

This will create a binary version in the directory you specify. The above command executes the following action:

If there is a need to bootstrap Ant's code. Bootstrap includes manually editing some ANT code to run Ant. Bootstrap is used in the build step below. Pass parameters to the build script to call bootstrap Ant. The parameter defines the attribute values ​​of Ant and specifies the "dist" Target of Ant's own build.xml file.

In most cases, you don't have to directly bootstrap Ant, because the Build script is done for you. Run bootstrap.bat (windows) or bootstrap.sh (unix) can build a new Bootstrap version Ant. If you want to install Ant into the Ant_Home directory, you can use:

Build Install (Windows) Build.sh Install (UNIX): BUILD INSTALL

If you want to skip the lengthy Javadoc steps, you can use:

Build Install-Lite (Windows) Build.sh Install-Lite (UNIX): BUILD INSTALL-LITE (UNIX)

This will only install the bin and lib directory. Note INSTALL and Install-Lite will override the current Ant version in Ant_home. Related Library If you need to perform a specific Task, you need to put the corresponding library into the ClassPath or put it in the lib directory of the Ant installation directory. Note You only need a regexp library when using Mapper. At the same time, you have to install an optional JAR package of Ant, which contains the definition of Task. Refer to the above installation ANT section. JAR Nameneeded ForaVailable Atan XSL Transformer Like Xalan Or XSL: PStyle Taskhttp: //xml.apache.org/xalan-j/index.html or http://www.clc-marketing.com/xslp/jakarta-regEXP-1.2. jarregexp type with mappersjakarta.apache.org/regexp/jakarta-oro-2.0.1.jarregexp type with mappers and the perforce tasksjakarta.apache.org/oro/junit.jarjunit taskswww.junit.orgstylebook.jarstylebook taskCVS repository of xml.apache .orgtestlet.jartest taskjava.apache.org/frameworkantlr.jarantlr taskwww.antlr.orgbsf.jarscript taskoss.software.ibm.com/developerworks/projects/bsfnetrexx.jarnetrexx taskwww2.hursley.ibm.com/netrexxrhino.jarjavascript with script taskwww. Mozilla.orgjpython.jarpython with script taskwww.jpython.orgnetComponents.jarftp and telnet taskswww.savarese.org/oro/downloads

return

3. Run Ant

Running Ant is very simple, when you correctly install Ant, just enter Ant. When you do not specify any parameters, Ant will query the build.xml file in the current directory. If you find it, use this file as buildfile. If you use the -find option. Ant will look for BuildFile in the superiors until the root of the file system is reached. To allow Ant to use other buildfiles, you can use the parameter -Buildfile file, where the file specifies the buildfile you want to use. You can also set some properties to override the property value specified in the buildfile (see Property Task). You can use the -Dproperty = value option, where Property is the name of the attribute, and the value is the value of the attribute. This approach can also be used to specify the value of some environment variables. You can also use Property Task to access environment variables. Just pass -dmyvar =% myvar% (Windows) or -dmyvar = $ myvar (unix) to Ant - you can access these environment variables with $ {myvar} in your buildfile. There are two option -quite, telling Ant run only a small amount of necessary information. And -verbose tells Ant to output more information. You can specify one or more Targets. When Target is omitted, the ANT uses the target attribute specified by the Default property of the label . If there is, the -projecthelp option outputs a list of description information and item Target. The Targets that are described and then described are not described. Command line option summary: Ant [options] [target [target2 [target3] ...] Options: -help print this message-projecthelp print project help information-version print the version information and exit-quiet be extra quiet-verbose be extra verbose-debug print debugging information-emacs produce logging information without adornments-logfile file use given file for log output-logger classname the class that is to perform logging-listener classname add an instance of class as a project listener-buildfile file use specified BUILDFILE-FIND FILE SEARCH for Buildfile Towards the root of the filesystem and use the first one found-dproperty = value set proteTy to Value

example

Ant

Use the build.xml in the current directory to run Ant, perform the default target.

Ant -buildfile Test.xml

Use the Test.xml in the current directory to run Ant, perform the default target.

Ant -Buildfile Test.xml Dist

Use the Test.xml under the current directory to run Ant, perform a target called DIST.

ANT -BUILDFILE TEST.XML -DBUILD = Build / Classes Dist

Use the Test.xml in the current directory to run Ant, perform a target called Dist, and set the value of the build property of the build / class. Files on UNIX, Ant's execution scripts will source before doing anything (read and calculate values) ~ / .antrc files; on Windows, Ant's batch file calls% home% / antc_pre.bat at the beginning, Call% home% / antc_post.bat at the end. You can use these files to configure or cancel some environment variables you need when you run Ant. Look at the example below. Environment Variable Wraps (Wrapper Scripts) Use the following environment variables (if any): JavaCMD Java executables absolute path. Use this value to specify a JVM different from java_home / bin / java (.exe). Ant_opts passed to the JVM command line variable - for example, you can define the attribute or set the maximum of Java stacks.

Handmade Ant If you do your own installation (DIY) Ant, you can start Ant with the following command:

Java-Dant.home = C: / Ant Org.apache.tools.ant.main [options] [target]

This command is the same as the front Ant command. Options and Targets are also the same as when using an ANT command. This example assumes that your classpath contains:

Ant.jar Jars / Classes for Your XML Parser The JDK's Required Jar / Zip Files

return

4. Write build.xml

Ant's buildfile is written with XML. Each buildfile contains a Project. Each Task element in BuildFile can have an ID attribute that can be used to reference the specified task with this ID value. This value must be unique. (For details, please refer to the following Task Section) ProjectsProject has the following properties:

AttributeScriptionRiptionRequiredName project name .NodeFault When the default targetyesbasedir used when not specified Target is used to calculate the base path of the other path. This property can be overwritten by BaseDir Property. This property is ignored when it is overwritten. If the attributes are not set, the parent directory of the buildfile file is used in BUILDIR Property. NO

The description of the project appears in the form of a top element (see Description Section). A project can define one or more Targets. A Target is a series of what you want to do. When performing Ant, you can choose to execute that Target. When there is no Target, use the Target determined by the Project's Default property. Targets A Target can rely on other Targets. For example, you may have a Target for compiler, a Target to generate an executable. You must compile the pass before generating an executable, so the target that generates executable files depends on compiling Targets. Ant will deal with this dependency. However, it should be noted that Ant's Depends attribute specifies the order of Target should be executed - if the dependent target cannot be run, this depends has no effect on target specified dependencies. Ant will perform each target in order (from left to right) in the order of Target in the Depends property. However, it is necessary to remember that as long as a Target relies on a Target, the latter will be executed first.

Assume that we have to perform Target D. From its dependency properties, you may think that C, then B, and final A is executed. Wrong, C depends on B, B depends on A, so before the A, then B, then C, and final D is executed. A Target can only be executed once, and there are multiple Targets depending on it (see the above example). If (or if not) certain attributes are set, a Target is executed. In this way, the process of Build is allowed to better control the process of the system according to the status of the system (Java Version, OS, command line attribute, etc.). To make a target element, you should join the IF (or UNSS) property in the Target element, with the target of the determined attribute. E.g:

Target will always be executed if there is no IF or UNSS attribute. Optional Description attributes can be used to provide a line description for Target, which can be output by the -projecthelp command line option. Well your TSTamp Task is a good practice in a so-called initial Target, and other Targets rely on this initial Target. To ensure that the initialization Target is the first Target that appears in other Target rerequisites. Most of the initial Target in this manual is "init". Target has the following properties:

AttributeScriptionRequiredNametarget's name yESDepends is a list of named Targets with a comma, which is the dependent table. NOIF executes the name of the attribute that Target needs to be set. Nounless executes the Target to clear the set properties name. NodeScription About Target features a short description. NO

Tasks a task is an executable code. A Task can have multiple properties (if you like, you can call it as a variable). Attributes may only contain references to Property. These references will be parsed before TASK execution. Below is a general constructor of Task:

>

Here Name is the name of Task, attributen is the property name, VALUEN is attribute value. There is a built-in Task, as well as some optional Task, but you can also write your own task. All Task has a Task name property. ANT uses attribute values ​​to generate log information. You can assign a ID attribute to Task:

Here TaskName is the name of Task, and Taskid is the unique identifier of this Task. With this identifier, you can reference the corresponding Task in the script. For example, you can do this in the script: