ANT Usage Guide (transfer)

xiaoxiao2021-03-06  55

Another exam connection: http://www.huihoo.com/java/hello-ant/index.html (Ant Practice Eric (Ericwq@263.net) 1. What is ANT? 2. Install ANT 3. Run Ant 4 Writing Build.xml 5. Built-in Task 6. Ear Task (Internet) 7. War Task 8. Junit Task (Internet) ----------------- -------------------------------------------------- ------------- 1. What is it? ------------------------------- ------------------------------------------------ Ant It is a Java-based build tool. Theoretically, it is similar to (UNIX) c, but there is no make defect. Since we already have made, gnumake, nmake, jam, and other build tools why What to have a new Build tool? Because the original author develops software on a variety of (hardware) platforms, it is impossible to endure the limitations and inconveniences of these tools. Similar to the Make's tool is based on the shell (language) : They calculate dependencies, then execute commands (these commands are not very different from you commands in commands). This means you can easily expand this by using OS unique or writing new (command) programs Tools; however, this also means that you will limit yourself on a specific OS, or on a specific OS type, such as Unix. Makefile is also very disgusting. Anyone who has used their people have encountered a hateful Tab. .Ant The original author often asked himself: "Is my order not to implement just because there is a space in front of my Tab ?!!". Tools similar to JAM have handled such problems well, but (users) must remember And use a new format. Ant is different. Different from the extended mode based on the shell command, ANT uses Java classes to extension. (Users) Do not need to write shell commands, the configuration file is based on XML, by calling the Target tree, Various task can be implemented. Each Task is run by an object that implements a specific TASK interface. (If you have no concept of Ant, you may not understand this section, there is no relationship, there will be Target , Task makes a detailed introduction. If there is not much time You can even skip this section, then come 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 does not use the concept in Make. ) Must admit that doing so, there will be some unique expressive capabilities 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, this task 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. 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 installing Ant Binary 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 Task Ant 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: / ant set java_home = c: /jdk1.2.2 set path =% PATH%;% ant_home% / bin unix (bash) assumes Ant installation in / usr / local / ant Under contents.

Below is a command to set the environment: export ant_home = / usr / local / ant export java_home = / usr / local / jdk-1.2.2 export path = $ {pat}: $ {ant_home} / bin Advanced To run Ant Use a lot of 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: 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 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 specified by the Target of the Default property specified. 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 Towards the root of the filesystem and use the root of the filend -dproperty = value set print --dproperty = value set print --dproperty = value set proteTy to value example ANT Using the build.xml in the current directory runs 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 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) Projects Project has the following properties: Attribute Description Required Name project name. No Default When the default Target Yes basedIr used when not specifying 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: If there is no IF or UNSS attribute, Target will always be executed. 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. Here is the general constructor of Task: Here Name is the name of Task, attribute is the property name, VALUEN is the property 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 do this: Set the Foo property of a Task instance. In another Task (written with Java), you can use the following statement to access the appropriate instance. Project.getReference ("task1"). Note 1: If Task1 has not yet been run, it will not take effect (for example: no attribute), if you are then configured, everything you do will be overwritten. Note 2: The future Ant version may not be compatible with the properties mentioned here, because it is likely that there is no Task instance at all, only PROXIES. Properties A Project can have a lot of Properties. You can set it in buildfile, or set outside ANT. A Property has a name and a value. Property can be used for the attribute value of Task. This is achieved by placing the property name between "$ {" and "}" and placed at the property value. For example, if there is a value of a Property BuildDir is "build", this property can be used for attribute values: $ {builddir} / classes. This value can be parsed to Build / Classes. Built-in attribute If you use Task to define all system properties, Ant allows you to use these properties. For example, $ {os.name} corresponds to the name of the operating system. To get a list of system properties, you can refer to The JavaDoc of System.getProperties. In addition to the Java system properties, Ant also defines some own built-in attributes: the absolute path of the BaseDir Project base record (like the BaseDir property). ANT.FILE BuildFile's absolute path. Ant.Version Ant version.

Ant.project.name The name of the current executed processing; setting the Name property. Ant.java.version Ant detected the version of JVM version; current value has "1.1", "1.2", "1.3" and "1.4 ". Example token Filters A Project can have a lot of tokens, which will be automatically extended when the file copy is filed, which requires the filter copy function in the TASK that supports this behavior. This feature can be set in BuildFile with Filter Task. Since this is likely to be a hazardous behavior, tokens in the file must take @ token @, here Token is the token name set in Filter Task. This token syntax does the same syntax similar to filtering, and it is not conflict with most programming and scripting languages ​​and document systems. Note: If you find a @ token @ in the form of Token in a file, there is no Filter is connected to this token, there is no problem; therefore, there is no escape method - but as long as you choose the right name for Token, it will not have problems. WARNING: If you open the filtering feature when you copy a binary file, you have possible destroying the file. This feature is only for text files. Path-Like Structures You can use ":" and ";" as a separator, specify a reference similar to PATH and ClassPath. ANT converts the separator to the separator used in the current system. Nesting elements can be used when you need to specify a value of similar paths. The general form is that the Location property specifies a file and directory relative to the Project base directory, and the Path property accepts a comma or a semicolon-separated list of locations. Path properties are generally used as a predefined path - in other cases, multiple location properties should be used. For the sake of simplicity, the ClassPath tag supports its own path and location attributes. So: You can be simply written: or the path can also be specified by the element. The order of multiple files constituting a FILESET adds to the order of path-like structure. The above example constructs a path value including: $ {classpath}, followed by all JAR files in the lib directory, followed by the classes directory. If you want to use the same path-like Structure in multiple task, you can define them with elements (with Target), then reference the reference property. Path-Like Structure may include reference to another path-like structure (through nested element): The simpler ways mentioned earlier are also effective, such as: written: command line variables Some task acceptable parameters, and Pass it to another process. In order to include a space character in the variable, nested Arg elements can be used. Attribute Description Required Value A command line variable; can contain space characters. You can only use a list of command line variables separated by a Line space. File as the file name of the command line variable; will be replaced by the absolute name of the file. A string of path-like as a single command line variable; or as a separator, Ant converts it into a separator of a particular platform. The example is a single command line variable containing spaces. It is two space separated command line variables.

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

New Post(0)