1 18 package org.apache.tools.ant.taskdefs; 19 20 import java.io.File ; 21 import java.io.IOException ; 22 23 import java.util.Vector ; 24 import java.util.Enumeration ; 25 26 import org.apache.tools.ant.Task; 27 import org.apache.tools.ant.Project; 28 import org.apache.tools.ant.BuildException; 29 30 import org.apache.tools.ant.types.Path; 31 import org.apache.tools.ant.types.DirSet; 32 import org.apache.tools.ant.types.FileSet; 33 import org.apache.tools.ant.types.FileList; 34 import org.apache.tools.ant.types.PropertySet; 35 import org.apache.tools.ant.types.Reference; 36 import org.apache.tools.ant.types.ResourceCollection; 37 38 import org.apache.tools.ant.taskdefs.Ant.TargetElement; 39 40 41 63 public class SubAnt 64 extends Task { 65 66 private Path buildpath; 67 68 private Ant ant = null; 69 private String subTarget = null; 70 private String antfile = "build.xml"; 71 private File genericantfile = null; 72 private boolean verbose = false; 73 private boolean inheritAll = false; 74 private boolean inheritRefs = false; 75 private boolean failOnError = true; 76 private String output = null; 77 78 private Vector properties = new Vector (); 79 private Vector references = new Vector (); 80 private Vector propertySets = new Vector (); 81 82 83 private Vector targets = new Vector (); 84 85 91 public void handleOutput(String output) { 92 if (ant != null) { 93 ant.handleOutput(output); 94 } else { 95 super.handleOutput(output); 96 } 97 } 98 99 114 public int handleInput(byte[] buffer, int offset, int length) 115 throws IOException { 116 if (ant != null) { 117 return ant.handleInput(buffer, offset, length); 118 } else { 119 return super.handleInput(buffer, offset, length); 120 } 121 } 122 123 130 public void handleFlush(String output) { 131 if (ant != null) { 132 ant.handleFlush(output); 133 } else { 134 super.handleFlush(output); 135 } 136 } 137 138 145 public void handleErrorOutput(String output) { 146 if (ant != null) { 147 ant.handleErrorOutput(output); 148 } else { 149 super.handleErrorOutput(output); 150 } 151 } 152 153 160 public void handleErrorFlush(String output) { 161 if (ant != null) { 162 ant.handleErrorFlush(output); 163 } else { 164 super.handleErrorFlush(output); 165 } 166 } 167 168 171 public void execute() { 172 if (buildpath == null) { 173 throw new BuildException("No buildpath specified"); 174 } 175 176 final String [] filenames = buildpath.list(); 177 final int count = filenames.length; 178 if (count < 1) { 179 log("No sub-builds to iterate on", Project.MSG_WARN); 180 return; 181 } 182 188 BuildException buildException = null; 189 for (int i = 0; i < count; ++i) { 190 File file = null; 191 String subdirPath = null; 192 Throwable thrownException = null; 193 try { 194 File directory = null; 195 file = new File (filenames[i]); 196 if (file.isDirectory()) { 197 if (verbose) { 198 subdirPath = file.getPath(); 199 log("Entering directory: " + subdirPath + "\n", Project.MSG_INFO); 200 } 201 if (genericantfile != null) { 202 directory = file; 203 file = genericantfile; 204 } else { 205 file = new File (file, antfile); 206 } 207 } 208 execute(file, directory); 209 if (verbose && subdirPath != null) { 210 log("Leaving directory: " + subdirPath + "\n", Project.MSG_INFO); 211 } 212 } catch (RuntimeException ex) { 213 if (!(getProject().isKeepGoingMode())) { 214 if (verbose && subdirPath != null) { 215 log("Leaving directory: " + subdirPath + "\n", Project.MSG_INFO); 216 } 217 throw ex; } 219 thrownException = ex; 220 } catch (Throwable ex) { 221 if (!(getProject().isKeepGoingMode())) { 222 if (verbose && subdirPath != null) { 223 log("Leaving directory: " + subdirPath + "\n", Project.MSG_INFO); 224 } 225 throw new BuildException(ex); 226 } 227 thrownException = ex; 228 } 229 if (thrownException != null) { 230 if (thrownException instanceof BuildException) { 231 log("File '" + file 232 + "' failed with message '" 233 + thrownException.getMessage() + "'.", Project.MSG_ERR); 234 if (buildException == null) { 236 buildException = (BuildException) thrownException; 237 } 238 } else { 239 log("Target '" + file 240 + "' failed with message '" 241 + thrownException.getMessage() + "'.", Project.MSG_ERR); 242 thrownException.printStackTrace(System.err); 243 if (buildException == null) { 244 buildException = 245 new BuildException(thrownException); 246 } 247 } 248 if (verbose && subdirPath != null) { 249 log("Leaving directory: " + subdirPath + "\n", Project.MSG_INFO); 250 } 251 } 252 } 253 if (buildException != null) { 255 throw buildException; 256 } 257 } 258 259 269 private void execute(File file, File directory) 270 throws BuildException { 271 if (!file.exists() || file.isDirectory() || !file.canRead()) { 272 String msg = "Invalid file: " + file; 273 if (failOnError) { 274 throw new BuildException(msg); 275 } 276 log(msg, Project.MSG_WARN); 277 return; 278 } 279 280 ant = createAntTask(directory); 281 String antfilename = file.getAbsolutePath(); 282 ant.setAntfile(antfilename); 283 for (int i = 0; i < targets.size(); i++) { 284 TargetElement targetElement = (TargetElement) targets.get(i); 285 ant.addConfiguredTarget(targetElement); 286 } 287 288 try { 289 ant.execute(); 290 } catch (BuildException e) { 291 if (failOnError) { 292 throw e; 293 } 294 log("Failure for target '" + subTarget 295 + "' of: " + antfilename + "\n" 296 + e.getMessage(), Project.MSG_WARN); 297 } catch (Throwable e) { 298 if (failOnError) { 299 throw new BuildException(e); 300 } 301 log("Failure for target '" + subTarget 302 + "' of: " + antfilename + "\n" 303 + e.toString(), 304 Project.MSG_WARN); 305 } finally { 306 ant = null; 307 } 308 } 309 310 318 public void setAntfile(String antfile) { 319 this.antfile = antfile; 320 } 321 322 332 public void setGenericAntfile(File afile) { 333 this.genericantfile = afile; 334 } 335 336 341 public void setFailonerror(boolean failOnError) { 342 this.failOnError = failOnError; 343 } 344 345 351 public void setTarget(String target) { 353 this.subTarget = target; 354 } 355 356 361 public void addConfiguredTarget(TargetElement t) { 362 String name = t.getName(); 363 if ("".equals(name)) { 364 throw new BuildException("target name must not be empty"); 365 } 366 targets.add(t); 367 } 368 369 374 public void setVerbose(boolean on) { 375 this.verbose = on; 376 } 377 378 384 public void setOutput(String s) { 385 this.output = s; 386 } 387 388 394 public void setInheritall(boolean b) { 395 this.inheritAll = b; 396 } 397 398 404 public void setInheritrefs(boolean b) { 405 this.inheritRefs = b; 406 } 407 408 414 public void addProperty(Property p) { 415 properties.addElement(p); 416 } 417 418 424 public void addReference(Ant.Reference r) { 425 references.addElement(r); 426 } 427 428 433 public void addPropertyset(PropertySet ps) { 434 propertySets.addElement(ps); 435 } 436 437 446 public void addDirset(DirSet set) { 447 add(set); 448 } 449 450 459 public void addFileset(FileSet set) { 460 add(set); 461 } 462 463 471 public void addFilelist(FileList list) { 472 add(list); 473 } 474 475 481 public void add(ResourceCollection rc) { 482 getBuildpath().add(rc); 483 } 484 485 490 public void setBuildpath(Path s) { 491 getBuildpath().append(s); 492 } 493 494 499 public Path createBuildpath() { 500 return getBuildpath().createPath(); 501 } 502 503 509 public Path.PathElement createBuildpathElement() { 510 return getBuildpath().createPathElement(); 511 } 512 513 518 private Path getBuildpath() { 519 if (buildpath == null) { 520 buildpath = new Path(getProject()); 521 } 522 return buildpath; 523 } 524 525 530 public void setBuildpathRef(Reference r) { 531 createBuildpath().setRefid(r); 532 } 533 534 542 private Ant createAntTask(File directory) { 543 Ant antTask = new Ant(this); 544 antTask.init(); 545 if (subTarget != null && subTarget.length() > 0) { 546 antTask.setTarget(subTarget); 547 } 548 549 550 if (output != null) { 551 antTask.setOutput(output); 552 } 553 554 if (directory != null) { 555 antTask.setDir(directory); 556 } 557 558 antTask.setInheritAll(inheritAll); 559 for (Enumeration i = properties.elements(); i.hasMoreElements();) { 560 copyProperty(antTask.createProperty(), (Property) i.nextElement()); 561 } 562 563 for (Enumeration i = propertySets.elements(); i.hasMoreElements();) { 564 antTask.addPropertyset((PropertySet) i.nextElement()); 565 } 566 567 antTask.setInheritRefs(inheritRefs); 568 for (Enumeration i = references.elements(); i.hasMoreElements();) { 569 antTask.addReference((Ant.Reference) i.nextElement()); 570 } 571 572 return antTask; 573 } 574 575 581 private static void copyProperty(Property to, Property from) { 582 to.setName(from.getName()); 583 584 if (from.getValue() != null) { 585 to.setValue(from.getValue()); 586 } 587 if (from.getFile() != null) { 588 to.setFile(from.getFile()); 589 } 590 if (from.getResource() != null) { 591 to.setResource(from.getResource()); 592 } 593 if (from.getPrefix() != null) { 594 to.setPrefix(from.getPrefix()); 595 } 596 if (from.getRefid() != null) { 597 to.setRefid(from.getRefid()); 598 } 599 if (from.getEnvironment() != null) { 600 to.setEnvironment(from.getEnvironment()); 601 } 602 if (from.getClasspath() != null) { 603 to.setClasspath(from.getClasspath()); 604 } 605 } 606 607 } | Popular Tags |