1. 为什么需要Ant构建工具?
complie---编译,generate document----产生文档,unit test---单元测试,package--打包,deploy---部署
2. ant执行的常用参数:
ant -buildfile 指定要运行的xml文件
ant -debug 打印详尽的执行信息
ant -quiet 打印一些简略的信息
ant -verbose 打印稍微详细的执行信息
ant -logfile 输出日志文件名
-D<property>=<value> use value for given property: ant -D属性名称=属性值 在build.xml文件中使用类似于EL表达式的方式来获取值
ant -find 要查找的构建文件
如果想执行指定的目标(一个或者多个目标): ant [-buildfile|-f] build.xml [目标name的值 目标name的值2,目标的name的值3......]
3. 如果在构建文件中存在多个依赖的时候,Ant会按照依赖的目标顺序来执行的,当Ant构建文件中自身运行的时候,目标是不会被重复执行的
现在举例如下:
-------init---------
| |
compile clean
|
|
archive
如果现在有 <target name="all" depends="archive,clean">,则首先会执行init,创建了两个文件夹,然后再执行compile,然后再
执行archive,然后再去执行clean,clean依赖于init,但是这里不会再去执行init,因为init已经被执行了,最后将创建的文件夹删除了
现在如果<target name="all" depends="clean,archive">那么首先会去执行init,创建两个文件夹,然后执行clean,又删除了该两个文件夹,
然后再去执行archive,但archive依赖compile,compile依赖init,但是这里不会执行init,因为init已经执行过了,所以这里会首先执行
compile,然后再去执行archive,在执行的过程中发现没有需要的那两个文件夹,所以构建的过程会失败,这里应该特别注意,以上所说的不会
重复执行时指的是在build.xml内部指定的,如果我们在命令行显示指定执行的目标那就可以重复执行的。
4. 数据类型和属性
1:fileset
<!--fileset-->
<fileset id="source.fileset" includes="**/*.java" dir="src">
</fileset>
<target name="copy">
<copy todir="build/copy">
<fileset refid="source.fileset"/>
</copy>
</target>
2: <description>
this is a decs
</description>
<target name="init" description="this is init">
</target>
以上表示的是description元素和description属性只是起到一个注释的作用,在ant构建的时候并不会显示处理啊
3: 定义与使用属性<property>
<property name="username" value="songwei"/>,读取定义的属性值可以使用类似于EL表达式的方式来取得
4: javac的classpath属性,相当于javac的-classpath选项,debug=true表示待调试信息的编译源文件
如果某个任务能够确定某个所请求的操作不需要执行,那么该操作就会被跳过
5: 创建jar文件
<target name="toJar" depends="myCompile"><jar destfile="build/Ant.jar" basedir="build/classes"></jar>
</target>
可执行jar文件:<target name="toJar" depends="myCompile">
<jar destfile="build/Ant.jar" basedir="build/classes">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-Class" value="com.ant.test.Test2"/>
</manifest>
</jar>
</target>
6: 时间戳的生成
先定义时间戳 <tstamp/>
然后在其他地方通过${DSTAMP}的形式就可以获得当前系统的时间戳
7: 文件系统操作
<delete file="build/Ant-20120707.jar"></delete> file属性指定的是删除单个文件,dir属性指定的是删除整个目录
复制文件:<copy file="" tofile=""></copy>
还可以使用move来执行重命名操作,而不是拷贝: <move file="" tofile=""></move>
另外一个常用的文件系统操作时将文件移动或拷贝到另一个目录:
<copy file="" todir=""></copy>
<move file="" todir=""></move>
8: 创建和解压缩ZIP文件
创建:<zip destfile="" basedir=""></zip>:
<zip destfile="build/ouput.zip" basedir="build"></zip>
解压缩:<unzip dest="d:/" src="build/ouput.zip"></unzip>
9: 替换文件中的标记
<replace file="input.txt" token="user" value="songwei"></replace>: 表示的是将input.txt文件中的user字符串全部替换为songwei
10: 模式匹配
<copy>
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</copy> 匹配所有的src目录下以及子目录下的所有的.java文件
5. 使用自定义任务来扩展Ant
1: 首先自定义类,该类必须要继承Task,并且重写其中的execute方法,在build.xml文件中的标签属性就是
在类中定义的属性:
public class FileSorted extends Task {
private File srcFile;
private File destFile;
public File getSrcFile() {
return srcFile;
}
public void setSrcFile(File srcFile) {
this.srcFile = srcFile;
}
public File getDestFile() {
return destFile;
}
public void setDestFile(File destFile) {
this.destFile = destFile;
}
@SuppressWarnings("unchecked")
@Override
public void execute() throws BuildException {
try {
BufferedReader reader = new BufferedReader(new FileReader(srcFile));
BufferedWriter writer = new BufferedWriter(new FileWriter(destFile));
List<String> list = new ArrayList<String>();
String line = reader.readLine();
while (line != null) {
list.add(line);
line = reader.readLine();
}
Collections.sort(list);
for (Iterator iterator = list.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
writer.write(string);
writer.newLine();
}
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2: 在build.xml文件中定义任务:
<taskdef classname="com.ant.test.FileSorted" name="fileSorted" classpath="bin">
</taskdef>
classpath指定的是编译好的文件的路径
3: 使用我们自定义的task:
<target name="myfile">
<fileSorted srcfile="input.txt" destfile="ouput.txt"/>
</target>