首先,要写出自己的Ant Task是一件非常简单的事情,有了心里的这层藐视之后,做起事来就容易多了!
说说我做这个Ant Task的背景,随着部门框架的推广,支持国际化是一个严肃的问题,我们选择了utf-8作为UNICODE字符集的编码形式,因此现有项目必须经过一些转换的处理,自然,Ant是首选工具。
可以看一个完整的build.xml:
解释一下,首先这个任务进行jsp页编码形式的替换,接着进行内容编码的转换,其中可以指定目标目录,文件格式,目标编码形式。Ant Task主要用到了java的反射技术,我们自己的Task可以继承Task类,然后复写excute()方法,看看GBK2UTF8的源代码:
这样,就完成任务了。看看是不是很简单?
参考资料:http://ant.apache.org/manual/tutorial-writing-tasks.html
说说我做这个Ant Task的背景,随着部门框架的推广,支持国际化是一个严肃的问题,我们选择了utf-8作为UNICODE字符集的编码形式,因此现有项目必须经过一些转换的处理,自然,Ant是首选工具。
可以看一个完整的build.xml:
<?
xml version
=
"
1.0
"
encoding
=
"
UTF-8
"
?>
< project default = " main " basedir = " . " >
< property name = " jsp.dir " value = " organtype " />
< property name = " lib.dir " value = " bin " />
< path id = " classpath " >
< fileset dir = " ${lib.dir} " >
< include name = " **/*.jar " />
</ fileset >
< pathelement path = " bin " />
</ path >
<!-- 编码关键字替换 -->
< target name = " replace " description = " 将GBK,gb2312换成UTF-8 " >
< replace dir = " ${jsp.dir} " token = " GBK " value = " UTF-8 " />
< replace dir = " ${jsp.dir} " token = " gb2312 " value = " UTF-8 " />
</ target >
<!-- 初始化任务 -->
< target name = " init-tasks " description = " ==> 生成代码前的初始化工作 " >
< taskdef name = " gbk2utf8 " classname = " org.loushang.bsp.util.GBK2UTF8 " >
< classpath refid = " classpath " />
</ taskdef >
</ target >
<!-- 编码格式转换 -->
< target name = " convert " description = " 改变文件的编码格式 " depends = " init-tasks " >
< gbk2utf8 destDir = " ${jsp.dir} " format = " java " destEncoding = " utf-8 " />
</ target >
<!-- 主任务 -->
< target name = " main " description = " 主任务 " >
< antcall target = " replace " />
< antcall target = " convert " />
</ target >
</ project >
< project default = " main " basedir = " . " >
< property name = " jsp.dir " value = " organtype " />
< property name = " lib.dir " value = " bin " />
< path id = " classpath " >
< fileset dir = " ${lib.dir} " >
< include name = " **/*.jar " />
</ fileset >
< pathelement path = " bin " />
</ path >
<!-- 编码关键字替换 -->
< target name = " replace " description = " 将GBK,gb2312换成UTF-8 " >
< replace dir = " ${jsp.dir} " token = " GBK " value = " UTF-8 " />
< replace dir = " ${jsp.dir} " token = " gb2312 " value = " UTF-8 " />
</ target >
<!-- 初始化任务 -->
< target name = " init-tasks " description = " ==> 生成代码前的初始化工作 " >
< taskdef name = " gbk2utf8 " classname = " org.loushang.bsp.util.GBK2UTF8 " >
< classpath refid = " classpath " />
</ taskdef >
</ target >
<!-- 编码格式转换 -->
< target name = " convert " description = " 改变文件的编码格式 " depends = " init-tasks " >
< gbk2utf8 destDir = " ${jsp.dir} " format = " java " destEncoding = " utf-8 " />
</ target >
<!-- 主任务 -->
< target name = " main " description = " 主任务 " >
< antcall target = " replace " />
< antcall target = " convert " />
</ target >
</ project >
解释一下,首先这个任务进行jsp页编码形式的替换,接着进行内容编码的转换,其中可以指定目标目录,文件格式,目标编码形式。Ant Task主要用到了java的反射技术,我们自己的Task可以继承Task类,然后复写excute()方法,看看GBK2UTF8的源代码:
1
import java.io.BufferedReader;
2 import java.io.File;
3 import java.io.FileOutputStream;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import org.apache.tools.ant.BuildException;
11 import org.apache.tools.ant.Task;
12
13 public class GBK2UTF8 extends Task {
14 private String format = "";
15
16 private String destEncoding = "";
17
18 private String destDir = "";
19
20 private static List list = new ArrayList();
21
22 public String getDestDir() {
23 return destDir;
24 }
25 public void setDestDir(String destDir) {
26 this.destDir = destDir;
27 }
28 /**//*
29 * 复写execute()方法,实现自己的内容
30 */
31 public void execute() throws BuildException {
32 super.execute();
33 getAllFilesOfDir(getDestDir());
34 StringBuffer tempSb = new StringBuffer();
35 int hasRead = 0;
36 for (int i = 0; i < list.size(); i++) {
37 System.out.println(list.get(i));
38 try {
39 BufferedReader bfr = new BufferedReader(new FileReader(
40 (File) list.get(i)));
41 String temp = bfr.readLine();
42 while (temp != null) {
43 tempSb.append(temp);
44 tempSb.append("\n");
45 temp = bfr.readLine();
46 }
47 bfr.close();
48 // 文件内容编码转换
49 OutputStreamWriter osw = new OutputStreamWriter(
50 new FileOutputStream((File) list.get(i)),
51 getDestEncoding());
52 osw.write(tempSb.toString(), 0, tempSb.toString().length());
53 osw.flush();
54 osw.close();
55 tempSb = null;
56 tempSb = new StringBuffer();
57 } catch (IOException e) {
58 e.printStackTrace();
59 }
60 }
61 }
62
63 /**//*
64 * 判断后缀名是否满足条件
65 */
66 private boolean isProperFormat(File file) {
67 return (file.getName().endsWith(getFormat()));
68 }
69
70 /**//*
71 * 取得所有的文件列表
72 */
73 private List getAllFilesOfDir(String dir) {
74 File file = new File(dir);
75 if (!file.exists()) {
76 throw new RuntimeException("给出的目录不存在!");
77 }
78 File[] files = file.listFiles();
79 for (int i = 0; i < files.length; i++) {
80 if (files[i].isFile() && isProperFormat(files[i])) {
81 list.add(files[i].getAbsoluteFile());
82 } else if (files[i].isDirectory()) {
83 getAllFilesOfDir(files[i].getAbsolutePath());
84 } else {
85 // 那些不符合格式的文件将略去
86 }
87 }
88 return list;
89 }
90
91 public String getDestEncoding() {
92 return destEncoding;
93 }
94
95 public void setDestEncoding(String destEncoding) {
96 this.destEncoding = destEncoding;
97 }
98
99 public String getFormat() {
100 return format;
101 }
102
103 public void setFormat(String format) {
104 this.format = format;
105 }
106}
107
2 import java.io.File;
3 import java.io.FileOutputStream;
4 import java.io.FileReader;
5 import java.io.IOException;
6 import java.io.OutputStreamWriter;
7 import java.util.ArrayList;
8 import java.util.List;
9
10 import org.apache.tools.ant.BuildException;
11 import org.apache.tools.ant.Task;
12
13 public class GBK2UTF8 extends Task {
14 private String format = "";
15
16 private String destEncoding = "";
17
18 private String destDir = "";
19
20 private static List list = new ArrayList();
21
22 public String getDestDir() {
23 return destDir;
24 }
25 public void setDestDir(String destDir) {
26 this.destDir = destDir;
27 }
28 /**//*
29 * 复写execute()方法,实现自己的内容
30 */
31 public void execute() throws BuildException {
32 super.execute();
33 getAllFilesOfDir(getDestDir());
34 StringBuffer tempSb = new StringBuffer();
35 int hasRead = 0;
36 for (int i = 0; i < list.size(); i++) {
37 System.out.println(list.get(i));
38 try {
39 BufferedReader bfr = new BufferedReader(new FileReader(
40 (File) list.get(i)));
41 String temp = bfr.readLine();
42 while (temp != null) {
43 tempSb.append(temp);
44 tempSb.append("\n");
45 temp = bfr.readLine();
46 }
47 bfr.close();
48 // 文件内容编码转换
49 OutputStreamWriter osw = new OutputStreamWriter(
50 new FileOutputStream((File) list.get(i)),
51 getDestEncoding());
52 osw.write(tempSb.toString(), 0, tempSb.toString().length());
53 osw.flush();
54 osw.close();
55 tempSb = null;
56 tempSb = new StringBuffer();
57 } catch (IOException e) {
58 e.printStackTrace();
59 }
60 }
61 }
62
63 /**//*
64 * 判断后缀名是否满足条件
65 */
66 private boolean isProperFormat(File file) {
67 return (file.getName().endsWith(getFormat()));
68 }
69
70 /**//*
71 * 取得所有的文件列表
72 */
73 private List getAllFilesOfDir(String dir) {
74 File file = new File(dir);
75 if (!file.exists()) {
76 throw new RuntimeException("给出的目录不存在!");
77 }
78 File[] files = file.listFiles();
79 for (int i = 0; i < files.length; i++) {
80 if (files[i].isFile() && isProperFormat(files[i])) {
81 list.add(files[i].getAbsoluteFile());
82 } else if (files[i].isDirectory()) {
83 getAllFilesOfDir(files[i].getAbsolutePath());
84 } else {
85 // 那些不符合格式的文件将略去
86 }
87 }
88 return list;
89 }
90
91 public String getDestEncoding() {
92 return destEncoding;
93 }
94
95 public void setDestEncoding(String destEncoding) {
96 this.destEncoding = destEncoding;
97 }
98
99 public String getFormat() {
100 return format;
101 }
102
103 public void setFormat(String format) {
104 this.format = format;
105 }
106}
107
这样,就完成任务了。看看是不是很简单?
参考资料:http://ant.apache.org/manual/tutorial-writing-tasks.html