Java构建工具之Ant超入门
Ant是一种基于Java的build工具。理论上来说,它有些类似于(Unix)C中的make ,但没有make的缺陷。
http://ant.apache.org/
版本:apache-ant-1.8.4
1、输出信息
<!-- 输出信息 --><echo>ANT_HOME:${ant.home}</echo><echo message="ANT_VERSION:${ant.version}"/>2、时间戳
<!-- 时间戳 --><tstamp /><echo message="${DSTAMP}"/>3、使用properties文件
<property file="build.properties" /><!-- 使用build.properties --><echo message="${test.key1}"/>4、定义变量
<!-- property定义 --><property name="test.property1" value="propertyA"/><echo message="${test.property1}"/>5、导入其他Build文件
<include file="included.xml"/><!-- included.xml --><echo message="${included}"/>......<import file="imported.xml"/><!-- imported.xml --><echo message="${imported}"/>6、自定义Task
package com.rensanning.ant;import org.apache.tools.ant.BuildException;import org.apache.tools.ant.Task;public class MyTask extends Task { private String name; private String country;@Override public void execute() throws BuildException { System.out.println(name + " from " + country);}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getCountry() {return country;}public void setCountry(String country) {this.country = country;}}<taskdef name="mt" classname="com.rensanning.ant.MyTask" classpath="bin"/><!-- 自定义Task --><mt name="rensanning" country="china"/>
7、创建文件夹
<!-- 创建文件夹 --><mkdir dir="C:\ant\test11\10" /><mkdir dir="C:\ant\test11\11" />
8、删除文件夹
<!-- 删除文件夹 --><delete dir="C:\ant\test11\11" />
9、移动文件夹
<!-- 移动文件夹 --><mkdir dir="C:\ant\folder\move" /><move todir="C:\ant\folder\move"> <fileset dir="test/folder/move"/></move>
10、拷贝文件夹
<!-- 拷贝文件夹 --><mkdir dir="C:\ant\folder\copy" /><copy todir="C:\ant\folder\copy"><fileset dir="test/folder/copy"><include name="**/*.java" /><exclude name="**/*Test.java" /></fileset></copy>
11、删除文件
<!-- 删除文件 --><delete file="test/file/delete/deletefile.txt" />
12、拷贝文件
<!-- 拷贝文件 --><mkdir dir="C:\ant\file\copy" /><copy todir="C:\ant\file\copy"><fileset dir=""><include name="**/*.java" /><exclude name="**/*.class" /></fileset></copy>
13、文件重命名
<!-- 文件重命名 --><move file="test/file/copy/C2.java" tofile="C:\ant\file\copy\RC2.java"/>
14、修改文件中的某个值
<!-- 修改文件中的某个值 --><replace file="C:\ant\file\copy\RC2.java" token="args" value="param"/>
15、压缩文件
<!-- 压缩文件 --><mkdir dir="C:\ant\file\zip" /><zip destfile="C:\ant\file\zip\manual.zip" basedir="test/folder" includes="**/*.java" excludes="**/*Test.java"/>
16、解压文件
<!-- 解压文件 --><mkdir dir="C:\ant\file\unzip" /><unzip src="C:\ant\file\zip\manual.zip" dest="C:\ant\file\unzip"/>
17、移动文件
<!-- 移动文件 --><mkdir dir="C:\ant\file\move" /><move file="test/file/move/movefile.txt" todir="C:\ant\file\move"/>
18、设置classpath
<!-- 设置classpath --><path id="cp"> <pathelement path="${java.class.path}"/> <fileset dir="./lib"> <include name="**/*.jar"/> </fileset></path>19、编译类
<!-- 编译类javac --><javac destdir="./bin" encoding="UTF-8" deprecation="on" debug="off" fork="true" memoryMaximumSize="256m"> <src path="src"/> <src path="src2"/><classpath refid="cp" /></javac>
20、运行类
<!-- 运行类java --><java classname="com.rensanning.ant.T" classpath="bin"/>
21、打包jar
<!-- 打包jar --><mkdir dir="C:\ant\jar" /><jar jarfile="C:\ant\jar\test.jar"> <fileset dir="./bin"> <include name="**/*" /> </fileset></jar>
22、打包war
<!-- 打包war --><mkdir dir="C:\ant\war" /><war destfile="C:\ant\war\myapp.war" webxml="test/war/web.xml"> <fileset dir="test/war/html"/> <fileset dir="test/war/jsp"/> <lib dir="./lib"> <exclude name="servlet-api.jar"/> </lib> <classes dir="./bin"/> <zipfileset dir="test/war/images" prefix="images"/></war>
23、CVS相关
<!-- CVS相关 --><cvspass cvsroot="${cvsroot}" password="${cvs.password}" passfile="${cvs.passfile}"/><cvs cvsroot="${cvsroot}" command="checkout" cvsrsh="ssh" package="myproject" dest="${basedir}" passfile="${cvs.passfile}"/><cvs cvsRoot=":pserver:${cvs.user}:${cvs.password}@${cvs.server}:${cvs.reppath}" package="${cvs.module}" tag="${tag60}" dest="${checkoutdir}" />24、执行外部文件
<!-- 执行外部文件 --><exec executable="cmd"> <arg value="/c"/> <arg value="ant.bat"/> <arg value="-p"/></exec>
25、SSH
<!-- SSH --><sshexec host="" username="" password="" trust="true" command=""/>
26、SCP
<!-- SCP --><scp todir="root:123456@192.168.0.2:/usr/local/tomcat/webapps/" trust="true"><fileset dir="dir" id="id"> <include name="include"/> <exclude name="exclude"/></fileset></scp>
27、文件同步
<!-- 文件同步 --><sync todir="site"> <fileset dir="generated-site"/></sync>
28、转换文件格式
<!-- 转换文件格式 --><native2ascii encoding="EUCJIS" src="srcdir" dest="srcdir" includes="**/*.eucjis" ext=".java"/>
29、发送邮件
<!-- 发送邮件 --><mail mailhost="smtp.myisp.com" mailport="1025" subject="Test build"><from address="me@myisp.com"/><to address="all@xyz.com"/><message>The {buildname} nightly build has completed</message><fileset dir="dist"><includes name="**/*.zip"/></fileset></mail>■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■
http://rensanning.iteye.com/blog/1548162 Java集合框架之fastutil
http://rensanning.iteye.com/blog/1547845 Java工具类之Apache的Commons Lang和BeanUtils
http://rensanning.iteye.com/blog/1546652 Java日期计算之Joda-Time
http://rensanning.iteye.com/blog/1545708 Java生成缩略图之Thumbnailator
http://rensanning.iteye.com/blog/1540613 模板引擎之FreeMarker超入门
http://rensanning.iteye.com/blog/1540336 Java构建工具之Ant超入门
http://rensanning.iteye.com/blog/1538689 Java操作PDF之iText超入门
http://rensanning.iteye.com/blog/1538591 Java读写Excel之POI超入门
■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■■