KickJava   Java API By Example, From Geeks To Geeks.

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


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.ArrayList JavaDoc;
41 import java.util.List JavaDoc;
42 import java.util.Map JavaDoc;
43 import java.util.StringTokenizer JavaDoc;
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 /**
54  * Maven builder class.
55  *
56  * Attempts to mimic the behavior of Ant builds, at least as far as CC is
57  * concerned. Basically it's a (heavily) edited version of AntBuilder.
58  * No style at all, but serves its purpose. :)
59  *
60  * @author <a HREF="mailto:fvancea@maxiq.com">Florin Vancea</a>
61  */

62 public class MavenBuilder extends Builder {
63
64     private static final Logger LOG = Logger.getLogger(MavenBuilder.class);
65
66     private String JavaDoc projectFile;
67     private String JavaDoc goal;
68     private String JavaDoc 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     /**
83      * build and return the results via xml. debug status can be determined
84      * from log4j category once we get all the logging in place.
85      */

86     public Element build(Map JavaDoc buildProperties) throws CruiseControlException {
87
88         File JavaDoc ckFile = new File JavaDoc(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 JavaDoc(projectFile);
94         ValidationHelper.assertTrue(ckFile.exists(),
95             "Project descriptor " + ckFile.getAbsolutePath() + " does not exist");
96
97         File JavaDoc workingDir = (new File JavaDoc(projectFile)).getParentFile();
98
99         long startTime = System.currentTimeMillis();
100
101         Element buildLogElement = new Element("build");
102
103         List JavaDoc runs = getGoalSets();
104         for (int runidx = 0; runidx < runs.size(); runidx++) {
105             String JavaDoc goalset = (String JavaDoc) 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                 // The maven.bat actually never returns error,
122
// due to internal cleanup called after the execution itself...
123
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 JavaDoc properties, String JavaDoc target) throws CruiseControlException {
141         String JavaDoc origGoal = goal;
142         try {
143             goal = target;
144             return build(properties);
145         } finally {
146             goal = origGoal;
147         }
148     }
149     
150     //***************************** Param setters ****************************
151

152     /**
153      * The path to the Maven script
154      * (i.e. the maven.bat file, because I tested this only on w2k)
155      */

156     public void setMavenScript(String JavaDoc mavenScript) {
157         this.mavenScript = mavenScript;
158     }
159
160     /**
161      * Maven goal to run. Supports sets of goals.
162      * Examples:
163      * "clean java:compile" will run 'clean' then 'java:compile' in one single invocation of Maven
164      * "clean jar:jar|site:generate" will run 'clean' and 'jar:jar' in one invocation,
165      * then 'site:generate' in _another_ invocation. Useful for updating from SCM
166      * with Maven goals, then doing the actual build with freshly loaded files.
167      * Notice the '|' as separator of sets.
168      */

169     public void setGoal(String JavaDoc goal) {
170         this.goal = goal;
171     }
172
173    /**
174      * project.xml to use
175      */

176     public void setProjectFile(String JavaDoc projectFile) {
177         this.projectFile = projectFile;
178     }
179
180     //******************** Command line generation **********************
181

182     /**
183      * Produces sets of goals, ready to be run each in a distinct call to Maven.
184      * Separation of sets in "goal" attribute is made with '|'.
185      *
186      * @return a List containing String elements
187      */

188     protected List JavaDoc getGoalSets() {
189         List JavaDoc al = new ArrayList JavaDoc();
190         if (goal != null) {
191             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(goal, "|");
192             while (stok.hasMoreTokens()) {
193                 String JavaDoc 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     /**
204      * Sets build timeout in seconds.
205      *
206      * @param timeout
207      * long build timeout
208      */

209     public void setTimeout(long timeout) {
210         this.timeout = timeout;
211     }
212 }
213
Popular Tags