1 18 19 package org.apache.tools.ant.taskdefs; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.util.Enumeration ; 24 import java.util.Hashtable ; 25 import org.apache.tools.ant.BuildException; 26 import org.apache.tools.ant.DirectoryScanner; 27 import org.apache.tools.ant.Project; 28 29 36 37 public class Copydir extends MatchingTask { 38 39 private File srcDir; 40 private File destDir; 41 private boolean filtering = false; 42 private boolean flatten = false; 43 private boolean forceOverwrite = false; 44 private Hashtable filecopyList = new Hashtable (); 45 46 51 public void setSrc(File src) { 52 srcDir = src; 53 } 54 55 60 public void setDest(File dest) { 61 destDir = dest; 62 } 63 64 69 public void setFiltering(boolean filter) { 70 filtering = filter; 71 } 72 73 78 public void setFlatten(boolean flatten) { 79 this.flatten = flatten; 80 } 81 82 88 public void setForceoverwrite(boolean force) { 89 forceOverwrite = force; 90 } 91 92 96 public void execute() throws BuildException { 97 log("DEPRECATED - The copydir task is deprecated. Use copy instead."); 98 99 if (srcDir == null) { 100 throw new BuildException("src attribute must be set!", 101 getLocation()); 102 } 103 104 if (!srcDir.exists()) { 105 throw new BuildException("srcdir " + srcDir.toString() 106 + " does not exist!", getLocation()); 107 } 108 109 if (destDir == null) { 110 throw new BuildException("The dest attribute must be set.", 111 getLocation()); 112 } 113 114 if (srcDir.equals(destDir)) { 115 log("Warning: src == dest", Project.MSG_WARN); 116 } 117 118 DirectoryScanner ds = super.getDirectoryScanner(srcDir); 119 120 try { 121 String [] files = ds.getIncludedFiles(); 122 scanDir(srcDir, destDir, files); 123 if (filecopyList.size() > 0) { 124 log("Copying " + filecopyList.size() + " file" 125 + (filecopyList.size() == 1 ? "" : "s") 126 + " to " + destDir.getAbsolutePath()); 127 Enumeration e = filecopyList.keys(); 128 while (e.hasMoreElements()) { 129 String fromFile = (String ) e.nextElement(); 130 String toFile = (String ) filecopyList.get(fromFile); 131 try { 132 getProject().copyFile(fromFile, toFile, filtering, 133 forceOverwrite); 134 } catch (IOException ioe) { 135 String msg = "Failed to copy " + fromFile + " to " 136 + toFile + " due to " + ioe.getMessage(); 137 throw new BuildException(msg, ioe, getLocation()); 138 } 139 } 140 } 141 } finally { 142 filecopyList.clear(); 143 } 144 } 145 146 private void scanDir(File from, File to, String [] files) { 147 for (int i = 0; i < files.length; i++) { 148 String filename = files[i]; 149 File srcFile = new File (from, filename); 150 File destFile; 151 if (flatten) { 152 destFile = new File (to, new File (filename).getName()); 153 } else { 154 destFile = new File (to, filename); 155 } 156 if (forceOverwrite 157 || (srcFile.lastModified() > destFile.lastModified())) { 158 filecopyList.put(srcFile.getAbsolutePath(), 159 destFile.getAbsolutePath()); 160 } 161 } 162 } 163 } 164 | Popular Tags |