Eclipse中已经集成了Ant,我们可以直接在Eclipse中运行Ant。
1、用 Ant 来构建简单系统
新建一个 Java project, 命名为Hello
Hello .java 文件内容
package example;
public class Hello {
public static void main(String[] args) {
System.out.println("我的项目是ant运行的");
}
}
bulid.xml 文件内容
熟悉 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项目以及打包工作。
只需要修改build.xml就可以了
运行结果:
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
首先看一个测试类
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
map1.put("name", "Alexia");
map1.put("sex", "female");
map1.put("age", "23");
Map
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
然后配置文件如下
这里运行结果如下
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
文章参考自:https://blog.csdn.net/qq_21383435/article/details/79082023