Introduction to Ant

xiaoxiao2021-03-06  52

What is ANT? 2. Install ANT3. Run ANT4. Write build.xml5. Built-in Task 6. Ear Task 7. War Task 8. JUnit Task (Internet) ----------- -------------------------------------------------- ------------------- 1. What is it? -------------------------------------------------- ---------------------------- Ant is a Java-based build tool. 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. 2. Install ANT ---------------------------------- ---------------------------------- Since Ant is an Open Source software, there are two installation Ant The way, one is to install Ant with the 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. 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. The following is a command to set the environment: set ant_home = c: / antset java_home = c: /jdk1.2.2set path =% path%;% ant_home% / binunix (bash) assumes that Ant is installed in / usr / local / ant directory . The following is a command to set the environment: export ant_home = / usr / local / antient / java_home = / usr / local / jdk-1.2.2export path = $ {pat}: $ {ant_home} / bin Advance To run Ant Many Variables. You at least reference requires the following content: 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 of: build -Ddist.dir = dist (Windows) build.sh -Ddist.dir = dist (Unix) so you can specify the directory to create a binary version. The above command performs the following action: if it is necessary, it is necessary 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 an Ant_Home directory, you can use: build install (unix) Build.sh Install If you want to skip lengthy Javadoc steps, you can use: Build Install-Lite (Windows) build.sh Install-Lite (UNIX) will only have bin and lib directorys. 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 Name Needed for available at an xsl transformer like xalan or xsl: p Style Task http://xml.apache.rg/xalan-j/index.html or http://www.clc-marketing.com/xslp/ jakarta -regexp-1.2.jar regexp type with mappers jakarta.apache.org/regexp/ jakarta-orom-2.0.1.jar regexp type with mappers and the perforce tasks jakarta.apache.org/oro/ junit.jar Junit Tasks WWW. Junit.org stylebook.jar style of xml.apache.org testlet.jar test task java.apache.org/framework antlr.jar ANTLR TASK www.antlr.org bsf.jar script task oss.software.ibm.com / developerworks / projects / bsf netrexx.jar netrexx task www2.hursley.ibm.com/netrexx rhino.jar javascript with script task www.mozilla.org jpython.jar python with script task www.jpython.org netcomponents.jar ftp and telnet Tasks www.savarese.org/oro/downloads 3. Run Ant ------------------------------------ -------------------------------------------- Run Ant is very simple, After you install Ant correctly, 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 print - DPRUE Example Ant Use the build.xml in the current directory runs Ant, performs 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 of Dist runs Ant using Test.xml in the current directory, performs a Target called DIST. ANT -BUILDFILE TEST.XML -DBUILD = BUILD / CLASSES DIST Use the Test.xml in the current directory running Ant, perform a Target called Dist and set the value of the build property of the build property. 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 maximum of the attribute or set the maximum of Java stacks Manually run Ant If you do your own installation (DIY) Ant, you can start Ant: java -dant.home with the following command = 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 your classpath contains: ant.jar jars / classes for Your XML Parser The JDK's Required Jar / Zip Files 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: Attribute Description Required Name project name. No Default When the default Target Yes basedIr used when not specified Target is used to calculate the base path of all other paths. 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. The description of the NO 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. For example: 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: Attribute Description Required Name Target's names Yes Depends The name of the name separated by a comma, is the dependent table. NO if executes the name of the attribute name you need to be set. No Unless executes the Target to clear the set properties name. NO Description 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: This is the name of Task, attribute is the property name, VALUEN is the 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, in the script you can: