1 package net.sourceforge.cruisecontrol.builders; 2 3 import java.io.File ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 import java.util.Map ; 7 import java.util.StringTokenizer ; 8 9 import net.sourceforge.cruisecontrol.Builder; 10 import net.sourceforge.cruisecontrol.CruiseControlException; 11 import net.sourceforge.cruisecontrol.util.DateUtil; 12 import net.sourceforge.cruisecontrol.util.ValidationHelper; 13 14 import org.apache.log4j.Logger; 15 import org.jdom.Element; 16 17 27 public class Maven2Builder extends Builder { 28 29 private static final Logger LOG = Logger.getLogger(Maven2Builder.class); 30 private static final String MVN = "bin" + File.separator + "mvn"; 31 32 private String mvnHome; 33 private String mvnScript; 34 private String pomFile; 35 private String goal; 36 private String settingsFile; 37 private String activateProfiles; 38 private long timeout = ScriptRunner.NO_TIMEOUT; 39 private String flags; 40 41 45 public void setSettingsFile(String settingsFile) { 46 47 this.settingsFile = settingsFile; 48 } 49 50 54 public void setActivateProfiles(String activateProfiles) { 55 56 this.activateProfiles = activateProfiles; 57 } 58 59 64 public void setMvnHome(String mvnHome) throws CruiseControlException { 65 66 if (!mvnHome.endsWith(File.separator)) { 67 mvnHome = mvnHome + File.separator; 68 } 69 this.mvnHome = mvnHome; 70 71 LOG.debug("MvnHome = " + this.mvnHome + " Mvn should be in " + this.mvnHome + MVN); 72 } 73 74 78 public void setMvnScript(final String mvnScipt) { 79 this.mvnScript = mvnScipt; 80 } 81 82 86 public void setPomFile(String pomFile) { 87 88 this.pomFile = pomFile; 89 90 LOG.debug("pom file : " + this.pomFile); 91 } 92 93 public void setGoal(String goal) { 94 95 this.goal = goal; 96 } 97 98 public void setTimeout(long timeout) { 99 100 this.timeout = timeout; 101 } 102 103 106 public void validate() throws CruiseControlException { 107 108 super.validate(); 109 110 if (mvnScript != null) { 111 ValidationHelper.assertTrue(new File (mvnScript).exists(), 112 "Maven Script file could not be found : " + mvnScript 113 + " Check the mvnscript attribute of the maven2 plugin"); 114 } else { 115 ValidationHelper.assertIsSet(mvnHome, "mvnhome", getClass()); 116 ValidationHelper.assertTrue(new File (mvnHome + MVN).exists(), 117 "mvn could not be found : " + mvnHome + MVN 118 + " Check the mvnhome attribute of the maven2 plugin"); 119 } 120 121 ValidationHelper.assertIsSet(pomFile, "pomfile", getClass()); 122 ValidationHelper.assertIsSet(goal, "goal", this.getClass()); 123 if (getGoalSets().isEmpty()) { 124 ValidationHelper.assertIsSet(null, "goal", this.getClass()); 125 } 126 127 if (settingsFile != null) { 128 ValidationHelper.assertTrue(new File (settingsFile).exists(), 129 "The settings file could not be found : " + settingsFile); 130 } 131 } 132 133 137 public Element build(Map buildProperties) throws CruiseControlException { 138 139 ValidationHelper.assertTrue(new File (pomFile).exists(), 142 "the pom file could not be found : " + pomFile + " Check the pomfile attribute"); 143 144 File workingDir = (new File (pomFile)).getParentFile(); 145 LOG.debug("Working dir is : " + workingDir.toString()); 146 147 long startTime = System.currentTimeMillis(); 148 149 Element buildLogElement = new Element("build"); 150 151 List goalSets = getGoalSets(); 152 for (int i = 0; i < goalSets.size(); i++) { 153 154 String goals = (String ) goalSets.get(i); 155 156 final String mvnScriptFile; 157 if (mvnScript != null) { 158 mvnScriptFile = mvnScript; 159 } else { 160 mvnScriptFile = mvnHome + MVN; 161 } 162 Maven2Script script = new Maven2Script(buildLogElement, mvnScriptFile, pomFile, goals, 163 settingsFile, activateProfiles, flags); 164 script.setBuildProperties(buildProperties); 165 166 ScriptRunner scriptRunner = new ScriptRunner(); 167 boolean scriptCompleted = scriptRunner.runScript(workingDir, script, timeout); 168 script.flushCurrentElement(); 169 170 if (!scriptCompleted) { 171 LOG.warn("Build timeout timer of " + timeout + " seconds has expired"); 172 buildLogElement = new Element("build"); 173 buildLogElement.setAttribute("error", "build timeout"); 174 } else if (script.getExitCode() != 0) { 175 synchronized (buildLogElement) { 178 buildLogElement.setAttribute("error", "Return code is " + script.getExitCode()); 179 } 180 } 181 182 if (buildLogElement.getAttribute("error") != null) { 183 break; 184 } 185 186 } 187 188 long endTime = System.currentTimeMillis(); 189 190 buildLogElement.setAttribute("time", DateUtil.getDurationAsString((endTime - startTime))); 191 return buildLogElement; 192 } 193 194 public Element buildWithTarget(Map properties, String target) throws CruiseControlException { 195 String origGoal = goal; 196 try { 197 goal = target; 198 return build(properties); 199 } finally { 200 goal = origGoal; 201 } 202 } 203 204 210 List getGoalSets() { 211 212 List list = new ArrayList (); 213 if (goal != null) { 214 StringTokenizer stok = new StringTokenizer (goal, "|"); 215 while (stok.hasMoreTokens()) { 216 String subSet = stok.nextToken().trim(); 217 if (subSet == null || subSet.length() == 0) { 218 continue; 219 } 220 list.add(subSet); 221 } 222 } 223 return list; 224 } 225 226 230 public void setFlags(String flags) { 231 232 this.flags = flags; 233 } 234 235 } 236 | Popular Tags |