ant Task to Copy specified files from Sub directories


The below class defines a custom ant task which is used to copy specified extension files from source sub directories to destination directory
package com.vz.ant.task;

import java.io.File;
import java.io.IOException;

import org.apache.tools.ant.BuildException;

public class CopyFromSubDir extends org.apache.tools.ant.Task {

 private String fromDir;
 private String toDir;
 private String fileExt;

 public void setFromDir(String fromDir) {
  this.fromDir = fromDir;
 }

 public void setToDir(String toDir) {
  this.toDir = toDir;
 }

 public void setFileExt(String fileExt) {
  this.fileExt = fileExt;
 }

 public void execute() throws BuildException {
  File srcDir = null;
  File destDir = null;

  if (fromDir != null && fromDir.trim() != "") {
   srcDir = new File(fromDir);
   if (!srcDir.isDirectory()) {
    System.err.print("fromDir is a File");
    throw new BuildException("fromDir is a File");
   }
  } else {
   System.err.print("fromDir attribute is not given");
   throw new BuildException("fromDir attribute is not given");
  }

  if (toDir != null && toDir.trim() != "") {
   destDir = new File(toDir);
   if (!destDir.exists()) {
    destDir.mkdirs();
   }
  } else {
   System.err.print("toDir attribute is not given");
   throw new BuildException("toDir attribute is not given");
  }

  if (!(fileExt != null && fileExt.trim() != "")) {
   System.err.print("fileExt attribute is not given");
   throw new BuildException("fileExt attribute is not given");
  }

  if (!srcDir.exists()) {
   throw new BuildException("from Directory doesnot exist");
  }

  File files[] = srcDir.listFiles();
  for (File f : files) {
   if (f.isDirectory()) {
    findExtensionFiles(f, fileExt);
   } else {

   }
  }
 }

 private boolean findExtensionFiles(File f, String exten) {

  for (File sf : f.listFiles()) {
   if (sf.getName().contains("." + exten.trim())) {
    try {
     System.out.println(sf.getAbsolutePath());
     System.out.println(toDir + "//" + sf.getName());
     FileCopy.copy(sf.getAbsolutePath(),
       toDir + "//" + sf.getName());
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
  }
  return false;
 }

 public static void main(String[] args) {
  CopyFromSubDir cd = new CopyFromSubDir();
  cd.setFromDir("C:\\Program Files\\Adobe\\Reader 10.0\\");
  cd.setToDir("c:\\anttest");
  cd.setFileExt("dll");
  cd.execute();
 }

}

package com.vz.ant.task;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;

public class FileCopy {
 public static void main(String[] args) {
   if (args.length != 2)    // Check arguments
     System.err.println("Usage: java FileCopy  ");
   else {
     try { copy(args[0], args[1]); }
     catch (IOException e) { System.err.println(e.getMessage()); }
   }
 }

 public static void copy(String from_name, String to_name) throws IOException{
   File from_file = new File(from_name);  // Get File objects from Strings
   File to_file = new File(to_name);
   if (!from_file.exists())
     abort("FileCopy: no such source file: " + from_name);
   if (!from_file.isFile())
     abort("FileCopy: can't copy directory: " + from_name);
   if (!from_file.canRead())
     abort("FileCopy: source file is unreadable: " + from_name);

   if (to_file.isDirectory())
     to_file = new File(to_file, from_file.getName());

   if (to_file.exists()) {
     if (!to_file.canWrite())
       abort("FileCopy: destination file is unwriteable: " + to_name);
     System.out.print("Overwrite existing file " + to_name + "? (Y/N): ");
     System.out.flush();
     BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
     String response = in.readLine();
     if (!response.equals("Y") && !response.equals("y"))
       abort("FileCopy: existing file was not overwritten.");
   }
   else {
     String parent = to_file.getParent();  // Get the destination directory
     if (parent == null) parent = System.getProperty("user.dir"); // or CWD
     File dir = new File(parent);          // Convert it to a file.
     if (!dir.exists())
       abort("FileCopy: destination directory doesn't exist: " + parent);
     if (dir.isFile())
       abort("FileCopy: destination is not a directory: " + parent);
     if (!dir.canWrite())
       abort("FileCopy: destination directory is unwriteable: " + parent);
   }

   FileInputStream from = null;  // Stream to read from source
   FileOutputStream to = null;   // Stream to write to destination
   try {
     from = new FileInputStream(from_file);  // Create input stream
     to = new FileOutputStream(to_file);     // Create output stream
     byte[] buffer = new byte[4096];         // A buffer to hold file contents
     int bytes_read;                         // How many bytes in buffer
     while((bytes_read = from.read(buffer)) != -1) // Read bytes until EOF
       to.write(buffer, 0, bytes_read);            //   write bytes
   }
   finally {
     if (from != null) try { from.close(); } catch (IOException e) { ; }
     if (to != null) try { to.close(); } catch (IOException e) { ; }
   }
 }

 private static void abort(String msg) throws IOException {
   throw new IOException(msg);
 }
}

After creating a jar using above classes use the jar file in ur class path and add the below lines in ur build.xml 1.creating a type def
2. use the above typedef to copy jar files from subdirectories
<typedef name="copySubDirectoryFiles" class="com.vz.ant.task.CopyFromSubDir" 
  classpath="${path}"/>

<copySubDirectoryFiles  fromDir="${srcDir}" toDir="${destDir}"  fileExt="jar" />

No comments:

Post a Comment