KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > builders > Maven2Script


1 package net.sourceforge.cruisecontrol.builders;
2
3 import java.io.File JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
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 /**
18  * Maven2 script class based on the Maven builder class from
19  * <a HREF="mailto:epugh@opensourceconnections.com">Eric Pugh</a>.
20  * <br />
21  * Contains all the details related to running a Maven based build.
22  * @author Steria Benelux Sa/Nv - Provided without any warranty
23  */

24 public class Maven2Script implements Script, StreamConsumer {
25
26     private static final String JavaDoc ERROR = "error";
27     private static final String JavaDoc SUCCESS = "success";
28     private static final Logger LOG = Logger.getLogger(Maven2Script.class);
29
30     private String JavaDoc goalset;
31     private String JavaDoc mvn;
32     private String JavaDoc pomFile;
33     private String JavaDoc settingsFile;
34     private String JavaDoc flags;
35     private Element buildLogElement; //Log to store result of the execution for CC
36
private Map JavaDoc buildProperties;
37     private String JavaDoc activateProfiles;
38
39     private int exitCode;
40     private Element currentElement;
41
42     /**
43      *
44      * @param mvn path to the mvn script
45      * @param pomFile path to the pom file
46      * @param goals the goalset to execute
47      * @param settingsFile path to the settings file (not required)
48      * @param activateProfiles comma-delimited list of profiles to activate. (not required)
49      * @param flags extra parameter to pass to mvn e.g.: -U (not required)
50      */

51     public Maven2Script(Element buildLogElement, String JavaDoc mvn, String JavaDoc pomFile, String JavaDoc goals,
52                         String JavaDoc settingsFile, String JavaDoc activateProfiles, String JavaDoc 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     /**
64      * Construct the command that we're going to execute.
65      * @return Commandline holding command to be executed
66      * @throws CruiseControlException
67      */

68     public Commandline buildCommandline() throws CruiseControlException {
69
70         //usage: maven [options] [<goal(s)>] [<phase(s)>]
71
Commandline cmdLine = new Commandline();
72         cmdLine.setExecutable(mvn);
73
74         //Run in non-interactive mode.
75
cmdLine.createArgument().setValue("-B");
76
77         //If log is enabled for CC, enable it for mvn
78
if (LOG.isDebugEnabled()) {
79             cmdLine.createArgument().setValue("-X");
80         }
81
82         //Alternate path for the user settings file
83
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 JavaDoc(pomFile).getName());
91         }
92
93         //activate specified profiles
94
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 JavaDoc stok = new StringTokenizer JavaDoc(goalset, " \t\r\n");
106             while (stok.hasMoreTokens()) {
107                 cmdLine.createArgument().setValue(stok.nextToken());
108             }
109         }
110
111         Iterator JavaDoc propertiesIterator = buildProperties.keySet().iterator();
112         while (propertiesIterator.hasNext()) {
113             String JavaDoc key = (String JavaDoc) propertiesIterator.next();
114             String JavaDoc value = (String JavaDoc) buildProperties.get(key);
115             // TODO doesn't work when properties contains spaces.
116
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 is enabled, log the command line
124
if (LOG.isDebugEnabled()) {
125             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
126             sb.append("Executing Command: ");
127             String JavaDoc[] args = cmdLine.getCommandline();
128             for (int i = 0; i < args.length; i++) {
129                 String JavaDoc arg = args[i];
130                 sb.append(arg);
131                 sb.append(" ");
132             }
133             LOG.debug(sb.toString());
134         }
135
136         return cmdLine;
137     }
138
139     /**
140      * Analyze the output of the mvn command. This is used to detect errors
141      * or succesfull build.
142      */

143     public void consumeLine(String JavaDoc line) {
144         String JavaDoc level = "";
145         String JavaDoc infoLine = null;
146         if (line == null || line.length() == 0 || buildLogElement == null) {
147             return;
148         }
149
150         synchronized (buildLogElement) {
151             //To detect errors, we will parse the output of the mvn command.
152
//Don't forget that this can stop working if changes are made in mvn.
153
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             /*} else if (line.startsWith("org.apache.maven.MavenException")) {
170              buildLogElement.setAttribute("error", "You have encountered an unknown error running Maven: " + line);
171              } else if (line.startsWith("The build cannot continue")) {
172              buildLogElement.setAttribute("error", "The build cannot continue: Unsatisfied Dependency");*/

173             } else if (infoLine.startsWith("[")
174                     && infoLine.endsWith("]")
175                     && infoLine.indexOf(":") > -1) { // heuristically this is a goal marker,
176
makeNewCurrentElement(infoLine.substring(1, infoLine.length() - 1));
177                 return; // Do not log the goal itself
178
}
179
180             Element msg = new Element("message");
181             msg.addContent(new CDATA(line));
182             // Initially call it "info" level.
183
// If "the going gets tough" we'll switch this to "error"
184
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 JavaDoc 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"); // Ok build successfull
215
} else if (buildLogElement.getAttribute(ERROR) != null) {
216                     // All the messages of the last (failed) goal should be
217
// switched to priority error
218
List JavaDoc lst = currentElement.getChildren("message");
219                     if (lst != null) {
220                         Iterator JavaDoc 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     /**
235      * @param buildProperties The buildProperties to set.
236      */

237     public void setBuildProperties(Map JavaDoc buildProperties) {
238         this.buildProperties = buildProperties;
239     }
240     /**
241      * @param goalset The goalset to set.
242      */

243     public void setGoalset(String JavaDoc goalset) {
244         this.goalset = goalset;
245     }
246     /**
247      * @param mvnScript The mavenScript to set.
248      */

249     public void setMvnScript(String JavaDoc mvnScript) {
250         this.mvn = mvnScript;
251     }
252     /**
253      * @param pomFile The projectFile to set.
254      */

255     public void setPomFile(String JavaDoc pomFile) {
256         this.pomFile = pomFile;
257     }
258     
259     /**
260      * @return the exitCode.
261      */

262     public int getExitCode() {
263
264         return exitCode;
265     }
266
267     /**
268      * @param exitCode The exitCode to set.
269      */

270     public void setExitCode(int exitCode) {
271
272         this.exitCode = exitCode;
273     }
274
275 }
276
Popular Tags