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.getId());
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 reports.size(),
215 (reports.size() > 1 ? "s" : ""),
216 (hasUserDefinedReports ? "configured" : "detected"),
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(pluginDescriptor.getPlugin(), report.getGoal(), null);
281
282 mojoExecution.setMojoDescriptor(mojoDescriptor);
283
284 mavenPluginManagerHelper.setupPluginRealm(
285 pluginDescriptor,
286 mavenReportExecutorRequest.getMavenSession(),
287 Thread.currentThread().getContextClassLoader(),
288 IMPORTS,
289 EXCLUDES);
290
291 if (!isMavenReport(mojoExecution, pluginDescriptor)) {
292 if (hasUserDefinedReports) {
293
294 LOGGER.warn(
295 "Ignoring {}:{}"
296 + " goal since it is not a report: should be removed from reporting configuration in POM",
297 mojoExecution.getPlugin().getId(),
298 report.getGoal());
299 }
300 return null;
301 }
302
303 Xpp3Dom pluginMgmtConfiguration = null;
304 if (project.getBuild() != null && project.getBuild().getPluginManagement() != null) {
305 Plugin pluginMgmt =
306 find(reportPlugin, project.getBuild().getPluginManagement().getPlugins());
307
308 if (pluginMgmt != null) {
309 pluginMgmtConfiguration = (Xpp3Dom) pluginMgmt.getConfiguration();
310 }
311 }
312
313 mojoExecution.setConfiguration(mergeConfiguration(
314 mojoDescriptor.getMojoConfiguration(),
315 pluginMgmtConfiguration,
316 reportPlugin.getConfiguration(),
317 report.getConfiguration(),
318 mojoDescriptor.getParameterMap().keySet()));
319
320 MavenReport mavenReport = getConfiguredMavenReport(mojoExecution, pluginDescriptor, mavenReportExecutorRequest);
321
322 MavenReportExecution mavenReportExecution = new MavenReportExecution(
323 report.getGoal(), mojoExecution.getPlugin(), mavenReport, pluginDescriptor.getClassRealm());
324
325 lifecycleExecutor.calculateForkedExecutions(mojoExecution, mavenReportExecutorRequest.getMavenSession());
326
327 if (!mojoExecution.getForkedExecutions().isEmpty()) {
328 String reportDescription = pluginDescriptor.getArtifactId() + ":" + report.getGoal() + " report";
329
330 String execution;
331 if (StringUtils.isNotEmpty(mojoDescriptor.getExecutePhase())) {
332
333 execution = "'"
334 + (StringUtils.isEmpty(mojoDescriptor.getExecuteLifecycle())
335 ? ""
336 : ('[' + mojoDescriptor.getExecuteLifecycle() + ']'))
337 + mojoDescriptor.getExecutePhase() + "' forked phase execution";
338 } else {
339
340 execution = "'" + mojoDescriptor.getExecuteGoal() + "' forked goal execution";
341 }
342
343 LOGGER.info("Preparing {} requires {}", reportDescription, execution);
344
345 lifecycleExecutor.executeForkedExecutions(mojoExecution, mavenReportExecutorRequest.getMavenSession());
346
347 LOGGER.info("{} for {} preparation done", execution, reportDescription);
348 }
349
350 return mavenReportExecution;
351 }
352
353 private boolean canAggregate(MavenProject project) {
354 return project.isExecutionRoot()
355 && "pom".equals(project.getPackaging())
356 && (project.getModules() != null)
357 && !project.getModules().isEmpty();
358 }
359
360 private MavenReport getConfiguredMavenReport(
361 MojoExecution mojoExecution,
362 PluginDescriptor pluginDescriptor,
363 MavenReportExecutorRequest mavenReportExecutorRequest)
364 throws PluginContainerException, PluginConfigurationException {
365 try {
366 Mojo mojo = mavenPluginManager.getConfiguredMojo(
367 Mojo.class, mavenReportExecutorRequest.getMavenSession(), mojoExecution);
368
369 return (MavenReport) mojo;
370 } catch (ClassCastException e) {
371 if (LOGGER.isDebugEnabled()) {
372 LOGGER.warn("Skipping ClassCastException", e);
373 } else {
374 LOGGER.warn("Skipping ClassCastException");
375 }
376 return null;
377 }
378 }
379
380 private boolean isMavenReport(MojoExecution mojoExecution, PluginDescriptor pluginDescriptor) {
381 ClassLoader originalClassLoader = Thread.currentThread().getContextClassLoader();
382
383
384 Class<?> mojoClass;
385 try {
386 Thread.currentThread()
387 .setContextClassLoader(mojoExecution.getMojoDescriptor().getRealm());
388
389 mojoClass = pluginDescriptor
390 .getClassRealm()
391 .loadClass(mojoExecution.getMojoDescriptor().getImplementation());
392 } catch (ClassNotFoundException e) {
393 if (LOGGER.isDebugEnabled()) {
394 LOGGER.warn("Skipping ClassNotFoundException mojoExecution.goal {}", mojoExecution.getGoal(), e);
395 } else {
396 LOGGER.warn("Skipping ClassNotFoundException mojoExecution.goal {}", mojoExecution.getGoal());
397 }
398 return false;
399 } finally {
400 Thread.currentThread().setContextClassLoader(originalClassLoader);
401 }
402
403
404 try {
405 Thread.currentThread()
406 .setContextClassLoader(mojoExecution.getMojoDescriptor().getRealm());
407 MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo(mojoExecution.getGoal());
408
409 boolean isMavenReport = MavenReport.class.isAssignableFrom(mojoClass);
410
411 if (LOGGER.isDebugEnabled()) {
412 if (mojoDescriptor != null && mojoDescriptor.getImplementationClass() != null) {
413 LOGGER.debug(
414 "Class {} is MavenReport: ",
415 mojoDescriptor.getImplementationClass().getName(),
416 isMavenReport);
417 }
418
419 if (!isMavenReport) {
420 LOGGER.debug(
421 "Skipping non MavenReport {}",
422 mojoExecution.getMojoDescriptor().getId());
423 }
424 }
425
426 return isMavenReport;
427 } catch (LinkageError e) {
428 if (LOGGER.isDebugEnabled()) {
429 LOGGER.warn("Skipping LinkageError mojoExecution.goal {}", mojoExecution.getGoal(), e);
430 } else {
431 LOGGER.warn("Skipping LinkageError mojoExecution.goal {}", mojoExecution.getGoal());
432 }
433 return false;
434 } finally {
435 Thread.currentThread().setContextClassLoader(originalClassLoader);
436 }
437 }
438
439
440
441
442
443
444
445
446
447
448
449
450 private Xpp3Dom mergeConfiguration(
451 PlexusConfiguration mojoConf,
452 Xpp3Dom pluginMgmtConfig,
453 PlexusConfiguration pluginConf,
454 PlexusConfiguration reportSetConf,
455 Set<String> parameters) {
456 Xpp3Dom mojoConfig = (mojoConf != null) ? convert(mojoConf) : new Xpp3Dom("configuration");
457
458 if (pluginMgmtConfig != null || pluginConf != null || reportSetConf != null) {
459 Xpp3Dom pluginConfig = (pluginConf == null) ? new Xpp3Dom("fake") : convert(pluginConf);
460
461
462 Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(convert(reportSetConf), pluginConfig);
463
464 mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, pluginMgmtConfig);
465
466 mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, mojoConfig);
467
468
469 Xpp3Dom cleanedConfig = new Xpp3Dom("configuration");
470 if (mergedConfig.getChildren() != null) {
471 for (Xpp3Dom parameter : mergedConfig.getChildren()) {
472 if (parameters.contains(parameter.getName())) {
473 cleanedConfig.addChild(parameter);
474 }
475 }
476 }
477
478 mojoConfig = cleanedConfig;
479 }
480
481 return mojoConfig;
482 }
483
484 private Xpp3Dom convert(PlexusConfiguration config) {
485 if (config == null) {
486 return null;
487 }
488
489 Xpp3Dom dom = new Xpp3Dom(config.getName());
490 dom.setValue(config.getValue(null));
491
492 for (String attrib : config.getAttributeNames()) {
493 dom.setAttribute(attrib, config.getAttribute(attrib, null));
494 }
495
496 for (int n = config.getChildCount(), i = 0; i < n; i++) {
497 dom.addChild(convert(config.getChild(i)));
498 }
499
500 return dom;
501 }
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519 protected String resolvePluginVersion(
520 ReportPlugin reportPlugin, MavenReportExecutorRequest mavenReportExecutorRequest)
521 throws PluginVersionResolutionException {
522 String reportPluginKey = reportPlugin.getGroupId() + ':' + reportPlugin.getArtifactId();
523 LOGGER.debug("Resolving version for {}", reportPluginKey);
524
525
526 if (reportPlugin.getVersion() != null) {
527 LOGGER.debug(
528 "Resolved {} version from the reporting.plugins section: {}",
529 reportPluginKey,
530 reportPlugin.getVersion());
531 return reportPlugin.getVersion();
532 }
533
534 MavenProject project = mavenReportExecutorRequest.getProject();
535
536
537 if (project.getBuild() != null) {
538 Plugin plugin = find(reportPlugin, project.getBuild().getPlugins());
539
540 if (plugin != null && plugin.getVersion() != null) {
541 LOGGER.debug(
542 "Resolved {} version from the build.plugins section: {}", reportPluginKey, plugin.getVersion());
543 return plugin.getVersion();
544 }
545 }
546
547
548 if (project.getBuild() != null && project.getBuild().getPluginManagement() != null) {
549 Plugin plugin =
550 find(reportPlugin, project.getBuild().getPluginManagement().getPlugins());
551
552 if (plugin != null && plugin.getVersion() != null) {
553 LOGGER.debug(
554 "Resolved {} version from the build.pluginManagement.plugins section: {}",
555 reportPluginKey,
556 plugin.getVersion());
557 return plugin.getVersion();
558 }
559 }
560
561 LOGGER.warn("Report plugin {} has an empty version.", reportPluginKey);
562 LOGGER.warn("");
563 LOGGER.warn("It is highly recommended to fix these problems"
564 + " because they threaten the stability of your build.");
565 LOGGER.warn("");
566 LOGGER.warn("For this reason, future Maven versions might no"
567 + " longer support building such malformed projects.");
568
569 Plugin plugin = new Plugin();
570 plugin.setGroupId(reportPlugin.getGroupId());
571 plugin.setArtifactId(reportPlugin.getArtifactId());
572
573 PluginVersionRequest pluginVersionRequest =
574 new DefaultPluginVersionRequest(plugin, mavenReportExecutorRequest.getMavenSession());
575
576 PluginVersionResult result = pluginVersionResolver.resolve(pluginVersionRequest);
577 LOGGER.debug("Resolved {} version from repository: {}", reportPluginKey, result.getVersion());
578 return result.getVersion();
579 }
580
581
582
583
584
585
586
587
588 private Plugin find(ReportPlugin reportPlugin, List<Plugin> plugins) {
589 if (plugins == null) {
590 return null;
591 }
592 for (Plugin plugin : plugins) {
593 if (Objects.equals(plugin.getArtifactId(), reportPlugin.getArtifactId())
594 && Objects.equals(plugin.getGroupId(), reportPlugin.getGroupId())) {
595 return plugin;
596 }
597 }
598 return null;
599 }
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616 private void mergePluginToReportPlugin(
617 MavenReportExecutorRequest mavenReportExecutorRequest, Plugin buildPlugin, ReportPlugin reportPlugin) {
618 Build build = mavenReportExecutorRequest.getProject().getBuild();
619 Plugin configuredPlugin = find(reportPlugin, build.getPlugins());
620 if (configuredPlugin == null && build.getPluginManagement() != null) {
621 configuredPlugin = find(reportPlugin, build.getPluginManagement().getPlugins());
622 }
623 if (configuredPlugin != null) {
624 if (!configuredPlugin.getDependencies().isEmpty()) {
625 buildPlugin.getDependencies().addAll(configuredPlugin.getDependencies());
626 }
627 }
628 }
629
630 private static class GoalWithConf {
631 private final String goal;
632
633 private final PlexusConfiguration configuration;
634
635 private final ReportPlugin reportPlugin;
636
637 private final PluginDescriptor pluginDescriptor;
638
639 GoalWithConf(
640 ReportPlugin reportPlugin,
641 PluginDescriptor pluginDescriptor,
642 String goal,
643 PlexusConfiguration configuration) {
644 this.reportPlugin = reportPlugin;
645 this.pluginDescriptor = pluginDescriptor;
646 this.goal = goal;
647 this.configuration = configuration;
648 }
649
650 public ReportPlugin getReportPlugin() {
651 return reportPlugin;
652 }
653
654 public PluginDescriptor getPluginDescriptor() {
655 return pluginDescriptor;
656 }
657
658 public String getGoal() {
659 return goal;
660 }
661
662 public PlexusConfiguration getConfiguration() {
663 return configuration;
664 }
665 }
666 }