1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.io.File ; 40 import java.util.Iterator ; 41 import java.util.List ; 42 import java.util.Map ; 43 import java.util.StringTokenizer ; 44 45 import org.apache.log4j.Logger; 46 import org.jdom.CDATA; 47 import org.jdom.Element; 48 49 import net.sourceforge.cruisecontrol.CruiseControlException; 50 import net.sourceforge.cruisecontrol.util.Commandline; 51 import net.sourceforge.cruisecontrol.util.StreamConsumer; 52 53 54 60 public class MavenScript implements Script, StreamConsumer { 61 private static final Logger LOG = Logger.getLogger(MavenScript.class); 62 63 private Map buildProperties; 64 private String goalset; 65 private String mavenScript; 66 private String projectFile; 67 private int exitCode; 68 69 private Element buildLogElement; 70 private Element currentElement = null; 71 72 77 public Commandline buildCommandline() throws CruiseControlException { 78 Commandline cmdLine = new Commandline(); 79 80 if (mavenScript != null) { 81 cmdLine.setExecutable(mavenScript); 82 } else { 83 throw new CruiseControlException( 84 "Non-script running is not implemented yet.\n" 85 + "As of 1.0-beta-10 Maven startup mechanism is still changing..."); 86 } 87 88 Iterator propertiesIterator = buildProperties.keySet().iterator(); 89 while (propertiesIterator.hasNext()) { 90 String key = (String ) propertiesIterator.next(); 91 cmdLine.createArgument().setValue("-D" + key + "=" + buildProperties.get(key)); 92 } 93 94 if (LOG.isDebugEnabled()) { 95 cmdLine.createArgument().setValue("-X"); 96 } 97 98 cmdLine.createArgument().setValue("-b"); if (projectFile != null) { 100 File pFile = new File (projectFile); 102 cmdLine.createArgument().setValue("-p"); 103 cmdLine.createArgument().setValue(pFile.getName()); 104 } 105 if (goalset != null) { 106 StringTokenizer stok = new StringTokenizer (goalset, " \t\r\n"); 107 while (stok.hasMoreTokens()) { 108 cmdLine.createArgument().setValue(stok.nextToken()); 109 } 110 } 111 112 if (LOG.isDebugEnabled()) { 113 StringBuffer sb = new StringBuffer (); 114 sb.append("Executing Command: "); 115 String [] args = cmdLine.getCommandline(); 116 for (int i = 0; i < args.length; i++) { 117 String arg = args[i]; 118 sb.append(arg); 119 sb.append(" "); 120 } 121 LOG.debug(sb.toString()); 122 } 123 return cmdLine; 124 } 125 126 130 public synchronized void consumeLine(String line) { 131 if (line == null || line.length() == 0 || buildLogElement == null) { 132 return; 133 } 134 135 synchronized (buildLogElement) { 136 if (line.startsWith("BUILD FAILED")) { 138 buildLogElement.setAttribute("error", "BUILD FAILED detected"); 139 } else if (line.startsWith("org.apache.maven.MavenException")) { 140 buildLogElement.setAttribute("error", "You have encountered an unknown error running Maven: " + line); 141 } else if (line.startsWith("The build cannot continue")) { 142 buildLogElement.setAttribute("error", "The build cannot continue: Unsatisfied Dependency"); 143 } else if ( 144 line.endsWith(":") && !line.startsWith(" ") && !line.startsWith("\t")) { 147 makeNewCurrentElement(line.substring(0, line.lastIndexOf(':'))); 148 return; } 150 Element msg = new Element("message"); 151 msg.addContent(new CDATA(line)); 152 msg.setAttribute("priority", "info"); 155 if (currentElement == null) { 156 buildLogElement.addContent(msg); 157 158 } else { 159 currentElement.addContent(msg); 160 } 161 } 162 } 163 164 private Element makeNewCurrentElement(String cTask) { 165 if (buildLogElement == null) { 166 return null; 167 } 168 synchronized (buildLogElement) { 169 flushCurrentElement(); 170 currentElement = new Element("mavengoal"); 171 currentElement.setAttribute("name", cTask); 172 currentElement.setAttribute("time", "? seconds"); 173 return currentElement; 174 } 175 } 176 177 protected void flushCurrentElement() { 178 if (buildLogElement == null) { 179 return; 180 } 181 synchronized (buildLogElement) { 182 if (currentElement != null) { 183 if (buildLogElement.getAttribute("error") != null) { 184 List lst = currentElement.getChildren("message"); 187 if (lst != null) { 188 Iterator it = lst.iterator(); 189 while (it.hasNext()) { 190 Element msg = (Element) it.next(); 191 msg.setAttribute("priority", "error"); 192 } 193 } 194 } 195 buildLogElement.addContent(currentElement); 196 } 197 currentElement = null; 198 } 199 } 200 201 202 205 public void setBuildProperties(Map buildProperties) { 206 this.buildProperties = buildProperties; 207 } 208 211 public void setGoalset(String goalset) { 212 this.goalset = goalset; 213 } 214 217 public void setMavenScript(String mavenScript) { 218 this.mavenScript = mavenScript; 219 } 220 223 public void setProjectFile(String projectFile) { 224 this.projectFile = projectFile; 225 } 226 229 public int getExitCode() { 230 return exitCode; 231 } 232 235 public void setExitCode(int exitCode) { 236 this.exitCode = exitCode; 237 } 238 241 public void setBuildLogElement(Element buildLogElement) { 242 this.buildLogElement = buildLogElement; 243 } 244 } 245 | Popular Tags |