Java MyEclipse下Ant build.xml简单实例详解

 

 

一、下载配置ant

1、首先下载ant: http://www.apache.org/ 下载最新的版本
2、解压ant 后设置ANT_HOME, PATH中添加ANT_HOME目录下的bin目录(如:ANT_HOME:D:\apache-ant-1.9.4,PATH:%ANT_HOME%\bin)
3、测试方法:开始-->运行-->cmd进入命令行-->键入 ant 回车,如果看到
Buildfile: build.xml does not exist!
Build failed
说明已经完成ant的设置。

 

二、创建项目

1、打开MyEclipse,创建一个java项目AntTestDemo, 创建一个测试类HelloAnt.java

package com.zdz.ant.test;

public class HelloAnt {
public static void main(String[] args){
    System.out.println("Hello ant!");
}
}

2、在项目根目录下创建一个build.xml文件,内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!-- ====================================================================== 
     Mar 17, 2015 3:17:08 PM                                                        

     project    
     description
                   
     Administrator                                                                
     ====================================================================== -->
<project name="AutoTestDemo" default="run" basedir=".">
<property name="src" value="src"></property>
<property name="dest" value="classes"/>
<property name="helloant_jar" value="helloant.jar"></property>
<target name="init">
    <mkdir dir="${dest}"/>
</target>
<target name="compile" depends="init" >
    <javac srcdir="${src}" destdir="${dest}"></javac>
</target>        
<target name="build" depends="compile">
    <jar jarfile="${helloant_jar}" basedir="${dest}"/>
</target>
<target name="run" depends="build">
    <java classname="com.zdz.ant.test.HelloAnt" classpath="${helloant_jar}"></java>
</target>
<target name="clean">
    <delete dir="${dest}"></delete>
    <delete file="${helloant_jar}"></delete>
</target>
<target name="rerun" depends="clean,run">
    <ant target="clean"></ant>
    <ant target="run"></ant>
</target>
</project>

 

如下图:

3、右键点击build.xml-Run As-Ant Build

4、运行结果:

Buildfile: E:\003_Workspace\EclipseWorkSpace\JavaForJMeter\AntTestDemo\build.xml
init:
    [mkdir] Created dir: E:\003_Workspace\EclipseWorkSpace\JavaForJMeter\AntTestDemo\classes
compile:
    [javac] Compiling 1 source file to E:\003_Workspace\EclipseWorkSpace\JavaForJMeter\AntTestDemo\classes
build:
      [jar] Building jar: E:\003_Workspace\EclipseWorkSpace\JavaForJMeter\AntTestDemo\helloant.jar
run:
     [java] Hello ant!
BUILD SUCCESSFUL
Total time: 5 seconds

出来BUILD SUCCESSFUL 和预期结果说明ant跑成功了。

 

三、遇到的问题

1、Error starting modern compiler

解决方法:点击项目下右键:Properties-Java Build Path->Libraries->右边选 AddLibrary -->看Workspace default JRE (XXX)XXX是JDK什么版本。不对的话。选Alternate JRE .Installed JRES .->选自己的JDK 安装目录 ,就OK。

 

附:项目例子

 

posted @ 2015-03-19 14:05  阳光温暖了心情  阅读(7902)  评论(3编辑  收藏  举报