Ant practice - Linux environment

Contents
What 1. Ant that?
2. Install the Ant
3. Run the Ant
4. prepare the build.xml
5. The built Task (Internet)
6. The Task the EAR (Internet)
7. The Task the WAR (Internet)
8. The the JUnit Task (Internet)

-------- -------------------------------------------------- ----------------------

What 1. Ant that?
-------------------------------------------------- ------------------------------

Ant is a Java-based build tool. In theory, it is somewhat similar to (Unix) C in the make, but did not make the defect.

Now that we have make, gnumake, nmake, jam, and other build tools to why a new build tool? Because Ant's original author in a variety of (hardware) platform for the development of software, can not stand the restrictions and inconvenience of these tools. The tool is essentially similar to the make is based on the shell (language): they calculate dependencies and then execute the command (these commands and you is not much difference in the command line to knock command). This means that you can easily by using the OS-specific or write new (command) program expansion of the tool; however, it also means that you limit yourself to a specific OS, or a specific OS type, such as Unix.

Makefile also very hateful. Anyone who has used their hateful tab have encountered problems. Ant's original author often ask yourself: "Is my command not only perform in front of my tab because there is a space !!?." The tool is similar to jam a good deal with such problems, but (the user) must remember and use a new format.

Ant is different. And extended mode shell commands based on different, Ant with Java classes to extend. (Users) do not have to write shell commands, XML-based configuration files, by calling the target tree, you can perform various task. Each task achieved by an object that implements a particular Task interface to run. (If you have no idea what Ant, it may not read this section, it does not matter, would be behind target, task to do in detail. If you do not have much time to even skip this section, and then here are a look back, then you will understand the Likewise, it does not matter if you are not familiar with tools like to make the following description will not make use of the concept.)

it must be recognized that to do so when constructing the shell command will lose some unique skills. Such as `find -name foo -exec rm {} `, but it gives you the ability to cross-platform - you can work anywhere. If you really need to perform some shell commands, Ant has an <exec> task, this task allows execute commands on a specific OS.

Returns

2. Install Ant
--------------------------------------------- -----------------------------------

Because Ant is an Open Source software, so there are two installation Ant of ways, one is to install Ant use precompiled binary files, and the other is to use the source code yourself build Ant.

binary form from Ant http://jakarta.apache.org/builds/ant/release/v1.4.1/bin download. If you wish you could compile your own Ant, you can from http://jakarta.apache.org/builds/ant/release/v1.4.1/src . Note the connections listed are the latest release of Ant. If you read this, the updated version has been found, please use the new version. If you are a crazy technology suitors, you can also download the latest version of the repository Ant from the Ant CVS.

System Requirements

To own build Ant. You need a JAXP-compliant XML parser (parser) in your CLASSPATH system variable.

binary form of Ant includes the latest version of the Apache Crimson XML parser. You can start http://java.sun.com/xml/ get more information about the JAXP. If you want to use a different JAXP-compliant parser. From your Ant lib directory and delete jaxp.jar crimson.jar. Then you can be your favorite parser jar file into Ant's lib directory or in your CLASSPATH system variable.

For the current version of Ant, your system needs to have JDK, 1.1 or later. Future versions of Ant will require JDK 1.2 or higher.

Install Ant

Ant binary version includes three directories: bin, docs and lib. Only the bin and lib directories are required to run Ant. To install Ant, choose a directory and release the files are copied to the directory. This directory is called ANT_HOME.

Before you run Ant need to do some configuration work.

The bin directory to the PATH environment variable.
ANT_HOME set environment variables point to the directory of your Ant installation. On some OS, Ant scripts can guess ANT_HOME (Unix and Windos NT / 2000) - but it is best not to rely on this feature.
Optionally, set the JAVA_HOME environment variable (see below senior section), the variable should point to the directory of your JDK installation.

Note: Do not ant.jar file into Ant's JDK / JRE's lib / ext directory. Ant is an application, lib / ext directory extension JDK is used (e.g. JCE, JSSE extension). And by extending the class will be loaded to security restrictions.

Optional Task

Ant support some optional task. An optional task generally requires additional libraries to work. Ant optional task and task built separately, packaged separately. This optional package can be downloaded from the same place you downloaded the Ant. Currently jar file contains optional task named jakarta-ant-1.4.1-optional.jar. This jar should be placed in the lib directory of the Ant installation directory.

Optional task required for each external libraries can be found in the dependent libraries section. These libraries can be placed external Ant lib directory, so Ant will automatically load, or placed in the environment variable.

Windows

assumes that Ant is installed in c: ant directory. The following is the command to set the environment:

SET C = the ANT_HOME: Ant
SET the JAVA_HOME = C: jdk1.2.2
SET% = the PATH the PATH%;% the ANT_HOME% in
the Unix (the bash)

Ant assumed installed in / usr / local / ant directory. The following is the command to set the environment:

Export the ANT_HOME = / usr / local / Ant
Export the JAVA_HOME = / usr / local / JDK 1.2.2-
Export the PATH = $ {} the PATH: the ANT_HOME {$} / bin
advanced

in order to be run Ant use a lot of variables. You need at least refer to the following contents:

Ant's CLASSPATH must contain ant.jar and your chosen JAXP-compliant XML parser jar file.
When you need JDK functionality (e.g. javac or rmic task), for JDK 1.1, JDK classes.zip files must be placed in the CLASSPATH; for JDK 1.2 or JDK 1.3, must be added tools.jar. If you set the correct JAVA_HOME environment variable, Ant brought the script in the bin directory, will automatically add the required JDK classes.
When the program (such as exec task or the cvs task) you perform specific platforms must be set ant.home attribute points to the Ant installation directory. Similarly, Ant brought the script to use ANT_HOME environment variables automatically set the property.
Building Ant

order from the source code build Ant, you must first install the Ant source distribution or checkout jakarta-ant module from CVS.

After installing the source code, enter the installation directory.

Set JAVA_HOME environment variable to point to the JDK installation directory. To know how to do see Ant installation section.

Make sure you have downloaded any supporting jar files to build task you are interested in. These jar files can be placed in the CLASSPATH can be placed under lib / optional directory. See the dependencies section shows a different task requires those jar files. Note that these files are just used as a jar build Ant use. To run Ant, you have to like Ant installation section is set as the jar files do.

Now you can build Ant up:

Build -Ddist.dir = <directory_to_contain_Ant_distribution> dist (Windows)
build.sh -Ddist.dir = <directory_to_contain_Ant_distribution> dist (Unix)

so you can specify the directory to create a binary version.

The above command performs the following actions:

If necessary code may bootstrap Ant. Some Ant bootstrap code comprises manually edited to run Ant. used in the following step bootstrap build.
Pass parameters to the build script to call bootstrap Ant. Ant parameter defines the property value and specify the "dist" target Ant own build.xml file.

In most cases, you do not have direct bootstrap Ant, because build scripts for you to complete it all. Run bootstrap.bat (Windows) or bootstrap.sh (UNIX) can build a new bootstrap version of Ant.

If you want to install Ant to the next ANT_HOME directory, you can use:

install Build (Windows)
build.sh install (Unix)

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

Build install-Lite (Windows)
build.sh install-Lite (Unix)

This will only install the bin and lib directory .

Note install and install-lite will overwrite the current Ant version in ANT_HOME.

Dependent libraries

if you need to perform a specific task, you need the corresponding libraries into CLASSPATH or put under the lib directory of the Ant installation directory. Only you need to use a regexp library note mapper time. At the same time, you have to install Ant optional jar package, which contains the definition of the task. Ant installed above the reference section.

At the Name Needed the For the Available JAR
An XSL or the Transformer like the Xalan XSL: P style Task http://xml.apache.org/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-oro-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 stylebook task CVS repository 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. 运行Ant
-------------------------------------------------- ------------------------------

run Ant is very simple, when you correctly install Ant, just type ant on it.

When you do not specify any parameters, Ant will check build.xml file in the current directory. If found to use the file as buildfile. If you use -find option. Ant will find buildfile in the parent directory until the root file system arrives. To get Ant to use other buildfile, you can use the parameters -buildfile file, this file is specified buildfile you want to use.

You can set some properties, so as to cover the attribute value (see property task) buildfile specified. Can -Dproperty = value option, where property is the name of the property, and the value refers to the value of the property. You can also specify some of the values of environment variables in this way. You can also use the property task to access environment variables. Just -DMYVAR =% MYVAR% (Windows) or -DMYVAR = $ MYVAR (Unix) passed to Ant - can you with your $ {MYVAR} buildfile access to these environment variables.

There are two options -quite, tell Ant output only a small amount of information necessary to run. The -verbose, tell Ant to output more information at runtime.

You may perform one or more specified target. When the target is omitted, Ant using the tag <project> default attribute of the designated target.

If any, -projecthelp output option program descriptions and project target lists. Those listed first target is described, and then is not described.

命令行选项总结:

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 property to value
an example of

ant

use to run Ant build.xml in the current directory, execute the default target.

ant -buildfile test.xml

use test.xml run Ant in the current directory, execute the default target.

ant -buildfile test.xml dist

use test.xml run Ant in the current directory, called the execution a target dist.

ant -buildfile test.xml -Dbuild = build / classes dist

use test.xml run Ant current directory, perform a target dist called and set property is build build / classes.

Files

on Unix, execute Ant script before doing anything will source (read and calculated value) ~ / .antrc file; on Windows, Ant batch file invokes% HOME% antrc_pre.bat at the beginning, in % HOME% antrc_post.bat call at the end. You can configure or cancel some only if you need to run Ant environment variables using these files. See the examples below.

Environment Variables

wrapper script (wrapper scripts) using the following environment variables (if any):

absolute path JAVACMD Java executable file. This value can be specified by JAVA_HOME / bin / java (.exe) is different from a JVM.
ANT_OPTS passed to the JVM command line arguments - for example, you can define the properties or set the maximum Java heap

manual Ant run

if you install it yourself (DIY) Ant, Ant you can start with the following command:

the Java -Dant.home = c: ant org.apache.tools.ant.Main [options] [target]

this command ant previous command. Options and target and also when using ant command. This example assumes that your CLASSPATH includes:

ant.jar

JARs / classes for your XML Parser

at The JDKs required JAR / ZIP Files

returns
4. Write the build.xml
------------------ -------------------------------------------------- ------------

Ant's buildfile is written in XML. Each buildfile containing a project.

buildfile Each task can have an id attribute element can be referenced by the specified task id value. This value must be unique. (For details, please refer to the following Task section)

the Projects

Project has the following attributes:

the Attribute the Description the Required
name Name of Project No.
The default when no default target Yes Specifies the target
the basedir path group for calculating all the other path. This property may be covered basedir property. When covered, this attribute is ignored. If the property and basedir property are not set, use the parent directory buildfile file. No
description of the project appears to form a top-level <description> element (see section description).

A project can define one or more target. A target is a series that you want to perform. The implementation of Ant, you can choose to execute the target. When no given target, the default attribute project using the determined target.

Targets

A target can depend on other target. For example, you may have a target for the compiler, a target for generating executable files. You must first compile before producing an executable file, an executable file of the target depends on the compile target. Ant will deal with this dependency.

However, it should be noted, Ant's depends attribute only specifies the order target should be executed - if the target can not be relied upon to run, this depends to specify dependencies target has no effect.

Order (left to right) the Ant target will appear sequentially executed in accordance with each of the attribute depends target. However, keep in mind that whenever a target is dependent on a target, which will be executed first.

<target name = "A" />
<target name = "B" The depends = "A" />
<target name = "C" The depends = "B" />
<target name = "D" The depends = "C, B, A" />

Suppose we want to execute target D. From its dependency property point of view, you may think to perform C, then B, A is finally executed. Wrong, C depends on B, B is dependent on A, the first implementation of A, then B, then C, D is finally executed.

A target can only be performed once, a plurality of target depends on an instant it (see example above).

If you (or if you do not) certain properties have been set, before the implementation of a target. Thus, according to the state of the system allows (java version, OS, etc. attribute definition command) to better control the build process. To get a target to do so, you should target element, adding if (or unless) attribute, bring the target attribute has been judged due. For example:

<target name = "Build-Module1-A" IF = "Module1-A-Present" />
<target name = "Build-own-Fake-Module1-A" The unless = "Module1-A-Present" />

If there is no if or unless property, target will always be executed.

The optional description attribute line may be used to provide a description of the target, which can be described by -projecthelp output command line options.

Your tstamp task in a so-called initialization target is good practice to other target rely on this initialization target. To ensure that the initialization target is the first target in a target dependent on the other table appears. In this manual, most initialization target name is "init".

target has the following attributes:

the Attribute the Description the Required
name target name Yes
The depends comma-separated list of names with the target, which is dependent on the table. NO
IF execution attribute name required target set. NO
The unless execution target set by the property name need to be cleared. NO
the Description brief description of the target function. NO

Tasks

A task is a piece of executable code.

A task can have multiple attributes (if you wish, you can call it variable). Properties may only contain a reference to the property. These references will be resolved before the task execution.

The following is the general form of construction Task:

<attribute1 name = "VALUE1" attribute2 = "value2" ... />

where name is the name of the task, attributeN is the attribute name, valueN is the attribute value.

A set of built-in (built-in) task, as well as some optional task, but you can also write your own task.

All task has a task name attribute. Ant property values to produce a log information.

Id attribute can be assigned to a Task:

<TaskName id = "taskID" ... />

Here is taskname task name, and taskID is a unique identifier of this task. Through this identifier, you can refer to the appropriate task in the script. For example, in a script you can do:

<Script ...>
task1.setFoo ( "bar");
</ Script>

set foo properties of a task instance. In another task (written in Java), you can use the following statement accesses the instance.

project.getReference ( "task1").

Note 1: If task1 not run, will not be entered into force (for example: do not set the property), if you then configure it, you have done everything will be covered.

Note 2: Future versions may not be compatible Ant properties mentioned here, because most likely there is no task instance, only proxies.

Properties

a project can have a lot of properties. Buildfile may be set by the property task, or set outside Ant. A property has a name and a value. property of the task attribute values may be used. This is achieved by the attribute name in the "$ {" and "}" and placed between the position of the attribute values. For example, if there is a value of a property of the builddir "build", this property can be used for the attribute value: $ {builddir} / classes. This value can be resolved to build / classes.

Built-in property

if you use <property> task defines all system properties, Ant allows you to use these attributes. For example, $ {os.

To get a list of system attributes can refer to the Javadoc of System.getProperties.

In addition to Java system properties, Ant also defines some of its own built-in attributes:
absolute path basedir project base directory (the <project> basedir attribute of the same).
ant.file buildfile the absolute path.
version of ant.version Ant.
ant.project.name project name of a currently executed; name attribute set by <project> a.
ant.java.version the Ant version of the JVM detected; current values are "1.1", "1.2", "1.3" and "1.4".

examples of

<Project name = "MyProject" default = "dist" the basedir = ".">

<-! Global Properties SET for the this Build ->
"." <Property name = "the src" value = / >
<Property name = "Build" value = "Build" />
<Property name = "dist" value = "dist" />




<!-- Create the build directory structure used by compile -->
<mkdir dir="${build}"/>
</target>

<target name="compile" depends="init">
<!-- Compile the java code from ${src} into ${build} -->
<javac srcdir="${src}" destdir="${build}"/>
</target>

<target name="dist" depends="compile">
<!-- Create the distribution directory -->
<mkdir dir="${dist}/lib"/>
<!-- Put everything in ${build} into the MyProject-${DSTAMP}.jar file -->
<jar jarfile="${dist}/lib/MyProject-${DSTAMP}.jar" basedir="${build}"/>
</target>

<target name="clean">
<!-- Delete the ${build} and ${dist} directory trees -->
<Delete the dir = "$ {Build}" />
<Delete the dir = "$ {dist}" />
</ target>

</ project>

the Token the Filters

a project can have many tokens, these tokens when the file copy will be automatically extension, which requires selection filter function to copy the behavior of the task in support of this. This function can be set to filter task in buildfile.

Since this is likely to be a harmful behavior, file tokens must take the form @ token @, where token is the filter task set in the token name. This token syntax of other build systems perform similar filtering the same syntax, but also with most programming and scripting languages and documentation system does not conflict

Note: If you find a @ token @ token in the form of a file, but no filter connected to this token, anything that does not occur; therefore, there is no method of escape - but as long as you choose the right name for the token, it will not cause problems.

Warning: If you turn on filtering function when copy binary file, you may destroy files. This feature is only for text files.

Path-like Structures
you can use the ":" and ";" as a delimiter, and the specified PATH CLASSPATH similar references. Ant will convert the separator by the current system separator.

When the need to specify the value similar path, nested elements may be used. The general form is

<classpath>

<LOCATION of pathelement = "lib / helper.jar" />
</ CLASSPATH>
LOCATION attribute specifies a directory-based file and directory project with respect to the path attribute accepts a list of locations comma or semicolon separated. path path attribute is generally used as a pre-defined - in other cases, be a plurality of location attribute.

For brevity, classpath tag supports his path and location attributes. Therefore:

<CLASSPATH>
<path of pathelement = "$ {CLASSPATH}" />
</ CLASSPATH>
may be abbreviated:

<path CLASSPATH = "$ {CLASSPATH}" />
can also be specified by a path <fileset> element. Order of a plurality of files constituting a fileset added path-like structure is undetermined.

<CLASSPATH>
<path of pathelement = "$ {CLASSPATH}" />
<fileset the dir = "lib">
<name = the include "** / *. JAR" />
</ fileset>
<= LOCATION of pathelement "

Examples of the above configuration comprises a path value: $ {classpath} path, all the jar files along lib directory, followed classes directory.

If you want to use the same task in a plurality of path-like structure, you can define them (the target at the same level) with <path> element, and then by reference id attribute - Referencs reference example.

path-like structure may further include a reference path-like structurede (by nesting <path> element):

<path ID = "base.path">
< "{} $ CLASSPATH" of pathelement path = />
<fileset the dir = "lib">
<name = the include "** / *. JAR" />
</ fileset>
<LOCATION of pathelement = "classes" />
</ path>
<path ID = "tests.path">
<path the refid = "base.path" />
<LOCATION of pathelement = "testclasses" />
</ path>

mentioned earlier about <



<LOCATION of pathelement = "testclasses" />
</ path>
can be written as:

<path ID = "base.path" path = "$ {CLASSPATH}" />
command-line arguments

some task acceptable parameters, and pass it to another a process. In order to contain the variable space character may be used nested arg elements.

The Description the Required the Attribute
value a command-line variable; can contain space characters. You can only use one
-separated list of command line space line variables.
file filename as a command-line variable; it will be replaced with the absolute name of the file.
a string of path-like path as a single command line variable; or as a separator, Ant will be converted into platform-specific delimiter.

Examples

<arg value = "- l -a " />
is a box containing a single command line arguments.

<arg line = "- l -a " />
is a space-separated two command-line arguments.

<arg path = "/ dir; / dir2: dir3" />
is a command variable, its value on DOS systems dir; dir2; dir3; on Unix systems / dir: / dir2:



buildfile id attribute element can be used to refer to these elements. If the same copy of the block of XML code you need to over and over again, this is useful properties - such as multi-use <classpath> structure.

Following example:

<Project ...>
<target ...>
<... the rmic>
<CLASSPATH>
<LOCATION of pathelement = "lib /" />
<path of pathelement = "$ {} the java.class.path /" />
<path of pathelement = "$ {additional.path}" />
</ CLASSPATH>
</ the rmic>
</ target>
<target ...>
<... the javac>
<CLASSPATH>
<LOCATION of pathelement = "lib / "/>
<path of pathelement =" $ {} the java.class.path / "/>
<path of pathelement =" $ {additional.path} "/>






<project ... >
<path id="project.class.path">
<pathelement location="lib/"/>
<pathelement path="${java.class.path}/"/>
<pathelement path="${additional.path}"/>
</path>
<target ... >
<rmic ...>
<classpath refid="project.class.path"/>
</rmic>
</target>
<target ... >
<javac ...>
<classpath refid="project.class.path"/>
</javac>
</target>
</project>
所有使用PatternSets, FileSets 或 path-like structures嵌套元素的task也接受这种类型的引用。

Reproduced in: https: //www.cnblogs.com/licheng/archive/2008/11/08/1329617.html

Guess you like

Origin blog.csdn.net/weixin_34240520/article/details/92633108