需要 工具:
Apache ant 的安装包:
Apache ant 下载
java jdk 安装包:
linux java jdk
widnow 64位 java jdk
Ant 参考文档:
Ant 在线教程
- Ant 常用的属性:
属性 | 解释 |
---|---|
More Actions属性解释ant.file | 该构建文件的完整路径 |
ant.version | 安装Apache Ant的版本 |
basedir | 构建文件的基目录的绝对路径,作为prioject 元素的 basedir属性 |
ant.java.version | Ant使用的Java语言的软件开发工具包的版本ant.project.name项目的名字,具体声明为project元素的name属性 |
ant.project.default-target | 当前项目的默认目标 |
ant.core.lib | ant的jat文件的完整的地址 |
ant.home | ant安装的主目录 |
ant.library.dir | ant库文件的主目录,特别是ANT_HOME/lib文件夹 |
Ant 的基本实用案例:
简单的文本输出:
测试代码
测试类:
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello World!");
}
}
创建 build.xml 测试文件
<project name="Hello world Project" default="info">
<target name="info">
<echo>Hello world - Welcome to Apache Ant!</echo>
<description> this is test !</description>
</target>
</project>
一个目标 (target) 是一系列你想运行的任务 (tasks),运行时看成一个单元。在我们的例子中,我们用一个简单的目标来为用户提供一个有信息的消息。
一般配置中需要为target 设置任务名:
配置好后Apache Ant 后打开命令行,输入ant 命令
F:\files\study\antDemo1>ant
Buildfile: F:\files\study\antDemo1\build.xml
info:
[echo] Hello world - Welcome to Apache Ant!
BUILD SUCCESSFUL
Total time: 0 seconds
F:\files\study\antDemo1>
便可以输出信息
depends的使用
目标和目标之间可以有依赖关系。举个例子,一个部署 (deploy) 目标可能依赖于封装 (package) 目标,而这个封装目标可能又依赖于编译 (compile) 目标等。依赖关系被表示成依赖属性 (depends)。
build.xml 文件:
<project name="Hello world Project" default="info">
<property name="sitname" value="www.tutorialspoint"/>
<target name="info" depends="antVersion">
<echo>Hello world - Welcome to Apache Ant!</echo>
<description> this is test !</description>
</target>
<target name="antVersion" depends="currentPath" if="ant.version" >
<echo> Apache ant version is ${ant.version} - You are at ${sitname}</echo>
<description> show ant version ! </description>
</target>
<target name="currentPath" depends="coreLib" unless="test">
<echo message="The base dir is: ${basedir}" />
<description> show current path ! </description>
</target>
<target name="coreLib" depends="homePath">
<echo message="The core lib is: ${ant.core.lib}" />
<description> show current core lib ! </description>
</target>
<target name="homePath" depends="loadProperties">
<echo message="The home path is :${ant.home}"></echo>
<description> show ant home path ! </description>
</target>
<property file="/properties/build.properties"></property>
<target name="loadProperties" depends="antLibraryDir">
<echo> name : ${name} , age : ${age} </echo>
<description> load a custom properties file to get any value you want to load </description>
</target>
<target name="antLibraryDir" depends="copyAction">
<echo message="The ant library dir is: ${ant.core.lib}" />
<description> show ant library dir ! </description>
</target>
<target name="copyAction" >
<copy todir="${basedir}/copy">
<fileset dir="${basedir}">
<include name="Test.java"/>
<exclude name="User.java"/>
<include name="properties/*"/>
</fileset>
</copy>
<echo> file copy is finish !</echo>
<description> test copy file function ! </description>
</target>
</project>
属性文件:
build.properties
#the custom properties
name= xiaoming
age= 23
输出的结果如下:
F:\files\study\antDemo1>
F:\files\study\antDemo1>ant
Buildfile: F:\files\study\antDemo1\build.xml
copyAction:
[echo] file copy is finish !
antLibraryDir:
[echo] The ant library dir is: E:\apache-ant-1.9.14\lib\ant.jar
loadProperties:
[echo] name : ${name} , age : ${age}
homePath:
[echo] The home path is :E:\apache-ant-1.9.14\bin\..
coreLib:
[echo] The core lib is: E:\apache-ant-1.9.14\lib\ant.jar
currentPath:
[echo] The base dir is: F:\files\study\antDemo1
antVersion:
[echo] Apache ant version is Apache Ant(TM) version 1.9.14 compiled on Mar
ch 12 2019 - You are at www.tutorialspoint
info:
[echo] Hello world - Welcome to Apache Ant!
BUILD SUCCESSFUL
Total time: 0 seconds
F:\files\study\antDemo1>
project 中标记的默认的任务执行,找到当前的任务,如果有依赖关系,先执行依赖的任务然后执行当前的任务。
copy ,fileset 共同作用可以拷贝文件到指定的文件夹:
include 后面跟文件,就拷贝文件,跟文件夹就拷贝文件夹,如果文件夹 为 ‘properties/’ 或者 ‘properties/** ’ 那么就拷贝文件及文件中的子文件到指定目录,如果是 ‘properties/*’ 表示拷贝当前文件夹中的文件,不在将文件夹中的子文件再次拷贝
exclude 表示排除当前文件夹中的文件或者文件夹中的文件