You are on page 1of 33

Ant

ANT

Java 1 http://www.javafan.net
Ant

TM
Ant Java Ant Java

Ant Ant

build file Java

Ant

Ant Java

Eclipse IDE

Ant

Ant Ant Ant

Ant Eclipse Ant

Eclipse Eclipse Eclipse.org Eclipse

Java Ant

Ant

Ant

Ant Ant

CVS

Java 2 http://www.javafan.net
Ant
Ant Java

Ant

Ant Ant

Matt Chapman 1996 Hursley IBM Centre for Java Technology

Java Java Swing

AWT Eclipse Matt Sun

Java mchapman@uk.ibm.com

Ant

Ant

Ant

Ant

Apache Ant Java James Duncan Davidson

another neat tool

Java 3 http://www.javafan.net
Ant
C C++ make shell

Ant make shell

Java Ant

Java Ant Java

Ant

Ant

Ant

Ant

Ant Tomcat Tomcat Java Servlet JavaServer Pages (JSP)

servlet Tomcat Apache

Apache Jakarta Java

Ant Jakarta

Jakarta 2000 7

Ant

Java 2002 11 Ant Apache

Ant 1.5.4 1.1 JDK

1.6 beta JDK 1.2 2.0

Ant 2.0

Ant

Ant

Java 4 http://www.javafan.net
Ant
Ant XML Ant

XML Ant

project target

target

target target

target

project default Ant

target

<?xml version="1.0"?>
<project default="init">
<target name="init">
</target>
</project>

XML XML XML

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 comments can also be used -->


<target name="init" description="Initialize Argon database">
<!-- perform initialization steps here -->
</target>
</project>

XML Ant

description description

Ant

Ant Java String

Ant

Ant

<property name="metal" value="beryllium"/>

${metal}

<property name="metal-database" value="${metal}.db"/>

Java 6 http://www.javafan.net
Ant
Ant Java Ant Ant

${user.home} Ant

${ant.version} Ant ${basedir}

project basedir

/ \

Ant location

location value

<property name="database-file" location="archive/databases/${metal}.db"/>

location

<property name="database-file" location="archive\databases\${metal}.db"/>

DOS

Java

Java Archive FileJAR

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

Ant compile init preprocess

init Ant preprocesstarget

init Ant preprocess

compile

depends

ANT

Apache Ant Ant

UNIX Linux shell Windows

Ant IDE

IDE 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

make makefile Ant

build.xml

Ant -buildfile <file>

-f <file>

-D

name

-Dmetal=beryllium

-D

Java 9 http://www.javafan.net
Ant

IDE

Ant IDE IDE

JEdit Jext Borland JBuilderIntelliJ IDEAJava

Development Environment for Emacs (JDEE)NetBeans IDEEclipse WebSphere Studio

Application Developer

Ant IDE

Eclipse Ant

Eclipse Ant

Eclipse Ant Eclipse Ant

<pro Ctrl-Space

<property>

Ant Ant

Java 10 http://www.javafan.net
Ant

Eclipse Ant

build.xml Eclipse Ant

Run Ant...

Run AntEclipse Console

Java 11 http://www.javafan.net
Ant

Eclipse

Eclipse build.xml Ant

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

Ant Java javac

Java Java

<javac srcdir="src"/>

src .java javac

destdir Ant

classpath javac -classpath


debug="true"

javac

javac

clean

Ant

Ant javac Java Ant javac

Ant Java JVM

Java javac JVM

Ant JVM Ant

JAR

Ant javac Ant JVM

Java 14 http://www.javafan.net
Ant
javac fork

true

<javac srcdir="src" fork="true"/>

javac

<javac srcdir="src" fork="true" executable="d:\sdk141\bin\javac"


memoryMaximumSize="128m"/>

Ant Jikes

GNU GNU Compiler CollectionGCC GCI

build.compiler

javac javac compiler

javac Ant

JAR

Java JAR zip

JAR JAR

Ant jar

<jar destfile="package.jar" basedir="classes"/>

package.jar JAR classes JAR

Ant

manifest JAR manifest

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/>

tstamp Ant tstamp


DSTAMP yyyymmdd 20031217
TSTAMP hhmm 1603
TODAY 2003 12 17

JAR

<jar destfile="package.jar" basedir="classes"/>

tstamp JAR

<jar destfile="package-${DSTAMP}.jar" basedir="classes"/>

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

Java src Hell World

Java classpath JAR

javac classpath

<?xml version="1.0"?>
<project default="dist" name="Project Argon">
<description>A simple Java project</description>

<property name="srcDir" location="src"/>


<property name="buildDir" location="build"/>
<property name="distDir" location="dist"/>

<target name="init">
<tstamp/>
<mkdir dir="${buildDir}"/>
<mkdir dir="${distDir}"/>
</target>

Java 17 http://www.javafan.net
Ant

<target name="compile" depends="init">


<javac srcdir="${srcDir}" destdir="${buildDir}"/>
</target>

<target name="dist" depends="compile">


<jar destfile="${distDir}/package-${DSTAMP}.jar" basedir="${buildDir}">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-Class" value="package.Main"/>
</manifest>
</jar>
<jar destfile="${distDir}/package-src-${DSTAMP}.jar" basedir="${srcDir}"/>
</target>

<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

java -jar package-20031217.jar JAR

Ant Java

Ant

Ant

mkdir

Windows UNIX/Linux

<mkdir dir="archive/metals/zinc"/>

/ UNIX Linux

Ant

\ Ant

mkdir

archive metals mkdir

metals mkdir zinc Ant

Java 19 http://www.javafan.net
Ant
mkdir

<delete dir="archive/metals/zinc"/>

file dir

Ant

<copy file="src/Test.java" tofile="src/TestCopy.java"/>

move

<move file="src/Test.java" tofile="src/TestCopy.java"/>

Ant

<copy file="src/Test.java" todir="archive"/>


<move file="src/Test.java" todir="archive"/>

Ant

verbose true

zip tar

JAR

zip Ant

<zip destfile="output.zip" basedir="output"/>

Java 20 http://www.javafan.net
Ant

tar GZip BZip

<gzip src="output.tar" zipfile="output.tar.gz"/>

<unzip src="output.tar.gz" dest="extractDir"/>

overwrite

untarunjargunzip bunzip2

replace

token value

<replace file="input.txt" token="old" value="new"/>

summary

true

Ant

CVS

Java 21 http://www.javafan.net
Ant

.java

DOS UNIX Ant

* ? .java

*.java

src*/*.java src

Java ** **/*.java

Java

fileset

<copy file="src/Test.java" todir="archive"/>

file fileset

<copy todir="archive">
<fileset dir="src">
<include name="*.java"/>
</fileset>
</copy>

fileset src Java

include exclude

include include exclude

include exclude

CVS

defaultexcludes no

Java 22 http://www.javafan.net
Ant

include exclude

include exclude

Ant

size units

when lessmore equalvalue

contains text

casesensitive="no"

filenamename include

negate="yes" exclude

present targetdir

depend present

targetdir

datewhen before

after equaldatetime

MM/DD/YYYY HH:MM AM_or_PM Windows 2

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>

ant Ant Ant

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

CVS concurrent versions system

Ant

CVS

Ant cvs 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>

cvs cvsRoot CVS

[:method:][[user][:password]@]hostname[:[port]]/path/to/repository

Eclipse

CVS

command

CVS

ANT

Ant

Ant-Contrib

Apache Ant Ant

Ant

Java 26 http://www.javafan.net
Ant

Ant

Java Java Ant exec

Ant

org.apache.tools.ant.Task

execute()

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class FileSorter extends Task {


// The method executing the task
public void execute() throws BuildException {}
}

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

// The setter for the "file" attribute


public void setFile(File file) {}

// The setter for the "tofile" attribute


public void setTofile(File tofile) {}

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;

// The method executing the task

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);
}
}

// The setter for the "file" attribute


public void setFile(File file) {
this.file = file;
}
// The setter for the "tofile" attribute
public void setTofile(File tofile) {
this.tofile = tofile;
}
}

setter execute()

Java 29 http://www.javafan.net
Ant

I/O BuildExceptions

javac IDE Ant

ant.jar classpath Ant

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

setter java.io.File java.lang.String

output.txt

And here is another one


Hello there
This is a line

Ant file not found

Ant

Java 31 http://www.javafan.net
Ant

Ant Ant

Ant

Ant

Ant

Eclipse Ant

JAR
Java

Ant


CVS

Java Ant

Ant Ant Ant

Java 32 http://www.javafan.net
Ant

Apache Ant Ant


Ant IDE

Eclipse

developerWorks Eclipse

XML developerWorksXML

Cygwin Windows Linux

CVS

WebSphere Studio Application Developer Eclipse

WebSphere Application ServerAnt Ant

JAR files revealed JAR developerWorks2003 10


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

Ant Jikes GNU Compiler for JavaGCJ

developerWorks Java Java

Java developerWorks Java


[]

Java 33 http://www.javafan.net

You might also like