1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.maven.reporting.exec;
20
21 import javax.inject.Inject;
22 import javax.inject.Named;
23 import javax.inject.Singleton;
24
25 import java.util.ArrayList;
26 import java.util.Arrays;
27 import java.util.Collections;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Objects;
31 import java.util.Set;
32
33 import org.apache.maven.lifecycle.LifecycleExecutor;
34 import org.apache.maven.model.Build;
35 import org.apache.maven.model.Plugin;
36 import org.apache.maven.plugin.MavenPluginManager;
37 import org.apache.maven.plugin.Mojo;
38 import org.apache.maven.plugin.MojoExecution;
39 import org.apache.maven.plugin.MojoExecutionException;
40 import org.apache.maven.plugin.MojoNotFoundException;
41 import org.apache.maven.plugin.PluginConfigurationException;
42 import org.apache.maven.plugin.PluginContainerException;
43 import org.apache.maven.plugin.descriptor.MojoDescriptor;
44 import org.apache.maven.plugin.descriptor.PluginDescriptor;
45 import org.apache.maven.plugin.version.DefaultPluginVersionRequest;
46 import org.apache.maven.plugin.version.PluginVersionRequest;
47 import org.apache.maven.plugin.version.PluginVersionResolutionException;
48 import org.apache.maven.plugin.version.PluginVersionResolver;
49 import org.apache.maven.plugin.version.PluginVersionResult;
50 import org.apache.maven.project.MavenProject;
51 import org.apache.maven.reporting.MavenReport;
52 import org.codehaus.plexus.configuration.PlexusConfiguration;
53 import org.codehaus.plexus.util.StringUtils;
54 import org.codehaus.plexus.util.xml.Xpp3Dom;
55 import org.codehaus.plexus.util.xml.Xpp3DomUtils;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 import static java.util.Objects.requireNonNull;
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101 @Singleton
102 @Named
103 public class DefaultMavenReportExecutor implements MavenReportExecutor {
104 private static final Logger LOGGER = LoggerFactory.getLogger(DefaultMavenReportExecutor.class);
105
106 private final MavenPluginManager mavenPluginManager;
107
108 private final MavenPluginManagerHelper mavenPluginManagerHelper;
109
110 private final LifecycleExecutor lifecycleExecutor;
111
112 private final PluginVersionResolver pluginVersionResolver;
113
114 private static final List<String> IMPORTS = Arrays.asList(
115 "org.apache.maven.reporting.MavenReport",
116 "org.apache.maven.reporting.MavenMultiPageReport",
117
118 "org.apache.maven.doxia.siterenderer.Renderer",
119 "org.apache.maven.doxia.siterenderer.SiteRenderer",
120 "org.apache.maven.doxia.sink.SinkFactory",
121
122 "org.codehaus.doxia.sink.Sink",
123 "org.apache.maven.doxia.sink.Sink",
124 "org.apache.maven.doxia.sink.SinkEventAttributes",
125
126 "org.apache.maven.doxia.logging.LogEnabled",
127
128 "org.apache.maven.doxia.logging.Log");
129
130 private static final List<String> EXCLUDES =
131 Arrays.asList("doxia-site-renderer", "doxia-sink-api", "maven-reporting-api");
132
133 @Inject
134 public DefaultMavenReportExecutor(
135 MavenPluginManager mavenPluginManager,
136 MavenPluginManagerHelper mavenPluginManagerHelper,
137 LifecycleExecutor lifecycleExecutor,
138 PluginVersionResolver pluginVersionResolver) {
139 this.mavenPluginManager = requireNonNull(mavenPluginManager);
140 this.mavenPluginManagerHelper = requireNonNull(mavenPluginManagerHelper);
141 this.lifecycleExecutor = requireNonNull(lifecycleExecutor);
142 this.pluginVersionResolver = requireNonNull(pluginVersionResolver);
143 }
144
145 @Override
146 public List<MavenReportExecution> buildMavenReports(MavenReportExecutorRequest mavenReportExecutorRequest)
147 throws MojoExecutionException {
148 if (mavenReportExecutorRequest.getReportPlugins() == null) {
149 return Collections.emptyList();
150 }
151
152 Set<String> reportPluginKeys = new HashSet<>();
153 List<MavenReportExecution> reportExecutions = new ArrayList<>();
154
155 String pluginKey = "";
156 try {
157 for (ReportPlugin reportPlugin : mavenReportExecutorRequest.getReportPlugins()) {
158 pluginKey = reportPlugin.getGroupId() + ':' + reportPlugin.getArtifactId();
159
160 if (!reportPluginKeys.add(pluginKey)) {
161 LOGGER.info("Plugin {} will be executed more than one time", pluginKey);
162 }
163
164 reportExecutions.addAll(buildReportPlugin(mavenReportExecutorRequest, reportPlugin));
165 }
166 } catch (Exception e) {
167 throw new MojoExecutionException("Failed to get report for " + pluginKey, e);
168 }
169
170 return reportExecutions;
171 }
172
173 protected List<MavenReportExecution> buildReportPlugin(
174 MavenReportExecutorRequest mavenReportExecutorRequest, ReportPlugin reportPlugin) throws Exception {
175
176 Plugin plugin = new Plugin();
177 plugin.setGroupId(reportPlugin.getGroupId());
178 plugin.setArtifactId(reportPlugin.getArtifactId());
179 plugin.setVersion(resolvePluginVersion(reportPlugin, mavenReportExecutorRequest));
180 LOGGER.info("Configuring report plugin {}:{}", plugin.getArtifactId(), plugin.getVersion());
181
182 mergePluginToReportPlugin(mavenReportExecutorRequest, plugin, reportPlugin);
183
184 PluginDescriptor pluginDescriptor =
185 mavenPluginManagerHelper.getPluginDescriptor(plugin, mavenReportExecutorRequest.getMavenSession());
186
187
188 List<GoalWithConf> goalsWithConfiguration = new ArrayList<>();
189 boolean hasUserDefinedReports = prepareGoals(reportPlugin, pluginDescriptor, goalsWithConfiguration);
190
191
192 List<MavenReportExecution> reports = new ArrayList<>(goalsWithConfiguration.size());
193 for (GoalWithConf report : goalsWithConfiguration) {
194 MavenReportExecution mavenReportExecution =
195 prepareReportExecution(mavenReportExecutorRequest, report, hasUserDefinedReports);
196
197 if (mavenReportExecution != null) {
198
199 reports.add(mavenReportExecution);
200 }
201 }
202
203 if (!reports.isEmpty()) {
204
205 StringBuilder buff = new StringBuilder();
206 for (MavenReportExecution mre : reports) {
207 if (buff.length() > 0) {
208 buff.append(", ");
209 }
210 buff.append(mre.getGoal());
211 }
212 LOGGER.info(
213 "{} {} report{} for {}:{}: {}",
214 (hasUserDefinedReports ? "Configured" : "Detected"),
215 reports.size(),
216 (reports.size() > 1 ? "s" : ""),
217 plugin.getArtifactId(),
218 plugin.getVersion(),
219 buff);
220 }
221
222 return reports;
223 }
224
225 private boolean prepareGoals(
226 ReportPlugin reportPlugin, PluginDescriptor pluginDescriptor, List<GoalWithConf> goalsWithConfiguration) {
227 if (reportPlugin.getReportSets().isEmpty() && reportPlugin.getReports().isEmpty()) {
228
229 List<MojoDescriptor> mojoDescriptors = pluginDescriptor.getMojos();
230 for (MojoDescriptor mojoDescriptor : mojoDescriptors) {
231 goalsWithConfiguration.add(new GoalWithConf(
232 reportPlugin, pluginDescriptor, mojoDescriptor.getGoal(), mojoDescriptor.getConfiguration()));
233 }
234
235 return false;
236 }
237
238 Set<String> goals = new HashSet<>();
239 for (String report : reportPlugin.getReports()) {
240 if (goals.add(report)) {
241 goalsWithConfiguration.add(
242 new GoalWithConf(reportPlugin, pluginDescriptor, report, reportPlugin.getConfiguration()));
243 } else {
244 LOGGER.warn("{} report is declared twice in default reports", report);
245 }
246 }
247
248 for (ReportSet reportSet : reportPlugin.getReportSets()) {
249 goals = new HashSet<>();
250 for (String report : reportSet.getReports()) {
251 if (goals.add(report)) {
252 goalsWithConfiguration.add(
253 new GoalWithConf(reportPlugin, pluginDescriptor, report, reportSet.getConfiguration()));
254 } else {
255 LOGGER.warn("{} report is declared twice in {} reportSet", report, reportSet.getId());
256 }
257 }
258 }
259
260 return true;
261 }
262
263 private MavenReportExecution prepareReportExecution(
264 MavenReportExecutorRequest mavenReportExecutorRequest, GoalWithConf report, boolean hasUserDefinedReports)
265 throws Exception {
266 ReportPlugin reportPlugin = report.getReportPlugin();
267 PluginDescriptor pluginDescriptor = report.getPluginDescriptor();
268
269 MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(report.getGoal());
270 if (mojoDescriptor == null) {
271 throw new MojoNotFoundException(report.getGoal(), pluginDescriptor);
272 }
273
274 MavenProject project = mavenReportExecutorRequest.getProject();
275 if (!hasUserDefinedReports && mojoDescriptor.isAggregator() && !canAggregate(project)) {
276
277 return null;
278 }
279
280 MojoExecution mojoExecution = new MojoExecution(
281 pluginDescriptor.getPlugin(), report.getGoal(), mavenReportExecutorRequest.getExecutionId());
282
283 mojoExecution.setMojoDescriptor(mojoDescriptor);
284
285 mavenPluginManagerHelper.setupPluginRealm(
286 pluginDescriptor,
287 mavenReportExecutorRequest.getMavenSession(),
288 Thread.currentThread().getContextClassLoader(),
289 IMPORTS,
290 EXCLUDES);
291
292 if (!isMavenReport(mojoExecution, pluginDescriptor)) {
293 if (hasUserDefinedReports) {
294
295 LOGGER.warn(
296 "Ignoring {}:{}"
297 + " goal since it is not a report: should be removed from reporting configuration in POM",
298 mojoExecution.getPlugin().getId(),
299 report.getGoal());
300 }
301 return null;
302 }
303
304 Xpp3Dom pluginMgmtConfiguration = null;
305 if (project.getBuild() != null && project.getBuild().getPluginManagement() != null) {
306 Plugin pluginMgmt =
307 find(reportPlugin, project.getBuild().getPluginManagement().getPlugins());
308
309 if (pluginMgmt != null) {
310 pluginMgmtConfiguration = (Xpp3Dom) pluginMgmt.getConfiguration();
311 }
312 }
313
314 mojoExecution.setConfiguration(mergeConfiguration(
315 mojoDescriptor.getMojoConfiguration(),
316 pluginMgmtConfiguration,
317 reportPlugin.getConfiguration(),
318 report.getConfiguration(),
319 mojoDescriptor.getParameterMap().keySet()));
320
321 MavenReport mavenReport = getConfiguredMavenReport(mojoExecution, pluginDescriptor, mavenReportExecutorRequest);
322
323 MavenReportExecution mavenReportExecution = new MavenReportExecution(
324 report.getGoal(), mojoExecution.getPlugin(), mavenReport, pluginDescriptor.getClassRealm());
325
326 lifecycleExecutor.calculateForkedExecutions(mojoExecution, mavenReportExecutorRequest.getMavenSession());
327
328 if (!mojoExecution.getForkedExecutions().isEmpty()) {
329 String reportDescription = pluginDescriptor.getArtifactId() + ":" + report.getGoal() + " report";
330
331 String execution;
332 if (StringUtils.isNotEmpty(mojoDescriptor.getExecutePhase())) {
333
334 execution = "'"
335 + (StringUtils.isEmpty(mojoDescriptor.getExecuteLifecycle())
336 ? ""
337 : ('[' + mojoDescriptor.getExecuteLifecycle() + ']'))
338 + mojoDescriptor.getExecutePhase() + "' forked phase execution";
339 } else {
340
341 execution = "'" + mojoDescriptor.getExecuteGoal() + "' forked goal execution";
342 }
343
344 LOGGER.info("Preparing {} requires {}", reportDescription, execution);
345
346 lifecycleExecutor.executeForkedExecutions(mojoExecution, mavenReportExecutorRequest.getMavenSession());
347
348 LOGGER.info("{} for {} preparation done", execution, reportDescription);
349 }
350
351 return mavenReportExecution;
352 }
353
354 private boolean canAggregate(MavenProject project) {
355 return project.isExecutionRoot()
356 && "pom".equals(project.getPackaging())
357 && (project.getModules() != null)
358 && !project.getModules().isEmpty();
359 }
360
361 private MavenReport getConfiguredMavenReport(
362 MojoExecution mojoExecution,
363 PluginDescriptor pluginDescriptor,
364 MavenReportExecutorRequest mavenReportExecutorRequest)
365 throws PluginContainerException, PluginConfigurationException {
366 try {
367 Mojo mojo = mavenPluginManager.getConfiguredMojo(
368 Mojo.class, mavenReportExecutorRequest.getMavenSession(), mojoExecution);
369
370 return (MavenReport) mojo;
371 } catch (ClassCastException e) {
372 if (LOGGER.isDebugEnabled()) {
373 LOGGER.warn("Skipping ClassCastException", e);
374 } else {
375 LOGGER.warn("Skipping ClassCastException");
376 }
377 return null;
378 }
379 }
380
381 private boolean isMavenReport(MojoExecution mojoExecution, PluginDescriptor pluginDescriptor) {
382 ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
383
384
385 Class<?> mojoClass;
386 try {
387 Thread.currentThread()
388 .setContextClassLoader(mojoExecution.getMojoDescriptor().getRealm());
389
390 mojoClass = pluginDescriptor
391 .getClassRealm()
392 .loadClass(mojoExecution.getMojoDescriptor().getImplementation());
393 } catch (ClassNotFoundException e) {
394 if (LOGGER.isDebugEnabled()) {
395 LOGGER.warn("Skipping ClassNotFoundException mojoExecution.goal {}", mojoExecution.getGoal(), e);
396 } else {
397 LOGGER.warn("Skipping ClassNotFoundException mojoExecution.goal {}", mojoExecution.getGoal());
398 }
399 return false;
400 } finally {
401 Thread.currentThread().setContextClassLoader(originalClassLoader);
402 }
403
404
405 try {
406 Thread.currentThread()
407 .setContextClassLoader(mojoExecution.getMojoDescriptor().getRealm());
408 MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(mojoExecution.getGoal());
409
410 boolean isMavenReport = MavenReport.class.isAssignableFrom(mojoClass);
411
412 if (LOGGER.isDebugEnabled()) {
413 if (mojoDescriptor != null && mojoDescriptor.getImplementationClass() != null) {
414 LOGGER.debug(
415 "Class {} is MavenReport: ",
416 mojoDescriptor.getImplementationClass().getName(),
417 isMavenReport);
418 }
419
420 if (!isMavenReport) {
421 LOGGER.debug(
422 "Skipping non MavenReport {}",
423 mojoExecution.getMojoDescriptor().getId());
424 }
425 }
426
427 return isMavenReport;
428 } catch (LinkageError e) {
429 if (LOGGER.isDebugEnabled()) {
430 LOGGER.warn("Skipping LinkageError mojoExecution.goal {}", mojoExecution.getGoal(), e);
431 } else {
432 LOGGER.warn("Skipping LinkageError mojoExecution.goal {}", mojoExecution.getGoal());
433 }
434 return false;
435 } finally {
436 Thread.currentThread().setContextClassLoader(originalClassLoader);
437 }
438 }
439
440
441
442
443
444
445
446
447
448
449
450
451 private Xpp3Dom mergeConfiguration(
452 PlexusConfiguration mojoConf,
453 Xpp3Dom pluginMgmtConfig,
454 PlexusConfiguration pluginConf,
455 PlexusConfiguration reportSetConf,
456 Set<String> parameters) {
457 Xpp3Dom mojoConfig = (mojoConf != null) ? convert(mojoConf) : new Xpp3Dom("configuration");
458
459 if (pluginMgmtConfig != null || pluginConf != null || reportSetConf != null) {
460 Xpp3Dom pluginConfig = (pluginConf == null) ? new Xpp3Dom("fake") : convert(pluginConf);
461
462
463 Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(convert(reportSetConf), pluginConfig);
464
465 mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, pluginMgmtConfig);
466
467 mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, mojoConfig);
468
469
470 Xpp3Dom cleanedConfig = new Xpp3Dom("configuration");
471 if (mergedConfig.getChildren() != null) {
472 for (Xpp3Dom parameter : mergedConfig.getChildren()) {
473 if (parameters.contains(parameter.getName())) {
474 cleanedConfig.addChild(parameter);
475 }
476 }
477 }
478
479 mojoConfig = cleanedConfig;
480 }
481
482 return mojoConfig;
483 }
484
485 private Xpp3Dom convert(PlexusConfiguration config) {
486 if (config == null) {
487 return null;
488 }
489
490 Xpp3Dom dom = new Xpp3Dom(config.getName());
491 dom.setValue(config.getValue(null));
492
493 for (String attrib : config.getAttributeNames()) {
494 dom.setAttribute(attrib, config.getAttribute(attrib, null));
495 }
496
497 for (int n = config.getChildCount(), i = 0; i < n; i++) {
498 dom.addChild(convert(config.getChild(i)));
499 }
500
501 return dom;
502 }
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520 protected String resolvePluginVersion(
521 ReportPlugin reportPlugin, MavenReportExecutorRequest mavenReportExecutorRequest)
522 throws PluginVersionResolutionException {
523 String reportPluginKey = reportPlugin.getGroupId() + ':' + reportPlugin.getArtifactId();
524 LOGGER.debug("Resolving version for {}", reportPluginKey);
525
526
527 if (reportPlugin.getVersion() != null) {
528 LOGGER.debug(
529 "Resolved {} version from the reporting.plugins section: {}",
530 reportPluginKey,
531 reportPlugin.getVersion());
532 return reportPlugin.getVersion();
533 }
534
535 MavenProject project = mavenReportExecutorRequest.getProject();
536
537
538 if (project.getBuild() != null) {
539 Plugin plugin = find(reportPlugin, project.getBuild().getPlugins());
540
541 if (plugin != null && plugin.getVersion() != null) {
542 LOGGER.debug(
543 "Resolved {} version from the build.plugins section: {}", reportPluginKey, plugin.getVersion());
544 return plugin.getVersion();
545 }
546 }
547
548
549 if (project.getBuild() != null && project.getBuild().getPluginManagement() != null) {
550 Plugin plugin =
551 find(reportPlugin, project.getBuild().getPluginManagement().getPlugins());
552
553 if (plugin != null && plugin.getVersion() != null) {
554 LOGGER.debug(
555 "Resolved {} version from the build.pluginManagement.plugins section: {}",
556 reportPluginKey,
557 plugin.getVersion());
558 return plugin.getVersion();
559 }
560 }
561
562 LOGGER.warn("Report plugin {} has an empty version.", reportPluginKey);
563 LOGGER.warn("");
564 LOGGER.warn("It is highly recommended to fix these problems"
565 + " because they threaten the stability of your build.");
566 LOGGER.warn("");
567 LOGGER.warn("For this reason, future Maven versions might no"
568 + " longer support building such malformed projects.");
569
570 Plugin plugin = new Plugin();
571 plugin.setGroupId(reportPlugin.getGroupId());
572 plugin.setArtifactId(reportPlugin.getArtifactId());
573
574 PluginVersionRequest pluginVersionRequest =
575 new DefaultPluginVersionRequest(plugin, mavenReportExecutorRequest.getMavenSession());
576
577 PluginVersionResult result = pluginVersionResolver.resolve(pluginVersionRequest);
578 LOGGER.debug("Resolved {} version from repository: {}", reportPluginKey, result.getVersion());
579 return result.getVersion();
580 }
581
582
583
584
585
586
587
588
589 private Plugin find(ReportPlugin reportPlugin, List<Plugin> plugins) {
590 if (plugins == null) {
591 return null;
592 }
593 for (Plugin plugin : plugins) {
594 if (Objects.equals(plugin.getArtifactId(), reportPlugin.getArtifactId())
595 && Objects.equals(plugin.getGroupId(), reportPlugin.getGroupId())) {
596 return plugin;
597 }
598 }
599 return null;
600 }
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617 private void mergePluginToReportPlugin(
618 MavenReportExecutorRequest mavenReportExecutorRequest, Plugin buildPlugin, ReportPlugin reportPlugin) {
619 Build build = mavenReportExecutorRequest.getProject().getBuild();
620 Plugin configuredPlugin = find(reportPlugin, build.getPlugins());
621 if (configuredPlugin == null && build.getPluginManagement() != null) {
622 configuredPlugin = find(reportPlugin, build.getPluginManagement().getPlugins());
623 }
624 if (configuredPlugin != null) {
625 if (!configuredPlugin.getDependencies().isEmpty()) {
626 buildPlugin.getDependencies().addAll(configuredPlugin.getDependencies());
627 }
628 }
629 }
630
631 private static class GoalWithConf {
632 private final String goal;
633
634 private final PlexusConfiguration configuration;
635
636 private final ReportPlugin reportPlugin;
637
638 private final PluginDescriptor pluginDescriptor;
639
640 GoalWithConf(
641 ReportPlugin reportPlugin,
642 PluginDescriptor pluginDescriptor,
643 String goal,
644 PlexusConfiguration configuration) {
645 this.reportPlugin = reportPlugin;
646 this.pluginDescriptor = pluginDescriptor;
647 this.goal = goal;
648 this.configuration = configuration;
649 }
650
651 public ReportPlugin getReportPlugin() {
652 return reportPlugin;
653 }
654
655 public PluginDescriptor getPluginDescriptor() {
656 return pluginDescriptor;
657 }
658
659 public String getGoal() {
660 return goal;
661 }
662
663 public PlexusConfiguration getConfiguration() {
664 return configuration;
665 }
666 }
667 }