Professional Documents
Culture Documents
Ant使用指南
Ant使用指南
ANT
Java 1 http://www.javafan.net
Ant
TM
Ant Java Ant Java
Ant Ant
Ant
Ant Java
Eclipse IDE
Ant
Java Ant
Ant
Ant
Ant Ant
CVS
Java 2 http://www.javafan.net
Ant
Ant Java
Ant
Ant Ant
Java mchapman@uk.ibm.com
Ant
Ant
Ant
Ant
Java 3 http://www.javafan.net
Ant
C C++ make shell
Java Ant
Ant
Ant
Ant
Ant
Ant Jakarta
Jakarta 2000 7
Ant
Ant 2.0
Ant
Ant
Java 4 http://www.javafan.net
Ant
Ant XML Ant
XML Ant
project target
target
target target
target
target
<?xml version="1.0"?>
<project default="init">
<target name="init">
</target>
</project>
Ant
target
<target name="init"/>
Java 5 http://www.javafan.net
Ant
<?xml version="1.0"?>
<project default="init" name="Project Argon">
<description>
A simple project introducing the use of descriptive tags in Ant build files.
</description>
XML Ant
description description
Ant
Ant
Ant
${metal}
Java 6 http://www.javafan.net
Ant
Ant Java Ant Ant
${user.home} Ant
project basedir
/ \
Ant location
location value
location
DOS
Java
target Ant
make
target depends
<target name="init"/>
<target name="preprocess" depends="init"/>
<target name="compile" depends="init,preprocess"/>
<target name="package" depends="compile"/>
Java 7 http://www.javafan.net
Ant
Ant
compile
depends
ANT
Ant IDE
IDE Ant
Ant
Eclipse Ant
Eclipse
Ant
Ant antAnt
Ant
Ant
Java 8 http://www.javafan.net
Ant
Ant
Buildfile: build.xml
init:
[mkdir] Created dir: E:\tutorials\ant\example\build
[mkdir] Created dir: E:\tutorials\ant\example\dist
compile:
[javac] Compiling 8 source files to E:\tutorials\ant\example\build
dist:
[jar] Building jar: E:\tutorials\ant\example\dist\example.jar
BUILD SUCCESSFUL
Total time: 2 seconds
build.xml
-f <file>
-D
name
-Dmetal=beryllium
-D
Java 9 http://www.javafan.net
Ant
IDE
Application Developer
Ant IDE
Eclipse Ant
Eclipse Ant
<pro Ctrl-Space
<property>
Ant Ant
Java 10 http://www.javafan.net
Ant
Eclipse Ant
Run Ant...
Java 11 http://www.javafan.net
Ant
Eclipse
Window=>Preferences Workbench
File Associations
mybuild.xml .xml
plugin.xml Eclipse
*.xml Ant
editor
Java 12 http://www.javafan.net
Ant
JAVA
Ant Ant
Java
JAR Ant
Java 13 http://www.javafan.net
Ant
Java Java
<javac srcdir="src"/>
destdir Ant
javac
javac
clean
Ant
JAR
Java 14 http://www.javafan.net
Ant
javac fork
true
javac
Ant Jikes
build.compiler
javac Ant
JAR
JAR JAR
Ant jar
Ant
jar
JAR
Java 15 http://www.javafan.net
Ant
<jar destfile="package.jar" basedir="classes">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-class" value="package.Main"/>
</manifest>
</jar>
JAR zip
tstamp
init <tstamp/>
DSTAMP yyyymmdd 20031217
TSTAMP hhmm 1603
TODAY 2003 12 17
JAR
tstamp JAR
2003 12 17 JAR
package-20031217.jar
Java 16 http://www.javafan.net
Ant
tstamp
format
<tstamp>
<format property="OFFSET_TIME"
pattern="HH:mm:ss"
offset="10" unit="minute"/>
</tstamp>
OFFSET_TIME 10
java.text.SimpleDateFormat
Java
src build
dist JAR
javac classpath
<?xml version="1.0"?>
<project default="dist" name="Project Argon">
<description>A simple Java project</description>
<target name="init">
<tstamp/>
<mkdir dir="${buildDir}"/>
<mkdir dir="${distDir}"/>
</target>
Java 17 http://www.javafan.net
Ant
<target name="clean">
<delete dir="${buildDir}"/>
<delete dir="${distDir}"/>
</target>
</project>
src
Buildfile: build.xml
init:
[mkdir] Created dir: E:\tutorial\javaexample\build
[mkdir] Created dir: E:\tutorial\javaexample\dist
compile:
[javac] Compiling 10 source files to E:\tutorial\javaexample\build
dist:
[jar] Building jar: E:\tutorial\javaexample\dist\package-20031217.jar
[jar] Building jar: E:\tutorial\javaexample\dist\package-src-20031217.jar
BUILD SUCCESSFUL
Total time: 5 seconds
Java 18 http://www.javafan.net
Ant
JAR
Ant Java
Ant
Ant
mkdir
Windows UNIX/Linux
<mkdir dir="archive/metals/zinc"/>
/ UNIX Linux
Ant
\ Ant
mkdir
Java 19 http://www.javafan.net
Ant
mkdir
<delete dir="archive/metals/zinc"/>
file dir
Ant
move
Ant
Ant
verbose true
zip tar
JAR
zip Ant
Java 20 http://www.javafan.net
Ant
overwrite
untarunjargunzip bunzip2
replace
token value
summary
true
Ant
CVS
Java 21 http://www.javafan.net
Ant
.java
* ? .java
*.java
src*/*.java src
Java ** **/*.java
Java
fileset
file fileset
<copy todir="archive">
<fileset dir="src">
<include name="*.java"/>
</fileset>
</copy>
include exclude
include exclude
CVS
defaultexcludes no
Java 22 http://www.javafan.net
Ant
include exclude
include exclude
Ant
size units
contains text
casesensitive="no"
filenamename include
negate="yes" exclude
present targetdir
depend present
targetdir
datewhen before
after equaldatetime
granularity
depthmin / max
Java 23 http://www.javafan.net
Ant
and
ornotnone majority
512 hello
<fileset dir="dir">
<and>
<contains text="hello"/>
<size value="512" when="more"/>
</and>
</fileset>
antfile Ant
<ant antfile="sub-build.xml"/>
inheritAll="false" property
inheritAll false
<?xml version="1.0"?>
<project default="showMessage">
<target name="showMessage">
<echo message="Message=${message}"/>
</target>
</project>
Java 24 http://www.javafan.net
Ant
echo
message
<?xml version="1.0"?>
<project default="callSub">
<target name="callSub">
<ant antfile="sub.xml" target="showMessage" inheritAll="false">
<property name="message" value="Hello from parent build"/>
</ant>
</target>
</project>
Buildfile: build.xml
callSub:
showMessage:
[echo] Message=Hello from parent build
BUILD SUCCESSFUL
Total time: 0 seconds
CVS
Ant
CVS
Linux Windows
Cygwin Cygwin
Java 25 http://www.javafan.net
Ant
CVS
<?xml version="1.0"?>
<project name="CVS Extract" default="extract" basedir=".">
<property name="cvsRoot" value=":pserver:anonymous@dev.eclipse.org:/home/eclipse"/>
<target name="extract">
<cvs cvsRoot="${cvsRoot}"
package="org.eclipse.swt.examples"
dest="${basedir}"/>
</target>
</project>
[:method:][[user][:password]@]hostname[:[port]]/path/to/repository
Eclipse
CVS
command
CVS
ANT
Ant
Ant-Contrib
Ant
Java 26 http://www.javafan.net
Ant
Ant
Ant
org.apache.tools.ant.Task
execute()
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
execute() BuildException
Ant
file tofile
Ant
Ant set
Java 27 http://www.javafan.net
Ant
setFile() setTofile() Ant
setter
setter
Ant
java.io.File
Ant
Ant
boolean int
Ant
setter
import java.io.*;
import java.util.*;
import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;
/**
* A simple example task to sort a file
*/
public class FileSorter extends Task {
private File file, tofile;
Java 28 http://www.javafan.net
Ant
public void execute() throws BuildException {
System.out.println("Sorting file="+file);
try {
BufferedReader from =
new BufferedReader(new FileReader(file));
BufferedWriter to =
new BufferedWriter(new FileWriter(tofile));
List allLines = new ArrayList();
// read in the input file
String line = from.readLine();
while (line != null) {
allLines.add(line);
line = from.readLine();
}
from.close();
// sort the list
Collections.sort(allLines);
// write out the sorted list
for (ListIterator i=allLines.listIterator(); i.hasNext(); ) {
String s = (String)i.next();
to.write(s); to.newLine();
}
to.close();
} catch (FileNotFoundException e) {
throw new BuildException(e);
} catch (IOException e) {
throw new BuildException(e);
}
}
setter execute()
Java 29 http://www.javafan.net
Ant
I/O BuildExceptions
lib
Ant
classpath taskdef
<taskdef name="filesorter"
classname="FileSorter"
classpath="."/>
Ant
<?xml version="1.0"?>
<project name="CustomTaskExample" default="main" basedir=".">
<taskdef name="filesorter"
classname="FileSorter
classpath="."/>
<target name="main">
<filesorter file="input.txt" tofile="output.txt"/>
</target>
</project>
input.txt
Hello there
This is a line
Java 30 http://www.javafan.net
Ant
And here is another one
Buildfile: build.xml
main:
[filesorter] Sorting file=E:\tutorial\custom\input.txt
BUILD SUCCESSFUL
Total time: 0 seconds
input.txt
output.txt
Ant
Java 31 http://www.javafan.net
Ant
Ant Ant
Ant
Ant
Ant
Eclipse Ant
JAR
Java
Ant
CVS
Java Ant
Java 32 http://www.javafan.net
Ant
Ant IDE
Eclipse
developerWorks Eclipse
XML developerWorksXML
CVS
developerWorks Ant
o Incremental development with Ant and JUnitMalcolm Davis 2000
11
o Extending Ant to support interactive buildsAnthony Young-Garner
2001 11
o Enhance Ant with XSL transformationsJim Creasman 2003 9
[]
Java 33 http://www.javafan.net