Eclipse構建Ant專案
阿新 • • 發佈:2018-11-11
1、用 Ant 來構建簡單系統
新建一個 Java project, 命名為Hello
Hello .java 檔案內容
package example;
public class Hello {
public static void main(String[] args) {
System.out.println("我的專案是ant執行的");
}
}
bulid.xml 檔案內容
<?xml version="1.0" encoding="UTF-8"?> <project name="project" default="default"> <target name="default" depends="depends" description="description"> <javac srcdir="src" destdir="bin" includes="org/**" includeantruntime="true"></javac> <jar basedir="bin" destfile="dist/ant_project.jar"></jar> </target> <target name="depends"> </target> </project>
熟悉 ant 的同學們對於上面的指令碼應該很容易看明白,這裡就不詳細解釋了,主要功能就是把這個工程編譯然後打成 jar 和 war 包。
2、運用ant
(1)eclipse-右鍵專案-properties-Builders-Add buids(選擇 ant builder):
(2)Buildfile-點選選擇專案下的build.xml檔案,生成路徑為:${workspace_loc:/ant_project/build.xml}
Base directory-點選選擇專案名字,生成路徑為:${workspace_loc:/ant_project}
(3)當前頁面切換到classPath下,add jar(junit.jar,這個jar之前先放在ant_project下面)
(4)去掉java builder勾選,選擇ant builder(可以去掉,也可以不去掉)
(5)點選build.xml右鍵—run as —-ant build 開始執行ant命令,構建ant專案以及打包工作。
3.上面只是打包了,下面我們做一個打包並且執行的
只需要修改build.xml就可以了
<?xml version="1.0" encoding="UTF-8" ?> <!-- ant的所有內容必須包含在這個裡邊,name是你給它取的名字,basedir故名思意就是工作的根目錄 .代表當前目錄。default代表預設要做的事情。 --> <project name="Hello" default="run" basedir="."> <property name="mySrc" value="src"/> <property name="myClassDirName" value="classes"/> <property name="myBuildJarName" value="helloant.jar"/> <!-- 第一步:初始化 把你想做的每一件事情寫成一個target ,它有一個名字,depends是它所依賴的target,在執行這個target 例如這裡的compile之前ant會先檢查init是否曾經被執行過,如果執行過則直接直接執行compile,如果沒有則會先執行它依賴的target例如這裡的init, 然後在執行這個target如我們的計劃 --> <target name="init"> <!-- 為了不用拷貝,我們可以在最開始定義好目標資料夾,這樣ant直接把結果就放在目標資料夾中了 新建資料夾: --> <mkdir dir="${basedir}/bulid"/> <mkdir dir="${basedir}/${myClassDirName}"/> </target> <!-- 第2步:編譯 --> <target name="compile" depends="init"> <!-- includeantruntime="false"、 解決: [javac] E:\04-scala_eclipse\01-work\Hello\build.xml:46: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds --> <javac srcdir="${mySrc}" destdir="${myClassDirName}" includeantruntime="false" /> </target> <!-- 第3步:做jar包 --> <target name="build" depends="compile"> <!-- Could not find com.phei.netty.protocol.http.xml.Test. Make sure you have it in your classpath 主要是解決: /Hello/classes 目錄的,編譯後的.class檔案存放位置 --> <jar jarfile="${myBuildJarName}" basedir="${myClassDirName}"/> <jar destfile="${basedir}/bulid/${myBuildJarName}" /> <!-- 建立生成的jar包存放位置 /Hello/bulid/helloant.jar --> </target> <!-- 第3步:執行 --> <target name="run" depends="clean,build"> <java classname="example.Hello" classpath="${myBuildJarName}"/> </target> <!-- 重新執行 --> <target name="rerun" depends="run"> <ant target="clean" /> <ant target="run" /> </target> <!-- 每次執行前清空,以前執行的東西,比如生成的jar包,編譯的.class檔案 --> <target name="clean"> <delete dir="${myClassDirName}" /> <delete file="${myBuildJarName}" /> </target> </project>
執行結果
Buildfile: E:\04-scala_eclipse\01-work\Hello\build.xml
clean:
[delete] Deleting directory E:\04-scala_eclipse\01-work\Hello\classes
[delete] Deleting: E:\04-scala_eclipse\01-work\Hello\helloant.jar
init:
[mkdir] Created dir: E:\04-scala_eclipse\01-work\Hello\classes
compile:
[javac] Compiling 1 source file to E:\04-scala_eclipse\01-work\Hello\classes
build:
[jar] Building jar: E:\04-scala_eclipse\01-work\Hello\helloant.jar
run:
[java] 我的專案是ant執行的
BUILD SUCCESSFUL
Total time: 1 second
4.上面都是沒有新增額外的jar的,如果我們lib目錄下,有額外的jar該怎麼辦?
首先看一個測試類
package com.phei.netty.protocol.http.xml;
import java.util.HashMap;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class Test {
public static void main(String[] args) throws JSONException {
ParseJson("{\"name\":\"Alexia\",\"age\":\"23\"}");
CreateJson();
}
//解析JSON
private static void ParseJson(String jsonString) throws JSONException
{
JSONObject jObject = new JSONObject(jsonString);
System.out.println(jObject.get("name"));
}
//建立一個JSON
private static void CreateJson() throws JSONException
{
// 下面構造兩個map、一個list和一個Employee物件
Map<String, String> map1 = new HashMap<String, String>();
map1.put("name", "Alexia");
map1.put("sex", "female");
map1.put("age", "23");
Map<String, String> map2 = new HashMap<String, String>();
map2.put("name", "Edward");
map2.put("sex", "male");
map2.put("age", "24");
// 將Map轉換為JSONArray資料
JSONArray jArray = new JSONArray();
jArray.put(map1);
jArray.put(map2);
//定義JSON
JSONObject jObject=new JSONObject();
jObject.put("List", jArray);
jObject.put("Count","1200");
System.out.println(jObject.toString());
}
}
然後在lib裡面發放上jar包/Hello/lib/json-20171018.jar
然後配置檔案如下
<?xml version="1.0" encoding="UTF-8" ?>
<!-- ant的所有內容必須包含在這個裡邊,name是你給它取的名字,basedir故名思意就是工作的根目錄 .代表當前目錄。default代表預設要做的事情。 -->
<project name="Hello" default="run" basedir=".">
<property name="mySrc" value="src"/>
<property name="myClassDirName" value="classes"/>
<property name="myBuildJarName" value="helloant.jar"/>
<!--
[javac] E:\04-scala_eclipse\01-work\Hello\src\com\phei\netty\protocol\http\xml\TestOrder.java:22: 錯誤: 程式包org.jibx.runtime不存在
[javac] import org.jibx.runtime.BindingDirectory;
-->
<property name="lib.home" value="${basedir}\lib"/>
<path id="project.classpath">
<fileset dir="${lib.home}" includes="***.jar" />
</path>
<!--
第一步:初始化
把你想做的每一件事情寫成一個target ,它有一個名字,depends是它所依賴的target,在執行這個target
例如這裡的compile之前ant會先檢查init是否曾經被執行過,如果執行過則直接直接執行compile,如果沒有則會先執行它依賴的target例如這裡的init,
然後在執行這個target如我們的計劃
-->
<target name="init">
<!-- 為了不用拷貝,我們可以在最開始定義好目標資料夾,這樣ant直接把結果就放在目標資料夾中了 新建資料夾: -->
<mkdir dir="${basedir}/bulid"/>
<mkdir dir="${basedir}/${myClassDirName}"/>
</target>
<!--
第2步:編譯
-->
<target name="compile" depends="init">
<!--
includeantruntime="false"、
解決:
[javac] E:\04-scala_eclipse\01-work\Hello\build.xml:46: warning: 'includeantruntime' was not set, defaulting to build.sysclasspath=last; set to false for repeatable builds
-->
<javac srcdir="${mySrc}" destdir="${myClassDirName}" includeantruntime="false" nowarn="on" source="1.8" target="1.8" deprecation="true" debug="true" encoding="UTF-8" classpathref="project.classpath" >
<compilerarg line="-Xlint:unchecked" />
</javac>
</target>
<!--
第3步:做jar包
-->
<target name="build" depends="compile">
<!--
Could not find com.phei.netty.protocol.http.xml.Test. Make sure you have it in your classpath
主要是解決:
/Hello/classes 目錄的,編譯後的.class檔案存放位置
-->
<jar jarfile="${myBuildJarName}" basedir="${myClassDirName}"/>
<!-- 建立生成的jar包存放位置 /Hello/bulid/helloant.jar -->
<jar destfile="${basedir}/bulid/${myBuildJarName}" ></jar>
</target>
<!--
第4步:執行
-->
<target name="run" depends="clean,build">
<java classname="com.phei.netty.protocol.http.xml.Test" classpath="${myBuildJarName}" classpathref="project.classpath"/>
</target>
<!-- 重新執行 -->
<target name="rerun" depends="run">
<ant target="clean" />
<ant target="run" />
</target>
<!--
每次執行前清空,以前執行的東西,比如生成的jar包,編譯的.class檔案
-->
<target name="clean">
<delete dir="${myClassDirName}" />
<delete file="${myBuildJarName}" />
</target>
</project>
這裡執行結果如下
Buildfile: E:\04-scala_eclipse\01-work\Hello\build.xml
clean:
[delete] Deleting directory E:\04-scala_eclipse\01-work\Hello\classes
[delete] Deleting: E:\04-scala_eclipse\01-work\Hello\helloant.jar
init:
[mkdir] Created dir: E:\04-scala_eclipse\01-work\Hello\classes
compile:
[javac] Compiling 1 source file to E:\04-scala_eclipse\01-work\Hello\classes
build:
[jar] Building jar: E:\04-scala_eclipse\01-work\Hello\helloant.jar
run:
[java] Alexia
[java] {"List":[{"name":"Alexia","age":"23","sex":"female"},{"name":"Edward","age":"24","sex":"male"}],"Count":"1200"}
BUILD SUCCESSFUL
Total time: 2 seconds
注意:
1.如果編譯的時候沒有加
classpathref="project.classpath"
會報錯,因為,編譯的時候需要執行的jar包
[javac] E:\04-scala_eclipse\01-work\Hello\src\com\phei\netty\protocol\http\xml\Test.java:6: 錯誤: 程式包org.json不存在
[javac] import org.json.JSONArray;
[javac] ^
[javac] E:\04-scala_eclipse\01-work\Hello\src\com\phei\netty\protocol\http\xml\Test.java:7: 錯誤: 程式包org.json不存在
[javac] import org.json.JSONException;
[javac] ^
[javac] E:\04-scala_eclipse\01-work\Hello\src\com\phei\netty\protocol\http\xml\Test.java:8: 錯誤: 程式包org.json不存在
[javac] import org.json.JSONObject;
[javac] ^
[javac] E:\04-scala_eclipse\01-work\Hello\src\com\phei\netty\protocol\http\xml\Test.java:12: 錯誤: 找不到符號
[javac] public static void main(String[] args) throws JSONException {
[javac] ^
[javac] 符號: 類 JSONException
[javac] 位置: 類 Test
2.如果執行的時候沒有加
classpathref="project.classpath"
會報錯,因為,執行的時候需要執行的jar包
Buildfile: E:\04-scala_eclipse\01-work\Hello\build.xml
clean:
[delete] Deleting directory E:\04-scala_eclipse\01-work\Hello\classes
[delete] Deleting: E:\04-scala_eclipse\01-work\Hello\helloant.jar
init:
[mkdir] Created dir: E:\04-scala_eclipse\01-work\Hello\classes
compile:
[javac] Compiling 1 source file to E:\04-scala_eclipse\01-work\Hello\classes
build:
[jar] Building jar: E:\04-scala_eclipse\01-work\Hello\helloant.jar
run:
[java] java.lang.NoClassDefFoundError: org/json/JSONException
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:195)
[java] at org.apache.tools.ant.taskdefs.Java.run(Java.java:772)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:222)
[java] at org.apache.tools.ant.taskdefs.Java.executeJava(Java.java:136)
[java] at org.apache.tools.ant.taskdefs.Java.execute(Java.java:109)
[java] at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:293)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[java] at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[java] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[java] at java.lang.reflect.Method.invoke(Method.java:498)
[java] at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:106)
[java] at org.apache.tools.ant.Task.perform(Task.java:348)
[java] at org.apache.tools.ant.Target.execute(Target.java:435)
[java] at org.apache.tools.ant.Target.performTasks(Target.java:456)
[java] at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1405)
[java] at org.apache.tools.ant.Project.executeTarget(Project.java:1376)
[java] at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[java] at org.eclipse.ant.internal.launching.remote.EclipseDefaultExecutor.executeTargets(EclipseDefaultExecutor.java:36)
[java] at org.apache.tools.ant.Project.executeTargets(Project.java:1260)
[java] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.run(InternalAntRunner.java:456)
[java] at org.eclipse.ant.internal.launching.remote.InternalAntRunner.main(InternalAntRunner.java:140)
[java] Caused by: java.lang.NoClassDefFoundError: org/json/JSONException
[java] at java.lang.Class.getDeclaredMethods0(Native Method)
[java] at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
[java] at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
[java] at java.lang.Class.getMethod0(Class.java:3018)
[java] at java.lang.Class.getMethod(Class.java:1784)
[java] at org.apache.tools.ant.taskdefs.ExecuteJava.execute(ExecuteJava.java:143)
[java] ... 20 more
[java] Caused by: java.lang.ClassNotFoundException: org.json.JSONException
[java] at org.apache.tools.ant.AntClassLoader.findClassInComponents(AntClassLoader.java:1388)
[java] at org.apache.tools.ant.AntClassLoader.findClass(AntClassLoader.java:1337)
[java] at org.apache.tools.ant.AntClassLoader.loadClass(AntClassLoader.java:1095)
[java] at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
[java] ... 26 more
[java] Java Result: -1
BUILD SUCCESSFUL
Total time: 2 seconds
---------------------