Ant 在eclipse 中编译和运行java代码

创建一个java 工程,目录结构如下:

F:\files\study\Ant>tree
.
├── bin
│   └── com
│       └── huaqin
│           ├── Test.class
│           └── User.class
├── build
│   └── classes
│       └── com
│           └── huaqin
│               ├── Test.class
│               └── User.class
├── build.xml
├── lib
│   └── json.jar
├── out
└── src
    └── com
        └── huaqin
            ├── Test.java
            └── User.java

12 directories, 8 files

Test.java 文件如下:

package com.huaqin;

import org.json.JSONException;
import org.json.JSONObject;

public class Test {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println("Hello World!");
		try {
			JSONObject mUserObject = new JSONObject("{'name':'tome',}");
			String mName = mUserObject.optString("name");
			System.out.println("name : "+ mName);
			
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

}

build.xml 文件如下:

<?xml version="1.0" ?>
<project name ="TestAnt" default="runPtint" basedir=".">
	
	<path id="jsonPackage" description="the path of package">
		<fileset dir="${basedir}/lib">
			<include name="*.jar"/>
		</fileset>	
	</path>
	
	
	<target name="clean" description="clear compiled file" >
		<delete dir="${basedir}/build"/>
		<delete dir="${basedir}/out"/>
	</target>
	
	
	
	<target name="compilePrint"  depends ="clean" >
		<mkdir dir ="${basedir}/build/classes"/>
		<javac srcdir ="${basedir}/src" destdir ="${basedir}/build/classes" includeAntRuntime="false">
			<classpath refid="jsonPackage"/>	
		</javac>
	</target>
   
	<target name="runPtint" depends ="compilePrint">
   		
		<java  fork="true" failonerror="yes" classname="com.huaqin.Test">
   			<classpath >
      			<pathelement path="${basedir}/build/classes/"/>
   			 	<fileset dir="${basedir}/lib">
   			      	<include name="json.jar"/>
   			   	</fileset>
    		</classpath>
   		</java>
   	</target>
   

 </project>

执行的结果

F:\files\study\Ant>
F:\files\study\Ant>ant clean
Buildfile: F:\files\study\Ant\build.xml

clean:
   [delete] Deleting directory F:\files\study\Ant\build
   [delete] Deleting directory F:\files\study\Ant\out

BUILD SUCCESSFUL
Total time: 0 seconds

F:\files\study\Ant>
F:\files\study\Ant>
F:\files\study\Ant>ant compilePrint
Buildfile: F:\files\study\Ant\build.xml

clean:

compilePrint:
    [mkdir] Created dir: F:\files\study\Ant\build\classes
    [javac] Compiling 2 source files to F:\files\study\Ant\build\classes

BUILD SUCCESSFUL
Total time: 1 second

F:\files\study\Ant>
F:\files\study\Ant>ant  runPtint
Buildfile: F:\files\study\Ant\build.xml

clean:
   [delete] Deleting directory F:\files\study\Ant\build

compilePrint:
    [mkdir] Created dir: F:\files\study\Ant\build\classes
    [javac] Compiling 2 source files to F:\files\study\Ant\build\classes

runPtint:
     [java] Hello World!
     [java] name : tome

BUILD SUCCESSFUL
Total time: 2 seconds

F:\files\study\Ant>

上面的案例是一个简单的 ant 任务的配置和运行的内容,
列举了文件的删除(delete),加载(inclues)和编译(javac)、运行的简单过程(java)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值