介绍Ant 脚本编写的书比较多,而介绍Java 调用 Ant API的书籍和资料比较少,初学者用户在进行Ant编程时会遇到不少麻烦,笔者也是在项目开发过程中,逐渐摸索并掌握了一些Java调用Ant API的一些方法和技巧,并将常用的案例收集起来呈现给大家(本文使用Ant 1.7.0版本),以供大家参考。当然,Ant的API非常丰富,本文仅做抛砖引玉之用,更强大的功能还需要读者自己在结合Ant的API进行摸索和领悟。

 
  
  1. <dependency> 
  2.             <groupId>ant</groupId> 
  3.             <artifactId>ant</artifactId> 
  4.             <version>1.6</version> 
  5.         </dependency> 


1. 目录操作:

1) 创建目录

  1. Project prj=new Project();
  2. Mkdir mkdir=new Mkdir();
  3. mkdir.setProject(prj);
  4. mkdir.setDir(new File("d:tempdir1"));
  5. mkdir.execute();

2) 删除目录

  1. Project prj=new Project();
  2. Delete delete=new Delete();
  3. delete.setProject(prj);
  4. delete.setDir(new File("d:tempdir1")); //可同时将子目录及所有文件删除
  5. delete.execute();

注:对每一个Ant Task,如Mkdir,Delete、Copy、Move、Zip等,都必须设置一个Project对象,可以几个Ant Task共用一个Project对象,但不能有Ant Task不设置Project对象。
 
2. 文件拷贝和移动、更名
1)文件copy

  1. Project prj=new Project();
  2. Copy copy=new Copy();
  3. copy.setProject(prj);
  4. copy.setFile(new File("d:tempf1.txt");
  5. copy.setTodir(new File("d:tempdir1"));
  6. copy.execute(); //将f1.txt文件copy到dir1中

2)copy文件并同时替换其中的内容, 如将 xml中的 @eosapp_name@ 替换成真正的应用名称

  1. Project prj=new Project();
  2. Copy copy = new Copy();
  3. copy.setEncoding("UTF-8");
  4. copy.setProject(prj);
  5. copy.setTodir("d:temp");
  6.  
  7. FileSet fileSet=new FileSet();
  8. fileSet.setDir(new File(eosHome "/base/template.app"));
  9. fileSet.setIncludes("**/*.xml");
  10. copy.addFileset(fileSet);
  11.  
  12. FilterSet filter=copy.createFilterSet();
  13. filter.addFilter("eosapp_name","app1");
  14. copy.execute();

2)文件或目录移动
Move的用法和Copy用法基本一致,Move本身为Copy的子类。

  1. Project prj=new Project();
  2. Copy copy=new Copy();
  3. copy.setProject(prj);
  4. copy.setFile(new File("d:tempf1.txt");
  5. copy.setTodir(new File("d:tempdir1"));
  6. copy.execute(); //将f1.txt文件移动到dir1中

3)文件改名:

  1. Project prj=new Project();
  2. Copy copy=new Copy();
  3. copy.setProject(prj);
  4. copy.setFile(new File("d:tempf1.txt");
  5. copy.setTodir(new File("d:tempf2.txt"));
  6. copy.execute(); //将f1.txt文件更名为f2.txt中

4)目录更名:

  1. Project prj=new Project();
  2. Copy copy=new Copy();
  3. copy.setProject(prj);
  4. copy.setFile(new File("d:tempdir1");
  5. copy.setTodir(new File("d:tempdir2"));
  6. copy.execute(); //将dir1目录更名为dir2,相当于将dir1目录下的所有文件移到dir2目录下

3.使用文件集 FileSet
使用文件集可以同时将多个满足匹配条件的文件集合进行copy、move和压缩等操作。

  1. Project prj=new Project();
  2. Copy copy=new Copy();
  3. copy.setProject(prj);
  4. copy.setTodir(new File("d:temptodir"));
  5.  
  6. FileSet fs=new FileSet();
  7. fs.setProject(prj);
  8. fs.setDir(new File("d:javaprjsrc"));
  9. fs.setIncludes("**/*.*"); //包含所有文件
  10. fs.setExcludes("**/CVS,**/*.class"); //排除CVS相关文件,以及.class文件
  11. copy.addFileset(fs);
  12.  
  13. copy.execute();

注: FileSet的setIncludes, 和setExcludes方法输入pattern, pattern是一个使用“,”或空格分隔的匹配字符串,其中, “**”代表所有文件或目录,“*.*”代表说有文件, “*.java”代表所有扩展名为java的文件。
 
4.目录扫描,查找文件

  1. DirectoryScanner ds=new DirectoryScanner();
  2. ds.setBasedir(new File("d:tempwar"));
  3. ds.setIncludes(new String[] );
  4. ds.scan();
  5. if(ds.getIncludedFilesCount()>0) {
  6. System.out.println("found jsp!");
  7. String[] includeFiles=ds.getIncludedFiles();
  8. for(String file:includeFiles){
  9. System.out.println(file);
  10. }
  11. }

5.文件压缩,打包
//压缩为zip文件

  1. Project prj=new Project();
  2. Zip zip=new Zip();
  3. zip.setProject(prj);
  4. zip.setDestFile(new File("d:tempsrc.zip"));
  5. FileSet fileSet=new FileSet();
  6. fileSet.setProject(prj);
  7. fileSet.setDir(new File("d:javaprjprj1src"));
  8. fileSet.setIncludes("**/*.java");
  9. zip.addFileset(fileSet);
  10. zip.execute();
  11.  
  12. //将class文件打成jar包
  13. Project prj=new Project();
  14. Jar jar=new Jar();
  15. jar.setProject(prj);
  16. jar.setDestFile(new File("d:tempprj1.jar"));
  17. FileSet fileSet=new FileSet();
  18. fileSet.setProject(prj);
  19. fileSet.setDir(new File("d:javaprjprj1bin"));
  20. fileSet.setIncludes("**/*.class,**/*.properties");
  21. jar.addFileset(fileSet);
  22. jar.execute();

6.文件解压
1)将压缩文件中的所有文件解压

  1. Project prj=new Project();
  2. Expand expand=new Expand();
  3. expand.setProject(prj);
  4. expand.setSrc(new File("d:tempsrc.zip"));
  5. expand.setOverwrite(overwrite);
  6. expand.setDest("d:tempoutsrc");
  7. expand.execute();

2)将压缩文件中的符合匹配条件的文件解压

  1. Project prj=new Project();
  2. Expand expand=new Expand();
  3. expand.setProject(prj);
  4. expand.setSrc(new File("d:tempsrc.zip"));
  5. expand.setOverwrite(overwrite);
  6. expand.setDest("d:tempoutsrc");
  7. PatternSet patternset = new PatternSet();
  8. patternset.setIncludes("**/*.java");
  9. patternset.setProject(prj);
  10. expand.addPatternset(patternset);
  11. expand.execute();

3)利用Mapper解压文件: 如将 .../lib/*.jar 解压到 .../WEB-INF/lib目录下(去除目录结构)

  1. Expand expand = new Expand();
  2. expand.setProject(prj);
  3. expand.setSrc(new File(zipFilePath));
  4. expand.setDest(new File(webDir "/WEB-INF/lib"));
  5.  
  6. PatternSet pattern = new PatternSet();
  7. pattern.setIncludes("lib/*.jar");
  8. expand.addPatternset(pattern);
  9.  
  10. FileNameMapper mapper=new FlatFileNameMapper();
  11. expand.add(mapper);
  12.  
  13. /* another way using mapper
  14. Mapper mapper=expand.createMapper();
  15. MapperType type=new MapperType();
  16. type.setValue("flatten");
  17. mapper.setType(type);
  18. */
  19. expand.execute();

7.读取zip文件
1) 读取zip文件中的文件和目录

  1. ZipFile zipfile = new ZipFile(new File(filepath));
  2. for (Enumeration entries = zipfile.getEntries(); entries.hasMoreElements();) {
  3. ZipEntry entry = (ZipEntry) entries.nextElement();
  4. if(entry.isDirectory())
  5. System.out.println("Directory: " entry.getName());
  6. else
  7. System.out.println("file: " entry.getName());
  8. }
  9. zipfile.close(); //ZipFile用完必须close,否则文件被锁定

2)zip文件扫描,在Zip文件中查找目录或文件
  1. ZipScanner scan=new ZipScanner();
  2. scan.setSrc(new File("d:temptest.zip"));
  3. scan.setIncludes(new String[] ); //查找目录(一、二级目录);
  4. scan.scan();
  5. String dirs[]=scan.getIncludedDirectories();
  6. scan.setIncludes(new String[]); //查找文件
  7. scan.scan();
  8. String files[]=scan.getIncludedFiles();