安装ant,去http://ant.apache.org下载
配置环境变量(前提是配置了java环境变量)
ANT_HOME
G:Softwareant1.9.7 //ant根目录
在PATH后添加
;%ANT_HOME%in //注意分号
cmd命令输入ant,结果为
Buildfile: build.xml does not exist! Build failed
表示安装成功
测试程序helloWorld
package test; public class HelloWorld { public static void main(String[] args) { System.out.println("HelloWorld"); } }
在src目录下新建build.xml
<?xml version="1.0" ?> <project name="itPackage" basedir="." default="main"> <property name="src.dir" value="src"/> <property name="build.dir" value="build"/> <property name="classes.dir" value="${build.dir}/classes"/> <property name="jar.dir" value="${build.dir}/jar"/> <property name="main-class" value="test.HelloWorld"/> <target name="clean"> <delete dir="${build.dir}"/> </target> <target name="compile"> <mkdir dir="${classes.dir}"/> <javac srcdir="${basedir}" destdir="${classes.dir}" includeAntRuntime="false"/> </target> <target name="jar" depends="compile"> <mkdir dir="${jar.dir}"/> <jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}"> <manifest> <attribute name="Main-Class" value="${main-class}"/> </manifest> </jar> </target> <target name="run" depends="jar"> <java jar="${jar.dir}/${ant.project.name}.jar" fork="true"/> </target> <target name="clean-build" depends="clean,jar"/> <target name="main" depends="clean,run"/> </project>
run as ant build结果为
Buildfile: G:workspaceitProjectsrcuild.xml clean: clean: compile: [mkdir] Created dir: G:workspaceitProjectsrcuildclasses [javac] Compiling 1 source file to G:workspaceitProjectsrcuildclasses jar: [mkdir] Created dir: G:workspaceitProjectsrcuildjar [jar] Building jar: G:workspaceitProjectsrcuildjarHelloWorld.jar run: [java] HelloWorld BUILD SUCCESSFUL Total time: 1 second
表示build成功
注意:javaEE工程是run之后不会clean,而java工程则run完之后会clean
Buildfile: G:workspacemyPackagesrcuild.xml compile: [mkdir] Created dir: G:workspacemyPackagesrcuildclasses [javac] Compiling 1 source file to G:workspacemyPackagesrcuildclasses jar: [mkdir] Created dir: G:workspacemyPackagesrcuildjar [jar] Building jar: G:workspacemyPackagesrcuildjarmyPackage.jar compile: jar: run: [java] HelloWorld compile: clean: [delete] Deleting directory G:workspacemyPackagesrcuild BUILD SUCCESSFUL Total time: 1 second