1 package org.antmod.tasks; 2 3 import java.io.BufferedInputStream ; 4 import java.io.File ; 5 import java.io.FileInputStream ; 6 import java.io.IOException ; 7 import java.util.ArrayList ; 8 import java.util.Enumeration ; 9 import java.util.HashMap ; 10 import java.util.Iterator ; 11 import java.util.List ; 12 import java.util.Properties ; 13 14 import org.antmod.util.AntUtil; 15 import org.apache.commons.io.FileUtils; 16 import org.apache.commons.lang.SystemUtils; 17 import org.apache.tools.ant.BuildException; 18 import org.apache.tools.ant.Project; 19 import org.apache.tools.ant.taskdefs.Ant; 20 import org.apache.tools.ant.taskdefs.Property; 21 import org.apache.tools.ant.types.FilterSet; 22 23 30 public class ConditionalAnt extends Ant { 31 32 private String defaultTarget; 33 private String otherAntfileName; 34 private String otherTarget; 35 private File checkChangesDir = null; 36 private boolean checkChangesOutput = false; 37 private ArrayList passbackRefs = new ArrayList (0); 38 39 42 private boolean inheritAllProperty = true; 43 44 47 private boolean inheritRefsProperty = false; 48 49 52 private ArrayList properties = new ArrayList (); 53 54 57 private File dirProperty = null; 58 59 60 private static HashMap newestFileInDir = new HashMap (); 61 62 65 public ConditionalAnt() { 66 setInheritRefs(true); 68 } 69 70 75 public void setInheritAll(boolean inheritAll) { 76 this.inheritAllProperty = inheritAll; 77 super.setInheritAll(inheritAll); 78 } 79 80 85 public void setInheritRefs(boolean inheritRefs) { 86 this.inheritRefsProperty = inheritRefs; 87 super.setInheritRefs(inheritRefs); 88 } 89 90 95 public void setDir(File dir) { 96 this.dirProperty = dir; 97 super.setDir(dir); 98 } 99 100 105 public Property createProperty() { 106 Property result = super.createProperty(); 107 this.properties.add(result); 108 return result; 109 } 110 111 public void setAntfile(String antFile) { 112 this.otherAntfileName = antFile; 113 super.setAntfile(antFile); 114 } 115 116 public void setTarget(String target) { 117 this.otherTarget = target; 118 super.setTarget(target); 119 } 120 121 public void setDefaultTarget(String defaultTarget) { 122 this.defaultTarget = defaultTarget; 123 } 124 125 public void setCheckChangesDir(File checkChangesDir) { 126 this.checkChangesDir = checkChangesDir; 127 } 128 129 public void setCheckChangesOutput(boolean checkChangesOutput) { 130 this.checkChangesOutput = checkChangesOutput; 131 } 132 133 144 public void addPassbackRef(Reference passbackReference) { 145 this.passbackRefs.add(passbackReference); 146 } 147 148 153 public void setPassbackRefs(List passbackRefs) { 154 this.passbackRefs = new ArrayList (passbackRefs); 155 } 156 157 164 public static boolean isExecuteNotNeeded(File checkChangesDir, String otherTarget, String defaultTarget) { 165 if (checkChangesDir != null) { 166 File [] checkChangesDirList = checkChangesDir.listFiles(); 168 if (checkChangesDirList != null && checkChangesDirList.length > 0) { 169 File newestFile = getNewestFile(checkChangesDir); 170 long lastExecTime = readLastExecTime(checkChangesDir, otherTarget + "-" + defaultTarget); 171 if (newestFile == null) { 172 System.err.println("Unknown whether changes occured in " + checkChangesDir.getPath() + ", continuing to execute '" + otherTarget + "'" + ((defaultTarget != null) ? " or '" + defaultTarget + "'" : "")); 173 } 174 else if (newestFile.lastModified() <= lastExecTime) { 175 return true; 178 } 179 else { 180 } 182 } 183 } 184 return false; 185 } 186 187 191 public void execute() throws BuildException { 192 setTaskName("cond-ant"); 194 195 if (isExecuteNotNeeded(this.checkChangesDir, this.otherTarget, this.defaultTarget)) { 196 if (this.checkChangesOutput) { 197 log("No changes for '" + this.otherTarget + "' in module: '" + this.checkChangesDir.getName() + "'", Project.MSG_WARN); 198 } 199 return; 200 } 201 202 203 File otherAntfile = null; 205 if (this.dirProperty != null) { 206 otherAntfile = new File (this.dirProperty, this.otherAntfileName); 207 } else { 208 otherAntfile = new File (this.otherAntfileName); 209 } 210 if (otherAntfile.exists()) { 211 Project newProject = AntUtil.getAntProject(getProject(), otherAntfile, false); 212 213 Enumeration targetNames = newProject.getTargets().keys(); 215 while (targetNames.hasMoreElements()) { 216 if (((String )targetNames.nextElement()).equalsIgnoreCase(this.otherTarget)) { 217 if (this.passbackRefs.isEmpty()) { 218 super.execute(); 219 } else { 220 customExecute(otherAntfile); 221 } 222 223 if (checkChangesDir != null) { 225 writeLastExecTime(checkChangesDir, otherTarget + "-" + defaultTarget, System.currentTimeMillis()); 226 } 227 return; 228 } 229 } 230 } 231 232 if (this.defaultTarget != null) { 234 getProject().executeTarget(this.defaultTarget); 235 } 236 237 if (checkChangesDir != null) { 239 writeLastExecTime(checkChangesDir, otherTarget + "-" + defaultTarget, System.currentTimeMillis()); 240 } 241 } 242 243 248 private void customExecute(File otherAntfile) { 249 251 Project newProject = AntUtil.getAntProject(getProject(), otherAntfile, this.inheritAllProperty); 253 254 if (this.dirProperty != null) { 256 newProject.setBaseDir(this.dirProperty); 257 newProject.setInheritedProperty("basedir", this.dirProperty.getAbsolutePath()); 258 } 259 260 if (this.inheritRefsProperty) { 262 AntUtil.inheritRefs(getProject(), newProject); 263 } 264 265 Iterator iter = this.properties.iterator(); 267 while (iter.hasNext()) { 268 Property p = (Property) iter.next(); 269 p.setProject(newProject); 270 p.execute(); 271 } 272 getProject().copyInheritedProperties(newProject); 273 274 newProject.executeTarget(this.otherTarget); 276 277 Iterator refIter = this.passbackRefs.iterator(); 279 while (refIter.hasNext()) { 280 Reference passbackRef = (Reference) refIter.next(); 281 Object childRef = newProject.getReference(passbackRef.getRefId()); 282 if (childRef != null) { 283 String toRefid = passbackRef.getToRefid(); 284 if (toRefid == null) { 285 toRefid = passbackRef.getRefId(); 286 } 287 getProject().addReference(toRefid, childRef); 289 } 291 } 292 } 293 294 297 private static long readLastExecTime(File dir, String targetKey) { 298 File lastExecFile = getLastExecFile(dir, targetKey); 299 if (!lastExecFile.exists()) { 300 return -1; 301 } else { 302 return lastExecFile.lastModified(); 303 } 304 } 305 306 309 private void writeLastExecTime(File dir, String targetKey, long newTime) { 310 File lastExecFile = getLastExecFile(dir, targetKey); 311 if (lastExecFile.exists()) { 312 lastExecFile.delete(); 313 } 314 try { 315 lastExecFile.getParentFile().mkdirs(); 316 FileUtils.writeStringToFile(lastExecFile, "This file intentionally left blank.", SystemUtils.FILE_ENCODING); 317 } 318 catch (IOException e) { 319 e.printStackTrace(); 320 } 321 } 322 323 326 private static File getLastExecFile(File dir, String targetKey) { 327 File tmpDir = new File ( 328 SystemUtils.JAVA_IO_TMPDIR, 329 SystemUtils.USER_NAME + File.separator + "antmod" + File.separator + dir.getPath().replace(':', File.separatorChar)); 330 File lastExecFile = new File (tmpDir, targetKey + ".accesstime"); 331 return lastExecFile; 332 } 333 334 338 private static File getNewestFile(File dir) { 339 File result = (File )newestFileInDir.get(dir.getPath()); 340 if (result == null) { 341 result = getNewestFileTimeImpl(dir); 342 newestFileInDir.put(dir.getPath(), result); 343 } 344 return result; 345 } 346 347 350 private static File getNewestFileTimeImpl(File dir) { 351 File result = null; 352 try { 353 ArrayList ignores = new ArrayList (0); 355 File ignoreFile = new File (dir, ".cvsignore"); 356 if (ignoreFile.exists()) { 357 Properties fileNames = new Properties (); 358 BufferedInputStream in = new BufferedInputStream (new FileInputStream (ignoreFile)); 359 fileNames.load(in); 360 Iterator it = fileNames.keySet().iterator(); 361 while (it.hasNext()) { 362 String newIgnore = (String )it.next(); 363 ignores.add(newIgnore); 364 } 366 } 367 368 ignores.add("build.number"); 370 ignores.add("CVS"); 371 ignores.add(".svn"); 372 ignores.add("build"); 373 ignores.add("build.xml"); 374 375 File [] list = dir.listFiles(); 377 for (int i = 0; list != null && i < list.length; i++) { 378 File file = list[i]; 379 380 if (ignores.contains(file.getName())) { 381 } 384 else if (file.isDirectory()) { 385 File newestInDir = getNewestFileTimeImpl(file); 386 if (result == null || 387 (newestInDir != null && newestInDir.lastModified() > result.lastModified())) 388 { 389 result = newestInDir; 390 } 391 } 392 else if (result == null || file.lastModified() > result.lastModified()) { 393 result = file; 394 } 395 } 396 } catch (IOException ioe) { 397 ioe.printStackTrace(); 398 } 399 return result; 400 } 401 402 } 403 | Popular Tags |