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.getArtifactId(), plugin.getVersion());
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                     (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             // 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(
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                 // reports were explicitly written in the POM
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                 // forked phase
334                 execution = "'"
335                         + (StringUtils.isEmpty(mojoDescriptor.getExecuteLifecycle())
336                                 ? ""
337                                 : ('[' + mojoDescriptor.getExecuteLifecycle() + ']'))
338                         + mojoDescriptor.getExecutePhase() + "' forked phase execution";
339             } else {
340                 // forked goal
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         // get the plugin's goal Mojo class
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         // check if it is a report
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      * Merge plugin configuration and reportset configuration to mojo configuration to get effective
442      * mojo configuration.
443      *
444      * @param mojoConf configuration done at mojo descriptor level
445      * @param pluginMgmtConfig configuration done at build.pluginManagement level
446      * @param pluginConf configuration done at reporting plugin level
447      * @param reportSetConf configuration done at reportSet level
448      * @param parameters set of supported parameters: any other parameter will be removed
449      * @return the effective configuration to be used
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             // merge pluginConf into reportSetConf
463             Xpp3Dom mergedConfig = Xpp3DomUtils.mergeXpp3Dom(convert(reportSetConf), pluginConfig);
464             // then merge pluginMgmtConfig
465             mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, pluginMgmtConfig);
466             // then merge mojoConf
467             mergedConfig = Xpp3DomUtils.mergeXpp3Dom(mergedConfig, mojoConfig);
468 
469             // clean result
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      * Resolve report plugin version. Steps to find a plugin version stop after each step if a non <code>null</code>
506      * value has been found:
507      * <ol>
508      * <li>use the one defined in the reportPlugin configuration,</li>
509      * <li>search similar (same groupId and artifactId) mojo in the build/plugins section of the pom,</li>
510      * <li>search similar (same groupId and artifactId) mojo in the build/pluginManagement section of the pom,</li>
511      * <li>ask {@link PluginVersionResolver} to get a fallback version and display a warning as it's not a recommended
512      * use.</li>
513      * </ol>
514      *
515      * @param reportPlugin the report plugin to resolve the version
516      * @param mavenReportExecutorRequest the current report execution context
517      * @return the report plugin version
518      * @throws PluginVersionResolutionException on plugin version resolution issue
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         // look for version defined in the reportPlugin configuration
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         // search in the build section
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         // search in pluginManagement section
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      * Search similar (same groupId and artifactId) plugin as a given report plugin.
584      *
585      * @param reportPlugin the report plugin to search for a similar plugin
586      * @param plugins the candidate plugins
587      * @return the first similar plugin
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      * TODO other stuff to merge ?
604      * <p>
605      * this method will "merge" some part of the plugin declaration existing in the build section to the fake plugin
606      * build for report execution:
607      * <ul>
608      * <li>dependencies</li>
609      * </ul>
610      * </p>
611      * The plugin could only be present in the dependency management section.
612      *
613      * @param mavenReportExecutorRequest
614      * @param buildPlugin
615      * @param reportPlugin
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 }