1. Ant 简介:
Ant 是一款广泛使用的流行的开源构建工具,它用Java语言编写。
2.Ant官网:
Ant官网:http://ant.apache.org/
3.设置环境变量:
新建
Variable name: ANT_HOME
Variable value: C:\tools\ant\apache-ant-1.9.7\
追加:
Variable name:path
Variable value: %ANT_HOME%bin\;
4. 示例项目的目录结构
目录 | 内容 |
src | 应用程序源代码 |
test | 单元测试代码 |
lib | 项目依赖 |
build | 构建过程中生成的任何文件 |
build/classes | 编译好的Java类 |
build/test-classes | 编译好的单元测试 |
dist | 发行文件,如打包的JAR或者WAR文件 |
5. Ant Hello World Demo
项目目录:
AntDemo
------------src
------------AntDemo.java
------------build.xml
AntDemo.java
public class AntDemo { public static void main(String[] args) { // TODO Auto-generated method stub String jvm=System.getProperty("java.version"); String osName=System.getProperty("os.name"); String osVersion=System.getProperty("os.version"); System.out.println("Running Java "+jvm+" on "+osName+"(version )"+osVersion+")"); } }
build.xml
<?xml version="1.0" ?> <project name="tax-calculator" default="package"> <!-- Use mkdir command create required folder --> <target name="init"> <mkdir dir="build/classes"/> <mkdir dir="dist"/> </target> <!-- Use javac compile Java Code to build/classes folder --> <target name="compile" depends="init" description="Compile Java code"> <javac srcdir="src" destdir="build/classes" includeantruntime="false"/> </target> <!-- Generate JAR files includes builde/classes all classes files --> <target name="package" depends="compile" description="Generate JAR files"> <jar destfile="dist/tax-calculator.jar" basedir="build/classes" /> </target> <!-- Delete created folder --> <target name="clean" description="Deletes generated directories"> <delete dir="build"/> <delete dir="dist"/> </target> </project>
Tips:
build.xml 放在和src同级目录下
打开命令提示符,进入AntDemo文件夹,执行命令:
ant
再执行清理命令:
ant clean
其他命令用法:
查看ant帮助命令: ant -v
默认ant命令构建build.xml,如果想换构建的xml名字: ant -f mybuild.xml
显示所有的description 属性:ant -projecthelp
时间: 2024-11-06 07:09:26