1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package de.smartics.ci.maven.mojo;
17
18 import java.io.File;
19 import java.io.FileNotFoundException;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.net.MalformedURLException;
23 import java.net.URL;
24
25 import org.apache.commons.io.FileUtils;
26 import org.apache.maven.execution.MavenSession;
27 import org.apache.maven.model.CiManagement;
28 import org.apache.maven.plugin.MojoExecutionException;
29 import org.apache.maven.project.MavenProject;
30 import org.apache.maven.settings.Server;
31 import org.apache.maven.settings.Settings;
32 import org.codehaus.plexus.util.StringUtils;
33 import org.sonatype.plexus.components.sec.dispatcher.SecDispatcher;
34 import org.xml.sax.InputSource;
35
36 import de.smartics.ci.comm.LogHelper;
37 import de.smartics.ci.config.load.HudsonJobConfig;
38 import de.smartics.ci.config.load.LocationManager;
39 import de.smartics.ci.config.maven.MavenConfig;
40 import de.smartics.ci.config.maven.MavenPom;
41 import de.smartics.ci.config.maven.MavenSettings;
42 import de.smartics.ci.maven.CiServer;
43 import de.smartics.ci.maven.Logger;
44 import de.smartics.ci.maven.util.SettingsDecrypter;
45
46
47
48
49 public abstract class AbstractCiMojo extends AbstractLoggingMojo
50 {
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 protected MavenProject project;
68
69
70
71
72
73
74
75
76
77 protected Settings settings;
78
79
80
81
82
83
84
85
86 private SecDispatcher securityDispatcher;
87
88
89
90
91
92
93
94
95 private String settingsSecurityLocation;
96
97
98
99
100
101
102
103
104 protected MavenSession session;
105
106
107
108
109
110
111
112
113 protected String[] hudsonConfigSourceLocations;
114
115
116
117
118
119
120
121
122
123
124 protected String ciServerId;
125
126
127
128
129
130
131
132
133 protected File ciConfigurationFile;
134
135
136
137
138
139
140
141
142 protected File hudsonTargetLocation;
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161 public final MavenProject getProject()
162 {
163 return project;
164 }
165
166
167
168
169
170
171 public final void setProject(final MavenProject project)
172 {
173 this.project = project;
174 }
175
176
177
178
179
180
181 public final void setSession(final MavenSession session)
182 {
183 this.session = session;
184 }
185
186
187
188
189
190
191
192
193 protected final LocationManager createLocationManager()
194 {
195 final ClassLoader classLoader =
196 Thread.currentThread().getContextClassLoader();
197 final LocationManager locationManager = new LocationManager(classLoader);
198
199 provideDefaultLocation(locationManager);
200
201 if (hudsonConfigSourceLocations != null)
202 {
203 for (final String location : hudsonConfigSourceLocations)
204 {
205 final File dir = new File(location);
206 if (dir.exists())
207 {
208 locationManager.addLocation(dir);
209 }
210 else
211 {
212 final File relativeDir = new File(project.getBasedir(), location);
213 if (relativeDir.exists())
214 {
215 locationManager.addLocation(relativeDir);
216 }
217 else
218 {
219 try
220 {
221 final URL url = new URL(location);
222 locationManager.addLocation(url);
223 }
224 catch (final MalformedURLException e)
225 {
226 final Logger logger = getLog();
227 LogHelper.logWarn(logger, logLevel, "Cannot add location '"
228 + location + "'. Skipping.");
229 }
230 }
231 }
232 }
233 }
234
235 return locationManager;
236 }
237
238 private void provideDefaultLocation(final LocationManager locationManager)
239 {
240 final File defaultDir = new File(project.getBasedir(), "/src/etc/ci");
241 if (defaultDir.exists())
242 {
243 locationManager.addLocation(defaultDir);
244 }
245 }
246
247
248
249
250
251
252
253
254 protected final MavenConfig createMavenConfig()
255 {
256 final MavenPom pom = new MavenPom();
257 pom.setGroupId(project.getGroupId());
258 pom.setArtifactId(project.getArtifactId());
259 pom.setDescription(project.getDescription());
260 pom.setScmConnectionString(project.getScm().getDeveloperConnection());
261
262 final MavenSettings settings = new MavenSettings();
263
264 final MavenConfig mavenConfig = new MavenConfig(settings, pom);
265 return mavenConfig;
266 }
267
268
269
270
271
272
273
274
275
276
277 protected final InputSource createInputSource() throws MojoExecutionException
278 {
279 final File ciConfigXml = ciConfigurationFile;
280 try
281 {
282 final InputSource is;
283 if (checkCiConfigXml(ciConfigXml))
284 {
285 is = fetchLocal(ciConfigXml);
286 }
287 else
288 {
289 is = fetchClassloader();
290 }
291 return is;
292 }
293 catch (final IOException e)
294 {
295 throw new MojoExecutionException("Cannot open file 'ci-config.xml'.", e);
296 }
297
298 }
299
300 private boolean checkCiConfigXml(final File ciConfigXml)
301 {
302 return ciConfigXml != null && ciConfigXml.exists();
303 }
304
305 private InputSource fetchClassloader() throws FileNotFoundException
306 {
307 final InputSource is;
308 final ClassLoader classLoader =
309 Thread.currentThread().getContextClassLoader();
310 final LocationManager lm = new LocationManager(classLoader);
311 is = lm.open("ci-config.xml");
312 return is;
313 }
314
315 private InputSource fetchLocal(final File ciConfigXml) throws IOException
316 {
317 final InputSource is;
318 is = new InputSource(ciConfigXml.getAbsolutePath());
319 final InputStream in = FileUtils.openInputStream(ciConfigXml);
320 is.setByteStream(in);
321 return is;
322 }
323
324
325
326
327
328
329
330 protected final void writeHudsonConfigXml(
331 final HudsonJobConfig hudsonJobConfig) throws MojoExecutionException
332 {
333 ensureDirectoryExists(hudsonTargetLocation);
334
335 final File file =
336 new File(hudsonTargetLocation, hudsonJobConfig.getId() + ".xml");
337 try
338 {
339 FileUtils.writeStringToFile(file, hudsonJobConfig.getConfigXml());
340 }
341 catch (final IOException e)
342 {
343 throw new MojoExecutionException(
344 "Cannot write Hudson configuration file to '" + file.getPath() + "'",
345 e);
346 }
347 }
348
349 private static void ensureDirectoryExists(final File dir)
350 throws MojoExecutionException
351 {
352 if (!dir.exists())
353 {
354 if (!dir.mkdirs())
355 {
356 throw new MojoExecutionException("Cannot generate directory '"
357 + dir.getPath() + "'");
358 }
359 }
360 }
361
362
363
364
365
366
367
368
369 protected final CiServer getCiServer() throws MojoExecutionException
370 {
371 final CiManagement ciManagement = project.getCiManagement();
372 final Server server = getCiServerFromSettings(ciManagement);
373 final String ciUrl = ciManagement.getUrl();
374
375 final SettingsDecrypter settingsDecrypter =
376 new SettingsDecrypter(securityDispatcher, settingsSecurityLocation);
377
378 return new CiServer(logger, server, ciUrl, settingsDecrypter);
379 }
380
381 private Server getCiServerFromSettings(final CiManagement ciManagement)
382 throws MojoExecutionException
383 {
384 Server server;
385
386 if (StringUtils.isNotBlank(ciServerId))
387 {
388 server = settings.getServer(ciServerId);
389 }
390 else
391 {
392 final String ciUrl = ciManagement.getUrl();
393 server = settings.getServer(ciUrl);
394 if (server != null)
395 {
396 return server;
397 }
398
399 final String ciName = ciManagement.getSystem();
400 if (StringUtils.isNotBlank(ciName))
401 {
402 server = settings.getServer(ciName);
403 }
404 }
405
406 return server;
407 }
408
409
410
411
412
413
414
415
416
417
418
419 protected void checkPreconditions() throws MojoExecutionException
420 {
421
422 if (project == null)
423 {
424 throw new MojoExecutionException(
425 "No project information provided. Please run this plugin on"
426 + " a project that provides a POM.");
427 }
428
429 final CiManagement ciManagement = project.getCiManagement();
430 if (ciManagement == null)
431 {
432 throw new MojoExecutionException(
433 "No CI management specified. Please provide CI information"
434 + " within the 'ciManagement' block of your POM.");
435 }
436
437 final String ciUrl = ciManagement.getUrl();
438 if (StringUtils.isBlank(ciUrl))
439 {
440 throw new MojoExecutionException(
441 "No CI URL specified. Please provide CI information within the"
442 + " 'ciManagement/url' block of your POM.");
443 }
444 }
445
446 }