安装ant,去http://ant.apache.org下载
配置环境变量(前提是配置了java环境变量)
ANT_HOME
G:\Software\ant1.9.7 //ant根目录
在PATH后添加
;%ANT_HOME%\bin //注意分号
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 ="itProject" default="compile" basedir="."> <target name="clean"> <delete dir="${basedir}/build"/> </target> <target name="compile" depends="clean"> <mkdir dir="${basedir}/build/classes"/> <javac srcdir="${basedir}" destdir="${basedir}/build/classes" includeAntRuntime="false"/> </target> <target name="jar"> <mkdir dir="build/jar"/> <jar destfile="build/jar/HelloWorld.jar" basedir="build/classes"> <manifest> <attribute name="Main-Class" value="test.HelloWorld"/> </manifest> </jar> </target> <target name="run"> <java jar="build/jar/HelloWorld.jar" fork="true"/> </target> </project>
run as ant build结果为
Buildfile: G:\workspace\itProject\src\build.xml clean: clean: compile: [mkdir] Created dir: G:\workspace\itProject\src\build\classes [javac] Compiling 1 source file to G:\workspace\itProject\src\build\classes jar: [mkdir] Created dir: G:\workspace\itProject\src\build\jar [jar] Building jar: G:\workspace\itProject\src\build\jar\HelloWorld.jar run: [java] HelloWorld BUILD SUCCESSFUL Total time: 1 second
表示build成功
以上是web项目当中,可以直接build,但是我发现在java普通项目中需要分开运行,就是先clean compile再jar run
时间: 2024-10-12 15:00:50