1 package net.sourceforge.cruisecontrol.builders; 2 3 import java.io.File ; 4 import java.util.Iterator ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.StringTokenizer ; 8 9 import org.apache.log4j.Logger; 10 import org.jdom.CDATA; 11 import org.jdom.Element; 12 13 import net.sourceforge.cruisecontrol.CruiseControlException; 14 import net.sourceforge.cruisecontrol.util.Commandline; 15 import net.sourceforge.cruisecontrol.util.StreamConsumer; 16 17 24 public class Maven2Script implements Script, StreamConsumer { 25 26 private static final String ERROR = "error"; 27 private static final String SUCCESS = "success"; 28 private static final Logger LOG = Logger.getLogger(Maven2Script.class); 29 30 private String goalset; 31 private String mvn; 32 private String pomFile; 33 private String settingsFile; 34 private String flags; 35 private Element buildLogElement; private Map buildProperties; 37 private String activateProfiles; 38 39 private int exitCode; 40 private Element currentElement; 41 42 51 public Maven2Script(Element buildLogElement, String mvn, String pomFile, String goals, 52 String settingsFile, String activateProfiles, String flags) { 53 54 this.buildLogElement = buildLogElement; 55 this.mvn = mvn; 56 this.pomFile = pomFile; 57 this.goalset = goals; 58 this.settingsFile = settingsFile; 59 this.flags = flags; 60 this.activateProfiles = activateProfiles; 61 } 62 63 68 public Commandline buildCommandline() throws CruiseControlException { 69 70 Commandline cmdLine = new Commandline(); 72 cmdLine.setExecutable(mvn); 73 74 cmdLine.createArgument().setValue("-B"); 76 77 if (LOG.isDebugEnabled()) { 79 cmdLine.createArgument().setValue("-X"); 80 } 81 82 if (settingsFile != null) { 84 cmdLine.createArgument().setValue("-s"); 85 cmdLine.createArgument().setValue(settingsFile); 86 } 87 88 if (pomFile != null) { 89 cmdLine.createArgument().setValue("-f"); 90 cmdLine.createArgument().setValue(new File (pomFile).getName()); 91 } 92 93 if (activateProfiles != null) { 95 cmdLine.createArgument().setValue("-P"); 96 cmdLine.createArgument().setValue(activateProfiles); 97 } 98 99 if (flags != null) { 100 cmdLine.createArgument().setValue(flags); 101 } 102 103 104 if (goalset != null) { 105 StringTokenizer stok = new StringTokenizer (goalset, " \t\r\n"); 106 while (stok.hasMoreTokens()) { 107 cmdLine.createArgument().setValue(stok.nextToken()); 108 } 109 } 110 111 Iterator propertiesIterator = buildProperties.keySet().iterator(); 112 while (propertiesIterator.hasNext()) { 113 String key = (String ) propertiesIterator.next(); 114 String value = (String ) buildProperties.get(key); 115 if (value.indexOf(' ') == -1) { 117 cmdLine.createArgument().setValue("-D" + key + "=" + value); 118 } else { 119 LOG.error("Maven2Script ignoring property with space. Key:" + key + "; Value:" + value); 120 } 121 } 122 123 if (LOG.isDebugEnabled()) { 125 StringBuffer sb = new StringBuffer (); 126 sb.append("Executing Command: "); 127 String [] args = cmdLine.getCommandline(); 128 for (int i = 0; i < args.length; i++) { 129 String arg = args[i]; 130 sb.append(arg); 131 sb.append(" "); 132 } 133 LOG.debug(sb.toString()); 134 } 135 136 return cmdLine; 137 } 138 139 143 public void consumeLine(String line) { 144 String level = ""; 145 String infoLine = null; 146 if (line == null || line.length() == 0 || buildLogElement == null) { 147 return; 148 } 149 150 synchronized (buildLogElement) { 151 if (line.startsWith("[ERROR]")) { 154 level = "error"; 155 infoLine = line.substring(line.indexOf(']') + 1).trim(); 156 } else if (line.startsWith("[INFO]") || line.startsWith("[DEBUG]")) { 157 level = "info"; 158 infoLine = line.substring(line.indexOf(']') + 1).trim(); 159 } else { 160 level = "info"; 161 infoLine = line; 162 } 163 if (infoLine.startsWith("BUILD SUCCESSFUL")) { 164 buildLogElement.setAttribute(SUCCESS, "BUILD SUCCESSFUL detected"); 165 } else if (infoLine.startsWith("BUILD FAILURE")) { 166 buildLogElement.setAttribute(ERROR, "BUILD FAILURE detected"); 167 } else if (infoLine.startsWith("BUILD ERROR")) { 168 buildLogElement.setAttribute(ERROR, "BUILD ERROR detected"); 169 173 } else if (infoLine.startsWith("[") 174 && infoLine.endsWith("]") 175 && infoLine.indexOf(":") > -1) { makeNewCurrentElement(infoLine.substring(1, infoLine.length() - 1)); 177 return; } 179 180 Element msg = new Element("message"); 181 msg.addContent(new CDATA(line)); 182 msg.setAttribute("priority", level); 185 if (currentElement == null) { 186 buildLogElement.addContent(msg); 187 } else { 188 currentElement.addContent(msg); 189 } 190 } 191 } 192 193 private Element makeNewCurrentElement(String cTask) { 194 if (buildLogElement == null) { 195 return null; 196 } 197 synchronized (buildLogElement) { 198 flushCurrentElement(); 199 currentElement = new Element("mavengoal"); 200 currentElement.setAttribute("name", cTask); 201 return currentElement; 202 } 203 } 204 205 protected void flushCurrentElement() { 206 207 if (buildLogElement == null) { 208 return; 209 } 210 synchronized (buildLogElement) { 211 if (currentElement != null) { 212 213 if (buildLogElement.getAttribute(SUCCESS) != null && buildLogElement.getAttribute(ERROR) == null) { 214 LOG.debug("Ok : BUILD SUCCESSFUL"); } else if (buildLogElement.getAttribute(ERROR) != null) { 216 List lst = currentElement.getChildren("message"); 219 if (lst != null) { 220 Iterator it = lst.iterator(); 221 while (it.hasNext()) { 222 Element msg = (Element) it.next(); 223 msg.setAttribute("priority", ERROR); 224 } 225 } 226 } 227 buildLogElement.addContent(currentElement); 228 currentElement = null; 229 } 230 } 231 } 232 233 234 237 public void setBuildProperties(Map buildProperties) { 238 this.buildProperties = buildProperties; 239 } 240 243 public void setGoalset(String goalset) { 244 this.goalset = goalset; 245 } 246 249 public void setMvnScript(String mvnScript) { 250 this.mvn = mvnScript; 251 } 252 255 public void setPomFile(String pomFile) { 256 this.pomFile = pomFile; 257 } 258 259 262 public int getExitCode() { 263 264 return exitCode; 265 } 266 267 270 public void setExitCode(int exitCode) { 271 272 this.exitCode = exitCode; 273 } 274 275 } 276 | Popular Tags |