1 37 package net.sourceforge.cruisecontrol.builders; 38 39 import java.io.File ; 40 import java.util.ArrayList ; 41 import java.util.List ; 42 import java.util.Map ; 43 import java.util.StringTokenizer ; 44 45 import net.sourceforge.cruisecontrol.Builder; 46 import net.sourceforge.cruisecontrol.CruiseControlException; 47 import net.sourceforge.cruisecontrol.util.ValidationHelper; 48 import net.sourceforge.cruisecontrol.util.DateUtil; 49 50 import org.apache.log4j.Logger; 51 import org.jdom.Element; 52 53 62 public class MavenBuilder extends Builder { 63 64 private static final Logger LOG = Logger.getLogger(MavenBuilder.class); 65 66 private String projectFile; 67 private String goal; 68 private String mavenScript; 69 private long timeout = ScriptRunner.NO_TIMEOUT; 70 71 public void validate() throws CruiseControlException { 72 super.validate(); 73 74 ValidationHelper.assertIsSet(mavenScript, "mavenScript", this.getClass()); 75 76 ValidationHelper.assertIsSet(goal, "goal", this.getClass()); 77 if (getGoalSets().isEmpty()) { 78 ValidationHelper.assertIsSet(null, "goal", this.getClass()); 79 } 80 } 81 82 86 public Element build(Map buildProperties) throws CruiseControlException { 87 88 File ckFile = new File (mavenScript); 89 ValidationHelper.assertTrue(ckFile.exists(), 90 "Script " + ckFile.getAbsolutePath() + " does not exist"); 91 92 ValidationHelper.assertIsSet(projectFile, "projectFile", this.getClass()); 93 ckFile = new File (projectFile); 94 ValidationHelper.assertTrue(ckFile.exists(), 95 "Project descriptor " + ckFile.getAbsolutePath() + " does not exist"); 96 97 File workingDir = (new File (projectFile)).getParentFile(); 98 99 long startTime = System.currentTimeMillis(); 100 101 Element buildLogElement = new Element("build"); 102 103 List runs = getGoalSets(); 104 for (int runidx = 0; runidx < runs.size(); runidx++) { 105 String goalset = (String ) runs.get(runidx); 106 MavenScript script = new MavenScript(); 107 script.setGoalset(goalset); 108 script.setBuildProperties(buildProperties); 109 script.setMavenScript(mavenScript); 110 script.setProjectFile(projectFile); 111 script.setBuildLogElement(buildLogElement); 112 ScriptRunner scriptRunner = new ScriptRunner(); 113 boolean scriptCompleted = scriptRunner.runScript(workingDir, script, timeout); 114 script.flushCurrentElement(); 115 116 if (!scriptCompleted) { 117 LOG.warn("Build timeout timer of " + timeout + " seconds has expired"); 118 buildLogElement = new Element("build"); 119 buildLogElement.setAttribute("error", "build timeout"); 120 } else if (script.getExitCode() != 0) { 121 synchronized (buildLogElement) { 124 buildLogElement.setAttribute("error", "Return code is " + script.getExitCode()); 125 } 126 } 127 128 if (buildLogElement.getAttribute("error") != null) { 129 break; 130 } 131 132 } 133 134 long endTime = System.currentTimeMillis(); 135 136 buildLogElement.setAttribute("time", DateUtil.getDurationAsString((endTime - startTime))); 137 return buildLogElement; 138 } 139 140 public Element buildWithTarget(Map properties, String target) throws CruiseControlException { 141 String origGoal = goal; 142 try { 143 goal = target; 144 return build(properties); 145 } finally { 146 goal = origGoal; 147 } 148 } 149 150 152 156 public void setMavenScript(String mavenScript) { 157 this.mavenScript = mavenScript; 158 } 159 160 169 public void setGoal(String goal) { 170 this.goal = goal; 171 } 172 173 176 public void setProjectFile(String projectFile) { 177 this.projectFile = projectFile; 178 } 179 180 182 188 protected List getGoalSets() { 189 List al = new ArrayList (); 190 if (goal != null) { 191 StringTokenizer stok = new StringTokenizer (goal, "|"); 192 while (stok.hasMoreTokens()) { 193 String subSet = stok.nextToken().trim(); 194 if (subSet == null || subSet.length() == 0) { 195 continue; 196 } 197 al.add(subSet); 198 } 199 } 200 return al; 201 } 202 203 209 public void setTimeout(long timeout) { 210 this.timeout = timeout; 211 } 212 } 213 | Popular Tags |