1 package net.sourceforge.basher.maven.plugins;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.io.File;
20 import java.util.*;
21
22 import net.sourceforge.basher.BasherContext;
23 import net.sourceforge.basher.booter.*;
24 import org.apache.maven.artifact.Artifact;
25 import org.apache.maven.artifact.factory.ArtifactFactory;
26 import org.apache.maven.artifact.metadata.ArtifactMetadataSource;
27 import org.apache.maven.artifact.repository.ArtifactRepository;
28 import org.apache.maven.artifact.resolver.*;
29 import org.apache.maven.artifact.resolver.filter.*;
30 import org.apache.maven.execution.MavenSession;
31 import org.apache.maven.plugin.*;
32 import org.apache.maven.project.MavenProject;
33 import org.apache.maven.surefire.booter.ForkConfiguration;
34 import org.apache.maven.toolchain.*;
35 import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
36 import org.codehaus.plexus.util.DirectoryScanner;
37
38
39
40
41
42
43
44
45
46
47
48 public class BasherMojo extends AbstractMojo
49 {
50
51
52
53
54
55 private File reportsDirectory;
56
57
58
59
60
61
62 private List remoteRepositories;
63
64
65
66
67
68 private ArtifactMetadataSource metadataSource;
69
70
71
72
73
74
75 private ArtifactResolver artifactResolver;
76
77
78
79
80
81
82
83
84 private Map pluginArtifactMap;
85
86
87
88
89
90
91 private ArtifactFactory artifactFactory;
92
93
94
95
96
97
98
99
100
101
102 private ArtifactRepository localRepository;
103
104
105
106
107
108
109
110
111 private File classesDirectory;
112
113
114
115
116
117
118
119 private File buildDirectory;
120
121
122
123
124
125
126
127
128
129 private List classpathElements;
130
131
132
133
134
135
136
137
138 private List additionalClasspathElements;
139
140
141
142
143
144
145
146
147 private List<String> includes;
148
149
150
151
152
153
154
155
156 private List<String> excludes;
157
158
159
160
161
162
163 private Properties systemProperties;
164
165 private Properties originalSystemProperties;
166
167
168
169
170
171
172
173 private List<BasherContext> basherContexts = new ArrayList<BasherContext>();
174
175
176
177
178
179
180
181
182
183
184
185
186
187 private String profiler;
188
189
190
191
192
193
194 private String activeBasherContext;
195
196
197
198
199
200
201
202
203 protected MavenProject project;
204
205
206
207
208
209
210 private String forkMode;
211
212
213
214
215
216
217
218 private String jvm;
219
220
221
222
223
224
225
226
227 private Boolean useSystemClassLoader;
228
229
230
231
232
233
234
235
236
237
238
239
240
241 private boolean useManifestOnlyJar;
242
243
244
245
246
247
248
249
250
251 private String debugForkedProcess;
252
253
254
255
256
257 private File workingDirectory;
258
259
260
261
262
263
264
265 private File basedir;
266
267
268
269
270
271
272 private String argLine;
273
274
275
276
277
278
279 private Map environmentVariables = new HashMap();
280
281
282
283
284
285
286
287 private boolean enableAssertions;
288
289
290
291
292
293
294
295
296 private MavenSession session;
297
298
299
300
301
302
303 private int processTimeOut = 0;
304
305 public void execute() throws MojoExecutionException
306 {
307 try
308 {
309 if (excludes == null)
310 {
311 excludes = new ArrayList<String>( );
312 }
313 if (excludes.isEmpty())
314 {
315 excludes.add( "**/*$*" );
316 }
317 if (includes == null)
318 {
319 includes= new ArrayList<String>( );
320 }
321 if (includes.isEmpty())
322 {
323 includes.add( "**/*Task.java" );
324 includes.add( "**/Task*.java" );
325 }
326
327
328
329 BasherBooter basherBooter = new BasherBooter();
330
331 Artifact basherBooterArtifact = (Artifact) pluginArtifactMap.get("net.sourceforge.basher:basher-booter");
332 if (basherBooterArtifact == null)
333 {
334 throw new MojoExecutionException("Unable to locate basher-booter in the list of plugin artifacts");
335 }
336 addArtifact(basherBooter, basherBooterArtifact);
337
338 if (!project.getBuild().getOutputDirectory().equals(classesDirectory.getAbsolutePath()))
339 {
340 classpathElements.remove(project.getBuild().getOutputDirectory());
341 classpathElements.add(classesDirectory.getAbsolutePath());
342 }
343 if (!project.getBuild().getTestOutputDirectory().equals(buildDirectory.getAbsolutePath()))
344 {
345 classpathElements.remove(project.getBuild().getTestOutputDirectory());
346 classpathElements.add(buildDirectory.getAbsolutePath());
347 }
348
349 getLog().debug( "Basher ClassPaths: " );
350
351 for (Iterator i = classpathElements.iterator(); i.hasNext();)
352 {
353 String classpathElement = (String) i.next();
354
355 getLog().debug(" " + classpathElement);
356
357 basherBooter.addClassPathUrl(classpathElement);
358 }
359
360
361 Toolchain tc = getToolchain();
362
363 if (tc != null)
364 {
365 getLog().info("Toolchain in basher-plugin: " + tc);
366 if (ForkConfiguration.FORK_NEVER.equals(forkMode))
367 {
368 forkMode = ForkConfiguration.FORK_ONCE;
369 }
370 if (jvm != null)
371 {
372 getLog().warn("Toolchains are ignored, 'executable' parameter is set to " + jvm);
373 }
374 else
375 {
376 jvm = tc.findTool("java");
377 }
378 }
379
380 if (additionalClasspathElements != null)
381 {
382 for (Iterator i = additionalClasspathElements.iterator(); i.hasNext();)
383 {
384 String classpathElement = (String) i.next();
385
386 getLog().debug(" " + classpathElement);
387
388 basherBooter.addClassPathUrl(classpathElement);
389 }
390 }
391
392
393
394
395
396 BasherForkConfiguration fork = new BasherForkConfiguration();
397
398 fork.setForkMode(forkMode);
399
400 processSystemProperties(!fork.isForking());
401
402 DirectoryScanner directoryScanner = new DirectoryScanner();
403 final List<String> list = project.getTestCompileSourceRoots();
404 for ( String compileSourceRoot : list )
405 {
406 directoryScanner.setBasedir( compileSourceRoot );
407 directoryScanner.setIncludes( includes.toArray(new String[includes.size()]) );
408 directoryScanner.setExcludes( excludes.toArray(new String[excludes.size()]) );
409 }
410
411 directoryScanner.scan();
412
413 String[] includedFiles = directoryScanner.getIncludedFiles();
414 basherBooter.setIncludedFiles(includedFiles);
415
416 if (fork.isForking())
417 {
418 getLog().info("Initiating forking run");
419 useSystemClassLoader = useSystemClassLoader == null ? Boolean.TRUE : useSystemClassLoader;
420 fork.setUseSystemClassLoader(useSystemClassLoader.booleanValue());
421 fork.setUseManifestOnlyJar(useManifestOnlyJar);
422
423 fork.setSystemProperties(systemProperties);
424
425 if ("true".equals(debugForkedProcess))
426 {
427 debugForkedProcess = "-Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005";
428 }
429
430 if ("yourkit".equals(profiler))
431 {
432
433 profiler = "-agentlib:yjpagent=port=10020,onlylocal,quiet,dir=" + reportsDirectory.getAbsolutePath();
434 }
435 else if ("none".equals( profiler ))
436 {
437 profiler = null;
438 }
439
440 if (profiler != null)
441 {
442 fork.setProfileLine( profiler );
443 }
444
445 fork.setDebugLine(debugForkedProcess);
446
447 if (jvm == null || "".equals(jvm))
448 {
449
450 jvm = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
451 getLog().debug("Using JVM: " + jvm);
452 }
453
454 fork.setJvmExecutable(jvm);
455
456 if (workingDirectory != null)
457 {
458 fork.setWorkingDirectory(workingDirectory);
459 }
460 else
461 {
462 fork.setWorkingDirectory(basedir);
463 }
464
465 fork.setArgLine(argLine);
466
467 fork.setEnvironmentVariables(environmentVariables);
468
469 if (getLog().isDebugEnabled())
470 {
471
472 fork.setDebug(true);
473 }
474
475 if (argLine != null)
476 {
477 List args = Arrays.asList(argLine.split(" "));
478 if (args.contains("-da") || args.contains("-disableassertions"))
479 {
480 enableAssertions = false;
481 }
482 }
483 }
484
485 basherBooter.setFailIfNoTasks(Boolean.FALSE);
486
487 basherBooter.setForkedProcessTimeoutInSeconds(processTimeOut);
488
489 basherBooter.setRedirectTasksOutputToFile(false);
490
491 basherBooter.setForkConfiguration(fork);
492
493 basherBooter.setEnableAssertions(enableAssertions);
494
495 basherBooter.setBasherContexts(basherContexts);
496
497 basherBooter.setActiveBasherContext(activeBasherContext);
498
499 basherBooter.setReportsDirectory(reportsDirectory);
500
501
502
503
504
505 int result = basherBooter.run();
506
507
508 }
509 catch (Exception e)
510 {
511 e.printStackTrace();
512 }
513
514 }
515
516 private Toolchain getToolchain()
517 {
518 Toolchain tc = null;
519 try
520 {
521 if (session != null)
522 {
523 ToolchainManager toolchainManager = (ToolchainManager) session.getContainer().lookup(ToolchainManager.ROLE);
524 if (toolchainManager != null)
525 {
526 tc = toolchainManager.getToolchainFromBuildContext("jdk", session);
527 }
528 }
529 }
530 catch (ComponentLookupException componentLookupException)
531 {
532
533 }
534 return tc;
535 }
536
537 protected void processSystemProperties(boolean setInSystem)
538 {
539 if (systemProperties == null)
540 {
541 systemProperties = new Properties();
542 }
543
544 originalSystemProperties = (Properties) System.getProperties().clone();
545
546
547
548
549
550
551
552 Properties userSpecifiedProperties = (Properties) session.getExecutionProperties().clone();
553 userSpecifiedProperties.putAll(systemProperties);
554
555
556 systemProperties.setProperty("basedir", basedir.getAbsolutePath());
557 systemProperties.setProperty("user.dir", workingDirectory.getAbsolutePath());
558
559 systemProperties.setProperty("localRepository", localRepository.getBasedir());
560
561 if (setInSystem)
562 {
563
564 Iterator iter = systemProperties.keySet().iterator();
565
566 while (iter.hasNext())
567 {
568 String key = (String) iter.next();
569
570 String value = systemProperties.getProperty(key);
571
572 System.setProperty(key, value);
573 }
574 }
575 }
576
577
578 private void addArtifact(final BasherBooter basherBooter, final Artifact basherArtifact)
579 throws ArtifactNotFoundException, ArtifactResolutionException
580 {
581 ArtifactResolutionResult result = resolveArtifact(null, basherArtifact);
582
583 for (Iterator i = result.getArtifacts().iterator(); i.hasNext();)
584 {
585 Artifact artifact = (Artifact) i.next();
586
587 getLog().debug("Adding to basher booter classpath: " + artifact.getFile().getAbsolutePath());
588
589 basherBooter.addBasherBootClassPathUrl(artifact.getFile().getAbsolutePath());
590 }
591 }
592
593 private ArtifactResolutionResult resolveArtifact(final Artifact filteredArtifact, Artifact providerArtifact)
594 throws ArtifactResolutionException, ArtifactNotFoundException
595 {
596 ArtifactFilter filter = null;
597 if (filteredArtifact != null)
598 {
599 filter =
600 new ExcludesArtifactFilter(Collections.singletonList(filteredArtifact.getGroupId() + ":" +
601 filteredArtifact.getArtifactId()));
602 }
603
604 Artifact originatingArtifact = artifactFactory.createBuildArtifact("dummy", "dummy", "1.0", "jar");
605
606 return artifactResolver.resolveTransitively(Collections.singleton(providerArtifact), originatingArtifact,
607 localRepository, remoteRepositories, metadataSource, filter);
608 }
609
610 }