Java MyEclipse下Ant build.xml簡單實例詳解

 

 

1、下載配置antjava

一、首先下載ant: http://www.apache.org/ 下載最新的版本
二、解壓ant 後設置ANT_HOME, PATH中添加ANT_HOME目錄下的bin目錄(如:ANT_HOME:D:\apache-ant-1.9.4,PATH:%ANT_HOME%\bin)
三、測試方法:開始-->運行-->cmd進入命令行-->鍵入 ant 回車,若是看到
Buildfile: build.xml does not exist!
Build failed
說明已經完成ant的設置。apache

 

2、建立項目測試

一、打開MyEclipse,建立一個java項目AntTestDemo, 建立一個測試類HelloAnt.javaui

package com.zdz.ant.test;

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

二、在項目根目錄下建立一個build.xml文件,內容以下:spa

<?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>

 

以下圖:命令行

三、右鍵點擊build.xml-Run As-Ant Buildcode

四、運行結果:xml

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跑成功了。blog

 

3、遇到的問題ip

一、Error starting modern compiler

解決方法:點擊項目下右鍵:Properties-Java Build Path->Libraries->右邊選 AddLibrary -->看Workspace default JRE (XXX)XXX是JDK什麼版本。不對的話。選Alternate JRE .Installed JRES .->選本身的JDK 安裝目錄 ,就OK。

 

附:項目例子

相關文章
相關標籤/搜索