KickJava   Java API By Example, From Geeks To Geeks.

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


1 package net.sourceforge.cruisecontrol.builders;
2
3 import java.io.File JavaDoc;
4 import java.util.ArrayList JavaDoc;
5 import java.util.List JavaDoc;
6 import java.util.Map JavaDoc;
7 import java.util.StringTokenizer JavaDoc;
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 /**
18  * Maven2 builder class based on the Maven builder class from
19  * <a HREF="mailto:fvancea@maxiq.com">Florin Vancea</a>.
20  * <br />
21  * Attempts to mimic the behavior of Ant builds, at least as far as CC is
22  * concerned. Basically it's a (heavily) edited version of AntBuilder. No style
23  * at all, but serves its purpose. :)
24  *
25  * @author Steria Benelux Sa/Nv - Provided without any warranty
26  */

27 public class Maven2Builder extends Builder {
28
29     private static final Logger LOG = Logger.getLogger(Maven2Builder.class);
30     private static final String JavaDoc MVN = "bin" + File.separator + "mvn";
31
32     private String JavaDoc mvnHome;
33     private String JavaDoc mvnScript;
34     private String JavaDoc pomFile;
35     private String JavaDoc goal;
36     private String JavaDoc settingsFile;
37     private String JavaDoc activateProfiles;
38     private long timeout = ScriptRunner.NO_TIMEOUT;
39     private String JavaDoc flags;
40
41     /**
42      * Set an Alternate path for the user settings file.
43      * @param settingsFile Alternate path for the user settings file.
44      */

45     public void setSettingsFile(String JavaDoc settingsFile) {
46
47         this.settingsFile = settingsFile;
48     }
49
50     /**
51      * Set the comma-delimited list of profiles to activate.
52      * @param activateProfiles comma-delimited list of profiles to activate.
53      */

54     public void setActivateProfiles(String JavaDoc activateProfiles) {
55
56         this.activateProfiles = activateProfiles;
57     }
58     
59     /**
60      * Set mvnHome. This will be used to find the mvn script which is mvnHome/bin/
61      * @param mvnHome the mvn home
62      * @throws CruiseControlException When the mvn script could not be found this excpetion is thrown.
63      */

64     public void setMvnHome(String JavaDoc 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     /**
75      * Full path to maven script, which overrides the default ".../bin/mvn".
76      * @param mvnScipt
77      */

78     public void setMvnScript(final String JavaDoc mvnScipt) {
79         this.mvnScript = mvnScipt;
80     }
81     
82     /**
83      * Set the pom file. This is also used to find the working directory.
84      * @param pomFile
85      */

86     public void setPomFile(String JavaDoc pomFile) {
87
88         this.pomFile = pomFile;
89
90         LOG.debug("pom file : " + this.pomFile);
91     }
92     
93     public void setGoal(String JavaDoc goal) {
94
95         this.goal = goal;
96     }
97
98     public void setTimeout(long timeout) {
99
100         this.timeout = timeout;
101     }
102
103     /**
104      * Check at the starting of CC if required attributes are set
105      */

106     public void validate() throws CruiseControlException {
107
108         super.validate();
109
110         if (mvnScript != null) {
111             ValidationHelper.assertTrue(new File JavaDoc(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 JavaDoc(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 JavaDoc(settingsFile).exists(),
129                     "The settings file could not be found : " + settingsFile);
130         }
131     }
132
133     /**
134      * build and return the results via xml. debug status can be determined
135      * from log4j category once we get all the logging in place.
136      */

137     public Element build(Map JavaDoc buildProperties) throws CruiseControlException {
138
139         //This check is done here because the pom can be downloaded after CC is started
140
// and before this plugin is run
141
ValidationHelper.assertTrue(new File JavaDoc(pomFile).exists(),
142                 "the pom file could not be found : " + pomFile + " Check the pomfile attribute");
143
144         File JavaDoc workingDir = (new File JavaDoc(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 JavaDoc goalSets = getGoalSets();
152         for (int i = 0; i < goalSets.size(); i++) {
153
154             String JavaDoc goals = (String JavaDoc) goalSets.get(i);
155
156             final String JavaDoc 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                 // The maven.bat actually never returns error,
176
// due to internal cleanup called after the execution itself...
177
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 JavaDoc properties, String JavaDoc target) throws CruiseControlException {
195         String JavaDoc origGoal = goal;
196         try {
197             goal = target;
198             return build(properties);
199         } finally {
200             goal = origGoal;
201         }
202     }
203     
204     /**
205      * Produces sets of goals, ready to be run each in a distinct call to Maven.
206      * Separation of sets in "goal" attribute is made with '|'.
207      *
208      * @return a List containing String elements
209      */

210     List JavaDoc getGoalSets() {
211
212         List JavaDoc list = new ArrayList JavaDoc();
213         if (goal != null) {
214             StringTokenizer JavaDoc stok = new StringTokenizer JavaDoc(goal, "|");
215             while (stok.hasMoreTokens()) {
216                 String JavaDoc 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     /**
227      * Set flags. E.g.: '-U -o'
228      * @param flags set the flags
229      */

230     public void setFlags(String JavaDoc flags) {
231     
232         this.flags = flags;
233     }
234
235 }
236
Popular Tags