Ant 的简单应用一

需要 工具:
Apache ant 的安装包:
Apache ant 下载
java jdk 安装包:
linux java jdk
widnow 64位 java jdk

Ant 参考文档:
Ant 在线教程

  1. Ant 常用的属性:
属性解释
More Actions属性解释ant.file该构建文件的完整路径
ant.version安装Apache Ant的版本
basedir构建文件的基目录的绝对路径,作为prioject 元素的 basedir属性
ant.java.versionAnt使用的Java语言的软件开发工具包的版本ant.project.name项目的名字,具体声明为project元素的name属性
ant.project.default-target当前项目的默认目标
ant.core.libant的jat文件的完整的地址
ant.homeant安装的主目录
ant.library.dirant库文件的主目录,特别是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 表示排除当前文件夹中的文件或者文件夹中的文件

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值