View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * <p>
63   * This component will build some {@link MavenReportExecution} from {@link MavenReportExecutorRequest}. If a
64   * {@link MavenReport} needs to fork a lifecycle, this fork is executed here. It will ask the core to get some
65   * informations in order to correctly setup {@link MavenReport}.
66   * </p>
67   * <p>
68   * <b>Note</b> if no version is defined in the report plugin, the version will be searched with
69   * {@link #resolvePluginVersion(ReportPlugin, MavenReportExecutorRequest) resolvePluginVersion(...)} method:
70   * </p>
71   * <ol>
72   * <li>use the one defined in the reportPlugin configuration,</li>
73   * <li>search similar (same groupId and artifactId) plugin in the build/plugins section of the pom,</li>
74   * <li>search similar (same groupId and artifactId) plugin in the build/pluginManagement section of the pom,</li>
75   * <li>ask {@link PluginVersionResolver} to get a fallback version (display a warning as it's not a recommended use).
76   * </li>
77   * </ol>
78   * <p>
79   * Following steps are done:
80   * </p>
81   * <ul>
82   * <li>get {@link PluginDescriptor} from the {@link MavenPluginManager} (through
83   * {@link MavenPluginManagerHelper#getPluginDescriptor(Plugin, org.apache.maven.execution.MavenSession)
84   * MavenPluginManagerHelper.getPluginDescriptor(...)} to protect from core API change)</li>
85   * <li>setup a {@link ClassLoader}, with the Site plugin classloader as parent for the report execution. <br>
86   * Notice that some classes are imported from the current Site plugin ClassRealm: see {@link #IMPORTS}. Corresponding
87   * artifacts are excluded from the artifact resolution: <code>doxia-site-renderer</code>, <code>doxia-sink-api</code>
88   *  and <code>maven-reporting-api</code>.<br>
89   * Work is done using {@link MavenPluginManager} (through
90   * {@link MavenPluginManagerHelper#setupPluginRealm(PluginDescriptor, MavenSession, ClassLoader, List, List)
91   * MavenPluginManagerHelper.setupPluginRealm(...)} to protect from core API change)</li>
92   * <li>setup the mojo using {@link MavenPluginManager#getConfiguredMojo(Class, MavenSession, MojoExecution)
93   * MavenPluginManager.getConfiguredMojo(...)}</li>
94   * <li>verify with {@link LifecycleExecutor#calculateForkedExecutions(MojoExecution, MavenSession)
95   * LifecycleExecutor.calculateForkedExecutions(...)} if any forked execution is needed: if yes, execute the forked
96   * execution here</li>
97   * </ul>
98   *
99   * @author Olivier Lamy
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             // TODO Will be removed after Doxia 2.0.0
118             "org.apache.maven.doxia.siterenderer.Renderer",
119             "org.apache.maven.doxia.siterenderer.SiteRenderer",
120             "org.apache.maven.doxia.sink.SinkFactory",
121             // TODO Will be removed after Doxia 2.0.0
122             "org.codehaus.doxia.sink.Sink",
123             "org.apache.maven.doxia.sink.Sink",
124             "org.apache.maven.doxia.sink.SinkEventAttributes",
125             // TODO Will be removed with Doxia 2.0.0
126             "org.apache.maven.doxia.logging.LogEnabled",
127             // TODO Will be removed with Doxia 2.0.0
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         // step 1: prepare the plugin
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         // step 2: prepare the goals
188         List<GoalWithConf> goalsWithConfiguration = new ArrayList<>();
189         boolean hasUserDefinedReports = prepareGoals(reportPlugin, pluginDescriptor, goalsWithConfiguration);
190 
191         // step 3: prepare the reports
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                 // ok, report is ready to generate
199                 reports.add(mavenReportExecution);
200             }
201         }
202 
203         if (!reports.isEmpty()) {
204             // log reports, either configured or detected
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             // by default, use every goal, which will be filtered later to only keep reporting goals
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             // aggregator mojos automatically added from plugin are only run at execution root
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                 // reports were explicitly written in the POM
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                 // forked phase
333                 execution = "'"
334                         + (StringUtils.isEmpty(mojoDescriptor.getExecuteLifecycle())
335                                 ? ""
336                                 : ('[' + mojoDescriptor.getExecuteLifecycle() + ']'))
337                         + mojoDescriptor.getExecutePhase() + "' forked phase execution";
338             } else {
339                 // forked goal
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         // get the plugin's goal Mojo class
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         // check if it is a report
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      * Merge plugin configuration and reportset configuration to mojo configuration to get effective
441      * mojo configuration.
442      *
443      * @param mojoConf configuration done at mojo descriptor level
444      * @param pluginMgmtConfig configuration done at build.pluginManagement level
445      * @param pluginConf configuration done at reporting plugin level
446      * @param reportSetConf configuration done at reportSet level
447      * @param parameters set of supported parameters: any other parameter will be removed
448      * @return the effective configuration to be used
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             // merge pluginConf into reportSetConf
462             Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(convert(reportSetConf), pluginConfig);
463             // then merge pluginMgmtConfig
464             mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, pluginMgmtConfig);
465             // then merge mojoConf
466             mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, mojoConfig);
467 
468             // clean result
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      * Resolve report plugin version. Steps to find a plugin version stop after each step if a non <code>null</code>
505      * value has been found:
506      * <ol>
507      * <li>use the one defined in the reportPlugin configuration,</li>
508      * <li>search similar (same groupId and artifactId) mojo in the build/plugins section of the pom,</li>
509      * <li>search similar (same groupId and artifactId) mojo in the build/pluginManagement section of the pom,</li>
510      * <li>ask {@link PluginVersionResolver} to get a fallback version and display a warning as it's not a recommended
511      * use.</li>
512      * </ol>
513      *
514      * @param reportPlugin the report plugin to resolve the version
515      * @param mavenReportExecutorRequest the current report execution context
516      * @return the report plugin version
517      * @throws PluginVersionResolutionException on plugin version resolution issue
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         // look for version defined in the reportPlugin configuration
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         // search in the build section
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         // search in pluginManagement section
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      * Search similar (same groupId and artifactId) plugin as a given report plugin.
583      *
584      * @param reportPlugin the report plugin to search for a similar plugin
585      * @param plugins the candidate plugins
586      * @return the first similar plugin
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      * TODO other stuff to merge ?
603      * <p>
604      * this method will "merge" some part of the plugin declaration existing in the build section to the fake plugin
605      * build for report execution:
606      * <ul>
607      * <li>dependencies</li>
608      * </ul>
609      * </p>
610      * The plugin could only be present in the dependency management section.
611      *
612      * @param mavenReportExecutorRequest
613      * @param buildPlugin
614      * @param reportPlugin
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 }