1.建立一個ant 項目目錄結構:
/home/nh/helloworld :項目主目錄
/src :來源程式目錄
/classes :編譯後的 class 檔案目錄
/lib :開發項目所需要的類庫目錄,比如開發資料庫時所需要的 jdbc lib(這次沒用到)
/jar :打包好的 jar 程式目錄(這次沒用到)
/build.xml :項目設定檔
build.xml內容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<project default="build" basedir=".">
<property name="appname" value="helloworld" />
<property name="base" value="." />
<property name="src" value="src" />
<property name="build" value="classes" />
<property name="myclasspath" value="${build}" />
<target name="build">
<mkdir dir="${build}" />
<javac classpath="$myclasspath" srcdir="${src}" destdir="${build}">
<include name="helloworld.java" />
</javac>
</target>
<target name="run" depends="build">
<exec executable="java">
<arg line=" -classpath ${myclasspath} helloworld" />
</exec>
</target>
<target name="clean">
<delete includeEmptyDirs="true">
<fileset dir="${build}"/>
</delete>
</target>
</project>
2.建立 helloworld.java
/home/nh/helloword/src/helloworld.java ,源碼如下:
-----------------------------------------------------------------------------
/**
* Demo class for run HelloWorld by ANT
*/
public class HelloWorld {
public static void main(String args[]){
System.out.println ( "Hello World! ");
}
} //EOC
-----------------------------------------------------------------------------
3.編譯 helloworld .java
nh@57ubuntu:~/helloworld$ ant build
Buildfile: build.xml
build:
[javac] Compiling 1 source file to /home/nh/helloworld/classes
BUILD SUCCESSFUL
Total time: 1 second