Ant_实现自定义任务(004)

实现自定义ant任务

文件操作系统

默认情况下,ant仅输出它执行的移动和复制摘要的操作。包括诸如已移动或者复制的文件的数量等信息。
如果想看到更详细的信息,包括涉及的文件名称等,可以将verbose的属性设置为true

创建和解压缩zip文件
<zip destfile="output.zip" basedir="output"/>
<unzip src="output.tar.gz" dest="extractDir"/>

还可以包括overwrite属性来控制覆盖行为。默认设置是覆盖与正在被提取的归档文件中的条目相匹配的所有现有文件。


使用CVS

cvs的主要属性是cvsRoot,它是对cvs库的完整引用,包括连接方法和用户详细信息
这个参数的格式如下[:method:][[user][:password]@]hostname[:[port]]/path/to/repository
提取是cvs的默认操作,其他操作可以通过command属性来指定


替换文件中的标记
replace任务,它执行文件中的查找和替换操作
token属性执行要查找的字符串,value属性执行一个新的字符串,查找得到的标记字符串的所有实例都被替换为这个新的字符串。
<replace file="input.txt" token="old" value="new"/>

替换操作将在文件本身之内的适当位置进行,为了提供更详细的输出,可以把summary的属性设置为true,这将导致该任务输出找到和替换的
标记字符串实例的数目。


模式匹配

可以对目录执行模式匹配。例如模式src*/*.java将匹配带src前缀的任何目录中的所有java文件。
还有另一种模式结果:**,它匹配任何数量的目录。例如模式**/*.java将匹配当前目录结构下的所有文件。

*/*.java一个层级结构
**/*.java多个层级结构

<copy todir="archive">
<fileset dir="src">
<include name="*.java"/>
</fileset>
</copy>

范例:
<?xml version="1.0" encoding="UTF-8"?>
<project name="myAntProject" basedir="." default="package">

<property name="compile" value="compile"/>
<property name="dist" value="dist"/>
<property name="src" value="src"/>

<!--
<property name="cvsRoot" value=":pserver:alin:123@localhost:d:/cvs"></property>
<property name="checkoutLocation" value="d:/myCvs"></property>
-->
<target name="init">
</target>

<target name="preprocess" depends="init">
<mkdir dir="${compile}"/>
<mkdir dir="${dist}"/>
<!--
<mkdir dir="${checkoutLocation}"/>
-->
</target>

<target name="compile" depends="init,preprocess">
</target>

<target name="package" depends="compile">
</target>

<target name="mycompile" depends="preprocess">
<javac srcdir="src" destdir="${compile}"/>
</target>

<target name="dist" depends="mycompile">

<tstamp/>

<jar destfile="${dist}/package-${DSTAMP}.jar" basedir="${compile}">
<manifest>
<attribute name="Built-By" value="${user.name}"/>
<attribute name="Main-Class" value="com.javase.ant.AntSwing"/>
</manifest>
</jar>
</target>

<target name="deletefile">
<delete file="${dist}/package.jar"></delete>
</target>

<target name="copyfile">
<copy file="src/com/javase/ant/TestCopy.java" tofile="d:/TestCopy2.java"></copy>
</target>

<target name="copyfile2">
<copy file="src/com/javase/ant/TestCopy.java" todir="d:/"></copy>
</target>

<target name="movefile">
<move file="src/com/javase/ant/TestCopy.java" todir="d:/"></move>
</target>

<target name="compress" depends="compile">
<zip destfile="${dist}/package.zip" basedir="${compile}"></zip>
</target>

<target name="uncompress" depends="compress">
<unzip dest="${dist}" src="${dist}/package.zip"></unzip>
</target>

<!--
<target name="cvs" depends="preprocess">
<cvs cvsroot="${cvsRoot}" package="chat" command="checkout" dest="${checkoutLocation}"></cvs>
</target>
-->

<target name="replaceOperation">
<replace file="input.txt" token="how" value="what" summary="on"></replace>
</target>

<target name="copy2">
<copy todir="${dist}">
<fileset dir="src">
<include name="**/*.java"/>
</fileset>
</copy>
</target>
</project>

fileset默认情况下包含指定src目录下的所有文件,因此为了仅选择java文件,我们对模式使用一个include元素。类似的,我们可以对另一个模式
添加一个exclude元素,从而潜在的排除include的匹配项。甚至可以指定多个inclue和exclude的元素,这样将得到一组文件和目录,它们包含include的
所有匹配项的并集,但排除了exclude模式的所有匹配项。


使用自定义任务来扩展ant

考察一个简单自定义任务的构造过程。这个任务将对文件中的行执行排序操作,并将排序后的行集写到一个新文件中。

为了实现一个简单的自定义任务,我们需要做的就是扩展org.apache.tools.ant.Task类,并重写execute方法。
大多数任务,不管是核心任务还是自定义任务,都利用属性来控制它们的行为,对于这个简单的任务,我们需要一个属性来指定要排序的文件,
需要另一个属性来执行排序内容的输出。我们把这两个属性分别叫做file和tofile

范例:
package com.javase.ant.tools.extend;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.ListIterator;

import org.apache.tools.ant.BuildException;
import org.apache.tools.ant.Task;

public class FileSorter 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;
}

@Override
public void execute() throws BuildException {

try {
BufferedReader br = new BufferedReader(new FileReader(srcFile));
BufferedWriter bw = new BufferedWriter(new FileWriter(destFile));

List<String> list = new ArrayList<String>();

String line = br.readLine();

while (null != line) {
list.add(line);
line = br.readLine();
}

Collections.sort(list);

for (ListIterator<String> iter = list.listIterator(); iter
.hasNext();) {

line = iter.next();
bw.write(line);
bw.newLine();
}

br.close();
bw.close();

} catch (Exception e) {
e.printStackTrace();
}
}
}


<?xml version="1.0" encoding="UTF-8"?>
<project name="myAntProject" basedir="." default="package">

<property name="compile" value="compile" />
<property name="dist" value="dist" />
<property name="src" value="src" />

<!--
<property name="cvsRoot" value=":pserver:alin:123@localhost:d:/cvs"></property>
<property name="checkoutLocation" value="d:/myCvs"></property>
-->
<target name="init">
</target>

<target name="preprocess" depends="init">
<mkdir dir="${compile}" />
<mkdir dir="${dist}" />
<!--
<mkdir dir="${checkoutLocation}"/>
-->
</target>

<target name="compile" depends="init,preprocess">
</target>

<target name="package" depends="compile">
</target>

<target name="mycompile" depends="preprocess">
<javac srcdir="src" destdir="${compile}" />
</target>

<target name="dist" depends="mycompile">

<tstamp />

<jar destfile="${dist}/package-${DSTAMP}.jar" basedir="${compile}">
<manifest>
<attribute name="Built-By" value="${user.name}" />
<attribute name="Main-Class" value="com.javase.ant.AntSwing" />
</manifest>
</jar>
</target>

<target name="deletefile">
<delete file="${dist}/package.jar">
</delete>
</target>

<target name="copyfile">
<copy file="src/com/javase/ant/TestCopy.java" tofile="d:/TestCopy2.java">
</copy>
</target>

<target name="copyfile2">
<copy file="src/com/javase/ant/TestCopy.java" todir="d:/">
</copy>
</target>

<target name="movefile">
<move file="src/com/javase/ant/TestCopy.java" todir="d:/">
</move>
</target>

<target name="compress" depends="compile">
<zip destfile="${dist}/package.zip" basedir="${compile}">
</zip>
</target>

<target name="uncompress" depends="compress">
<unzip dest="${dist}" src="${dist}/package.zip">
</unzip>
</target>

<!--
<target name="cvs" depends="preprocess">
<cvs cvsroot="${cvsRoot}" package="chat" command="checkout" dest="${checkoutLocation}"></cvs>
</target>
-->

<target name="replaceOperation">
<replace file="input.txt" token="how" value="what" summary="on">
</replace>
</target>

<target name="copy2">
<copy todir="${dist}">
<fileset dir="src">
<include name="**/*.java" />
</fileset>
</copy>
</target>

<taskdef name="myFileSorterExample" classname="com.javase.ant.tools.extend.FileSorter" classpath="bin">
</taskdef>

<target name="myFileSorter">
<myFileSorterExample srcFile="input.txt" destFile="output.txt">
</myFileSorterExample>
</target>
</project>

posted @ 2012-06-11 23:35  雪中飞雁  阅读(221)  评论(0)    收藏  举报