使用Ant作爲順手工具

source : http://unserializableone.blogspot.com/2009/01/using-ant-as-library.html

 

Ant has a lot of predefined tasks that could be a great help. You can use them directly from your java code. Here are some examples using Ant to download a file, unzip a package and exec some shell command.

You would need to have
+ ant-1.7.1.jar
+ ant-launcher-1.7.1.jar
+ commons-io-1.3.2.jar (for exec example, found at http://commons.apache.org/io)

in your classpath.

 

  1. public class AntDemo {  
  2.     
  3.   /** 
  4.    * Download a file at sourceUrl to dest 
  5.    * 
  6.    */  
  7.   public static void download(URL sourceUrl, File dest) {  
  8.     Project dummyProject = new Project();  
  9.     dummyProject.init();  
  10.       
  11.     Get antGet = new Get();  
  12.     antGet.setProject(dummyProject);  
  13.     antGet.setVerbose(true);  
  14.     antGet.setSrc(sourceUrl);  
  15.     antGet.setDest(dest);  
  16.     antGet.execute();  
  17.   }  
  18.   
  19.   /** 
  20.    * Unzip a zip file 
  21.    * 
  22.    */  
  23.   public static void unzip(File src, File dest) {  
  24.     Project dummyProject = new Project();  
  25.     dummyProject.init();  
  26.   
  27.     Expand antUnzip = new Expand();  
  28.     antUnzip.setProject(dummyProject);  
  29.     antUnzip.setSrc(src);  
  30.     antUnzip.setDest(dest);  
  31.     antUnzip.execute();  
  32.   
  33.     /* ant doesn't preserve permission bits 
  34.        need to restore them manually */  
  35.   
  36.     Chmod chmod = new Chmod();  
  37.     chmod.setProject(dummyProject);  
  38.     chmod.setDir(new File(src.getAbsolutePath().replaceAll(".zip""")));  
  39.     chmod.setPerm("a+rx");  
  40.     chmod.setIncludes("**/**");  
  41.     chmod.execute();  
  42.   }  
  43.   
  44.     
  45.   /**  
  46.    * Run a shell command and return the output as String  
  47.    *    
  48.    */  
  49.   public static String exec(String command, List<STRING> params, File workDir) {  
  50.     File outputFile;  
  51.     try {  
  52.       outputFile = File.createTempFile("exec"".out");  
  53.     } catch (IOException e) {  
  54.       throw new RuntimeException(e);  
  55.     }  
  56.   
  57.     Project dummyProject = new Project();  
  58.     dummyProject.init();  
  59.   
  60.     ExecTask execTask = new ExecTask();  
  61.     execTask.setProject(dummyProject);  
  62.     execTask.setOutput(outputFile);  
  63.     execTask.setDir(workDir != null ? workDir : new File(System  
  64.         .getProperty("user.dir")));  
  65.     execTask.setExecutable(command);  
  66.     if (params != null) {  
  67.       for (String param : params) {  
  68.         execTask.createArg().setValue(param);  
  69.       }  
  70.     }  
  71.   
  72.     execTask.execute();  
  73.   
  74.     FileReader reader = null;  
  75.     try {  
  76.       reader = new FileReader(outputFile);  
  77.       return IOUtils.toString(reader);  
  78.     } catch (Exception e) {  
  79.       throw new RuntimeException(e);  
  80.     } finally {  
  81.       IOUtils.closeQuietly(reader);  
  82.       outputFile.delete();  
  83.     }  
  84.   }  
  85.   
  86.   public static void main(String[] args) {  
  87.     List<STRING> params = Arrays.asList(new String[] { "Hello""World" });  
  88.     System.out.println(exec("echo", params, null));  
  89.   }  
  90. }  

public class AntDemo {

/**
* Download a file at sourceUrl to dest
*
*/
public static void download(URL sourceUrl, File dest) {
Project dummyProject = new Project();
dummyProject.init();

Get antGet = new Get();
antGet.setProject(dummyProject);
antGet.setVerbose(true);
antGet.setSrc(sourceUrl);
antGet.setDest(dest);
antGet.execute();
}

/**
* Unzip a zip file
*
*/
public static void unzip(File src, File dest) {
Project dummyProject = new Project();
dummyProject.init();

Expand antUnzip = new Expand();
antUnzip.setProject(dummyProject);
antUnzip.setSrc(src);
antUnzip.setDest(dest);
antUnzip.execute();

/* ant doesn't preserve permission bits
need to restore them manually */

Chmod chmod = new Chmod();
chmod.setProject(dummyProject);
chmod.setDir(new File(src.getAbsolutePath().replaceAll(".zip", "")));
chmod.setPerm("a+rx");
chmod.setIncludes("**/**");
chmod.execute();
}


/**
* Run a shell command and return the output as String
*
*/
public static String exec(String command, List params, File workDir) {
File outputFile;
try {
outputFile = File.createTempFile("exec", ".out");
} catch (IOException e) {
throw new RuntimeException(e);
}

Project dummyProject = new Project();
dummyProject.init();

ExecTask execTask = new ExecTask();
execTask.setProject(dummyProject);
execTask.setOutput(outputFile);
execTask.setDir(workDir != null ? workDir : new File(System
.getProperty("user.dir")));
execTask.setExecutable(command);
if (params != null) {
for (String param : params) {
execTask.createArg().setValue(param);
}
}

execTask.execute();

FileReader reader = null;
try {
reader = new FileReader(outputFile);
return IOUtils.toString(reader);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
IOUtils.closeQuietly(reader);
outputFile.delete();
}
}

public static void main(String[] args) {
List params = Arrays.asList(new String[] { "Hello", "World" });
System.out.println(exec("echo", params, null));
}
}



Just note that you'll need a dummy project for the Ant task. Otherwise you'll get an NullPointerException.

There are a lot of tasks that you could use. The list is here http://ant.apache.org/manual/tasksoverview.html

發佈了695 篇原創文章 · 獲贊 15 · 訪問量 307萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章