1 19 20 package org.netbeans.nbbuild; 21 22 import java.io.File ; 23 import java.util.ArrayList ; 24 import java.util.Hashtable ; 25 import java.util.List ; 26 import java.util.StringTokenizer ; 27 import java.util.Vector ; 28 29 import org.apache.tools.ant.BuildException; 30 import org.apache.tools.ant.Project; 31 import org.apache.tools.ant.Target; 32 import org.apache.tools.ant.Task; 33 import org.apache.tools.ant.taskdefs.Ant; 34 35 39 public class CleanAll extends Task { 40 41 private List <String > modules = new ArrayList <String >(); 42 private List <String > failedmodules = new ArrayList <String >(); 43 private String targetname = "clean"; 44 private File topdir = null; 45 private File [] topdirs = null; 46 private boolean resolvedependencies = false; private String deptargetprefix = ""; private Hashtable targets; 49 private boolean failonerror = true; 51 52 public void setModules (String s) { 53 StringTokenizer tok = new StringTokenizer (s, ", "); 54 modules = new ArrayList <String >(); 55 while (tok.hasMoreTokens ()) 56 modules.add(tok.nextToken()); 57 } 58 59 60 public void setTargetname (String s) { 61 targetname = s; 62 } 63 64 67 public void setDepTargetPrefix (String s) { 68 deptargetprefix = s; 69 } 70 71 72 public void setResolveDependencies (boolean b) { 73 resolvedependencies = b; 74 } 75 76 79 public void setFailOnError (boolean b) { 80 failonerror = b; 81 } 82 83 84 public void setTopdir (File f) { 85 topdir = f; 86 } 87 88 public void setTopdirs (String str) { 89 StringTokenizer st = new StringTokenizer (str, ","); 90 int count = st.countTokens(); 91 topdirs = new File [count]; 92 for (int i = 0; i < count; i++) { 93 topdirs[i] = new File (st.nextToken().trim()); 94 } 95 } 96 97 98 private void resolveDependencies () throws BuildException { 99 Target dummy = new Target (); 100 String dummyName = "nbmerge-" + getOwningTarget().getName(); 101 targets = getProject().getTargets(); 102 while (targets.contains (dummyName)) 103 dummyName += "-x"; 104 dummy.setName (dummyName); 105 for (String module : modules) { 106 dummy.addDependency (deptargetprefix + module); 107 } 108 getProject().addTarget(dummy); 109 @SuppressWarnings ("unchecked") 110 Vector <Target> fullList = getProject().topoSort(dummyName, targets); 111 @SuppressWarnings ("unchecked") 113 Vector <Target> doneList = getProject().topoSort(getOwningTarget().getName(), targets); 114 List <Target> todo = new ArrayList <Target>(fullList.subList(0, fullList.indexOf(dummy))); 115 todo.removeAll(doneList.subList(0, doneList.indexOf(getOwningTarget()))); 116 117 for (Target t : todo) { 118 String _targetname = t.getName(); 119 if (_targetname.startsWith(deptargetprefix)) { 120 String module = _targetname.substring(deptargetprefix.length()); 121 if (modules.indexOf(module) < 0) { 122 modules.add(module); 123 log("Adding dependency module \"" + module + "\" to the list of modules for cleaning", Project.MSG_VERBOSE); 124 } 125 } 126 } 127 } 128 129 public void execute () throws BuildException { 130 131 if (topdirs == null && topdir != null) { 132 topdirs = new File [1]; 133 topdirs[0] = topdir; 134 } 135 136 if (topdir == null && topdirs == null) { 137 throw new BuildException("You must set at least one topdir attribute", getLocation()); 138 } 139 140 if (resolvedependencies) resolveDependencies(); 141 142 for (int j = 0; j < topdirs.length; j++) { 143 topdir = topdirs[j]; 144 for (String module : modules) { 145 Ant ant = (Ant) getProject().createTask("ant"); 146 ant.init (); 147 ant.setLocation(getLocation()); 148 File fl = new File (topdir.getAbsolutePath () + 149 File.separatorChar + module + File.separatorChar + "build.xml"); 150 if (! fl.exists()) { 151 continue; 152 } 153 ant.setDir(new File (topdir, module)); 154 ant.setTarget (targetname); 155 try { 156 log("Process '"+ module + "' location with '" + targetname + "' target", Project.MSG_INFO); 157 ant.execute (); 158 } catch (BuildException be) { 159 if (failonerror) { 160 throw new BuildException(be.getMessage(), be, getLocation()); 161 } else { 162 log("Target \"" + targetname + "\" failed in module \"" + module + "\"", Project.MSG_WARN); 163 log(fl.getAbsolutePath()); 164 log(be.getMessage()); 165 String fname = fl.getAbsolutePath(); 166 failedmodules.add(fname); 167 } 168 } 169 } 170 } 171 if (failedmodules.size() > 0) { 172 log("<cleanall> SOME MODULES FAILED TO BUILD, BUT THEIR BuildException WAS CAUGHT", Project.MSG_WARN); 173 log("<cleanall> cleanfailedmodules=\"" + failedmodules.toString() + "\"", Project.MSG_WARN); 174 } 175 } 176 } 177 | Popular Tags |