標籤:des style blog http java color
目前的Eclipse都整合了ant,但是如何在Eclipse下使用ant呢?
1.建立Java Project-建立Java檔案HelloWorld.java
HelloWorld.java:
package example;public class HelloWorld { public static void main(String[] args) { System.out.println("Hello World"); }}
View Code
2.在工程根目錄下建立build.xml
build.xml:
<?xml version="1.0" encoding="utf-8"?><project default="main" basedir="."> <target name="main" depends="compile, compress" description="Main target"> <echo>Building the .jar file.</echo> </target> <target name="compile" description="Compilation target"> <javac srcdir="${basedir}/src/example" /> </target> <target name="compress" description="Compression target"> <jar jarfile="HelloWorld.jar" basedir="${basedir}/src/example"includes="*.class" /> </target></project>
View Code
此指令檔內容是編譯/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: F:\workfile\ant_test\build.xmlcompile: [javac] F:\workfile\ant_test\build.xml:7: warning: ‘includeantruntime‘ was not set, defaulting to build.sysclasspath=last; set to false for repeatable buildscompress:main: [echo] Building the .jar file.BUILD SUCCESSFULTotal time: 1 second
View Code