Ant HelloWorld

开始学习一下ANT,在网上查了些资料,走出HELLOWORLD第一步

一.配置JDK,ANT:

系统环境变量配置:
JAVA_HOME:D:\jdk1.6.0_10
PATH:%JAVA_HOME%/bin;%JAVA_HOME%/jre/bin
CLASSPATH:.;%JAVA_HOME%/lib/dt.jar;%JAVA_HOME%/lib/tools.jar (要加.表示当前路径)
ANT_HOME:E:\apache-ant-1.8.2
PATH:%ANT_HOME%\bin; %ANT_HOME%\lib
打开CMD,输入java -version,输入ant,有相关提示说明配置成功
二、HELLOWORLD
1.新建src文件夹,在其中放入HelloWorld.java文件,内容如下:
public class HelloWorld{
public static void main(String[] args){
System.out.println("Hello world");
}
}
2.在src同级目录下放入build.xml文件,注意要保存成UTF-8格式。内容如下:
<?xml version="1.0" encoding="UTF-8" ?>
<!--ant的所有内容必须包含在project中,name是名字,
basedir是根目录 .代表当前目录。default代表默认要做的事情-->
<project name="HelloWorld" default="run" basedir=".">
<!--property类似程序中的变量-->
 <property name="src" value="src"/>
 <property name="dest" value="classes"/>
 <property name="hello_jar" value="hello.jar"/>
 <!--target类似于方法,depends表示依赖的方法,如果未执行则先执行-->
 <target name="init">
  <!--mkdir用于创建目录-->
  <mkdir dir="${dest}"/>
 </target>
 <target name="compile" depends="init">
  <javac srcdir="${src}" destdir="${dest}"/>
 </target>
 <target name="build" depends="compile">
  <jar jarfile="${hello_jar}" basedir="${dest}"/>
 </target>
 <target name="run" depends="build">
  <java classname="HelloWorld" classpath="${hello_jar}"/>
 </target>
 <target name="clean">
  <delete dir="${dest}" />
  <delete file="${hello_jar}" />
 </target>
 <target name="rerun" depends="clean,run">
  <ant target="clean" />
  <ant target="run" />
 </target>
</project>
3.打开CMD,进入工程目录,输入ant(执行默认目标)或ant -file build.xml即可编译
E:\JavaWorkSpace\HelloWorld>ant -file build.xml
Buildfile: E:\JavaWorkSpace\HelloWorld\build.xml
init:
    [mkdir] Created dir: E:\JavaWorkSpace\HelloWorld\classes
compile:
    [javac] E:\JavaWorkSpace\HelloWorld\build.xml:15: warning: 'includeantruntim
e' was not set, defaulting to build.sysclasspath=last; set to false for repeatab
le builds
    [javac] Compiling 1 source file to E:\JavaWorkSpace\HelloWorld\classes
build:
      [jar] Building jar: E:\JavaWorkSpace\HelloWorld\hello.jar
run:
     [java] Hello world
BUILD SUCCESSFUL
Total time: 4 seconds
4.输入ant clean即可清除编译

ant

相关推荐