KickJava   Java API By Example, From Geeks To Geeks.

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


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.builders;
38
39 import java.io.File JavaDoc;
40 import java.util.Iterator JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
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 /**
55  * Maven script class.
56  *
57  * Contains all the details related to running a Maven based build.
58  * @author <a HREF="mailto:epugh@opensourceconnections.com">Eric Pugh</a>
59  */

60 public class MavenScript implements Script, StreamConsumer {
61     private static final Logger LOG = Logger.getLogger(MavenScript.class);
62     
63     private Map JavaDoc buildProperties;
64     private String JavaDoc goalset;
65     private String JavaDoc mavenScript;
66     private String JavaDoc projectFile;
67     private int exitCode;
68     /** Global log to produce, passed in from MavenBuilder */
69     private Element buildLogElement;
70     private Element currentElement = null;
71
72     /**
73      * construct the command that we're going to execute.
74      * @return Commandline holding command to be executed
75      * @throws CruiseControlException
76      */

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 JavaDoc propertiesIterator = buildProperties.keySet().iterator();
89         while (propertiesIterator.hasNext()) {
90             String JavaDoc key = (String JavaDoc) 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"); // no banner
99
if (projectFile != null) {
100             // we need only the name of the file
101
File JavaDoc pFile = new File JavaDoc(projectFile);
102             cmdLine.createArgument().setValue("-p");
103             cmdLine.createArgument().setValue(pFile.getName());
104         }
105         if (goalset != null) {
106             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(goalset, " \t\r\n");
107             while (stok.hasMoreTokens()) {
108                 cmdLine.createArgument().setValue(stok.nextToken());
109             }
110         }
111
112         if (LOG.isDebugEnabled()) {
113             StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
114             sb.append("Executing Command: ");
115             String JavaDoc[] args = cmdLine.getCommandline();
116             for (int i = 0; i < args.length; i++) {
117                 String JavaDoc arg = args[i];
118                 sb.append(arg);
119                 sb.append(" ");
120             }
121             LOG.debug(sb.toString());
122         }
123         return cmdLine;
124     }
125
126     /**
127      * Ugly parsing of Maven output into some Elements.
128      * Gets called from StreamPumper.
129      */

130     public synchronized void consumeLine(String JavaDoc line) {
131         if (line == null || line.length() == 0 || buildLogElement == null) {
132             return;
133         }
134
135         synchronized (buildLogElement) {
136             // The BAT never returns errors, so I'll catch it like this. Brrr.
137
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(":") // heuristically this is a goal marker,
145
&& !line.startsWith(" ") // but debug lines might look like that
146
&& !line.startsWith("\t")) {
147                 makeNewCurrentElement(line.substring(0, line.lastIndexOf(':')));
148                 return; // Do not log the goal itself
149
}
150             Element msg = new Element("message");
151             msg.addContent(new CDATA(line));
152             // Initially call it "info" level.
153
// If "the going gets tough" we'll switch this to "error"
154
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 JavaDoc 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                     // All the messages of the last (failed) goal should be
185
// switched to priority error
186
List JavaDoc lst = currentElement.getChildren("message");
187                     if (lst != null) {
188                         Iterator JavaDoc 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     /**
203      * @param buildProperties The buildProperties to set.
204      */

205     public void setBuildProperties(Map JavaDoc buildProperties) {
206         this.buildProperties = buildProperties;
207     }
208     /**
209      * @param goalset The goalset to set.
210      */

211     public void setGoalset(String JavaDoc goalset) {
212         this.goalset = goalset;
213     }
214     /**
215      * @param mavenScript The mavenScript to set.
216      */

217     public void setMavenScript(String JavaDoc mavenScript) {
218         this.mavenScript = mavenScript;
219     }
220     /**
221      * @param projectFile The projectFile to set.
222      */

223     public void setProjectFile(String JavaDoc projectFile) {
224         this.projectFile = projectFile;
225     }
226     /**
227      * @return Returns the exitCode.
228      */

229     public int getExitCode() {
230         return exitCode;
231     }
232     /**
233      * @param exitCode The exitCode to set.
234      */

235     public void setExitCode(int exitCode) {
236         this.exitCode = exitCode;
237     }
238     /**
239      * @param buildLogElement The buildLogElement to set.
240      */

241     public void setBuildLogElement(Element buildLogElement) {
242         this.buildLogElement = buildLogElement;
243     }
244 }
245
Popular Tags