创建一个java 工程,目录结构如下:
F:\files\study\Ant>tree
.
├── bin
│ └── com
│ └── huaqin
│ ├── Test.class
│ └── User.class
├── build
│ └── classes
│ └── com
│ └── huaqin
│ ├── Test.class
│ └── User.class
├── build.xml
├── lib
│ └── json.jar
├── out
└── src
└── com
└── huaqin
├── Test.java
└── User.java
12 directories, 8 files
Test.java 文件如下:
package com.huaqin;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!");
try {
JSONObject mUserObject = new JSONObject("{'name':'tome',}");
String mName = mUserObject.optString("name");
System.out.println("name : "+ mName);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
build.xml 文件如下:
<?xml version="1.0" ?>
<project name ="TestAnt" default="runPtint" basedir=".">
<path id="jsonPackage" description="the path of package">
<fileset dir="${basedir}/lib">
<include name="*.jar"/>
</fileset>
</path>
<target name="clean" description="clear compiled file" >
<delete dir="${basedir}/build"/>
<delete dir="${basedir}/out"/>
</target>
<target name="compilePrint" depends ="clean" >
<mkdir dir ="${basedir}/build/classes"/>
<javac srcdir ="${basedir}/src" destdir ="${basedir}/build/classes" includeAntRuntime="false">
<classpath refid="jsonPackage"/>
</javac>
</target>
<target name="runPtint" depends ="compilePrint">
<java fork="true" failonerror="yes" classname="com.huaqin.Test">
<classpath >
<pathelement path="${basedir}/build/classes/"/>
<fileset dir="${basedir}/lib">
<include name="json.jar"/>
</fileset>
</classpath>
</java>
</target>
</project>
执行的结果
F:\files\study\Ant>
F:\files\study\Ant>ant clean
Buildfile: F:\files\study\Ant\build.xml
clean:
[delete] Deleting directory F:\files\study\Ant\build
[delete] Deleting directory F:\files\study\Ant\out
BUILD SUCCESSFUL
Total time: 0 seconds
F:\files\study\Ant>
F:\files\study\Ant>
F:\files\study\Ant>ant compilePrint
Buildfile: F:\files\study\Ant\build.xml
clean:
compilePrint:
[mkdir] Created dir: F:\files\study\Ant\build\classes
[javac] Compiling 2 source files to F:\files\study\Ant\build\classes
BUILD SUCCESSFUL
Total time: 1 second
F:\files\study\Ant>
F:\files\study\Ant>ant runPtint
Buildfile: F:\files\study\Ant\build.xml
clean:
[delete] Deleting directory F:\files\study\Ant\build
compilePrint:
[mkdir] Created dir: F:\files\study\Ant\build\classes
[javac] Compiling 2 source files to F:\files\study\Ant\build\classes
runPtint:
[java] Hello World!
[java] name : tome
BUILD SUCCESSFUL
Total time: 2 seconds
F:\files\study\Ant>
上面的案例是一个简单的 ant 任务的配置和运行的内容,
列举了文件的删除(delete),加载(inclues)和编译(javac)、运行的简单过程(java)