1. 程式人生 > >Eclipse下使用Ant

Eclipse下使用Ant

目前的Eclipse都集成了ant,本文圖示如何在eclipse下使用ant。 1.新建Java Project-新建Java檔案HelloWorld.java HelloWorld.java
packageexample; publicclassHelloWorld { publicstaticvoidmain(String[] args) { System.out.println("Hello World"); } }
2.在工程根目錄下新建build.xml build.xml
<?xml version="1.0" encoding="utf-8"?> <projectdefault="main"basedir="."> <targetname="main"depends="compile, compress"
description="Main target"> <echo>Building the .jar file.</echo> </target> <targetname="compile"description="Compilation target"> <javacsrcdir="${basedir}/src/example"/> </target> <targetname="compress"description="Compression target"> <jarjarfile="HelloWorld.jar"
basedir="${basedir}/src/example"includes="*.class"/> </target> </project>
此指令碼檔案內容是編譯/src/example下的java檔案,並就地生成class檔案,將這個class檔案打成jar包,HelloWorld.jar。 此時工程的目錄結構如下圖所示: 右鍵選中HelloAnt工程,選擇Properties: 選擇Builders-New…,選擇Ant Build, Name:Ant_Builder; Buildfile:${workspace_loc:/HelloAnt/build.xml}; Base Directory:${workspace_loc:/HelloAnt}; (按“Browse Workspace”選擇工程根目錄)
在Builder面板中鉤上Ant_Build,去掉Java Builder,即可編譯執行。 每次編譯時,右鍵build.xml,選擇Run As-Ant Build: 此示例工程編譯結果: Buildfile: D:\dev\Workspaces\J2EE\HelloAnt\build.xml compile: compress: main: [echo] Building the .jar file. BUILD SUCCESSFUL Total time: 281 milliseconds