You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by hb...@apache.org on 2016/10/22 20:17:03 UTC

svn commit: r1766230 - /maven/shared/trunk/maven-reporting-exec/src/main/java/org/apache/maven/reporting/exec/DefaultMavenReportExecutor.java

Author: hboutemy
Date: Sat Oct 22 20:17:03 2016
New Revision: 1766230

URL: http://svn.apache.org/viewvc?rev=1766230&view=rev
Log:
refactoring

Modified:
    maven/shared/trunk/maven-reporting-exec/src/main/java/org/apache/maven/reporting/exec/DefaultMavenReportExecutor.java

Modified: maven/shared/trunk/maven-reporting-exec/src/main/java/org/apache/maven/reporting/exec/DefaultMavenReportExecutor.java
URL: http://svn.apache.org/viewvc/maven/shared/trunk/maven-reporting-exec/src/main/java/org/apache/maven/reporting/exec/DefaultMavenReportExecutor.java?rev=1766230&r1=1766229&r2=1766230&view=diff
==============================================================================
--- maven/shared/trunk/maven-reporting-exec/src/main/java/org/apache/maven/reporting/exec/DefaultMavenReportExecutor.java (original)
+++ maven/shared/trunk/maven-reporting-exec/src/main/java/org/apache/maven/reporting/exec/DefaultMavenReportExecutor.java Sat Oct 22 20:17:03 2016
@@ -26,7 +26,6 @@ import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
 
-import org.apache.maven.execution.MavenSession;
 import org.apache.maven.lifecycle.LifecycleExecutor;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MavenPluginManager;
@@ -74,7 +73,7 @@ import org.codehaus.plexus.util.xml.Xpp3
  * Following steps are done:
  * <ul>
  * <li>get {@link PluginDescriptor} from the {@link MavenPluginManager} (through
- * {@link MavenPluginManagerHelper#getPluginDescriptor(Plugin, MavenSession)
+ * {@link MavenPluginManagerHelper#getPluginDescriptor(Plugin, org.apache.maven.execution.MavenSession)
  * MavenPluginManagerHelper.getPluginDescriptor(...)} to protect from core API change)</li>
  * <li>setup a {@link ClassLoader}, with the Site plugin classloader as parent for the report execution. <br>
  * Notice that some classes are imported from the current Site plugin ClassRealm: see {@link #IMPORTS}. Corresponding
@@ -169,159 +168,178 @@ public class DefaultMavenReportExecutor
         plugin.setGroupId( reportPlugin.getGroupId() );
         plugin.setArtifactId( reportPlugin.getArtifactId() );
         plugin.setVersion( resolvePluginVersion( reportPlugin, mavenReportExecutorRequest ) );
+        logger.info( "configuring report plugin " + plugin.getId() );
 
         mergePluginToReportPlugin( mavenReportExecutorRequest, plugin, reportPlugin );
 
-        logger.info( "configuring report plugin " + plugin.getId() );
+        PluginDescriptor pluginDescriptor =
+            mavenPluginManagerHelper.getPluginDescriptor( plugin, mavenReportExecutorRequest.getMavenSession() );
 
-        MavenSession session = mavenReportExecutorRequest.getMavenSession();
+        // step 2: prepare the goals
+        List<GoalWithConf> goalsWithConfiguration = new ArrayList<GoalWithConf>();
+        boolean hasUserDefinedReports = prepareGoals( reportPlugin, pluginDescriptor, goalsWithConfiguration );
 
-        PluginDescriptor pluginDescriptor = mavenPluginManagerHelper.getPluginDescriptor( plugin, session );
+        // step 3: prepare the reports
+        List<MavenReportExecution> reports = new ArrayList<MavenReportExecution>();
+        for ( GoalWithConf report : goalsWithConfiguration )
+        {
+            MavenReportExecution mavenReportExecution =
+                prepareReportExecution( mavenReportExecutorRequest, report, hasUserDefinedReports );
 
+            if ( mavenReportExecution != null )
+            {
+                // ok, report is ready to generate
+                reports.add( mavenReportExecution );
+            }
+        }
 
-        // step 2: prepare the goals
-        List<GoalWithConf> goalsWithConfiguration = new ArrayList<GoalWithConf>();
-        boolean userDefinedReports = true;
+        return reports;
+    }
 
+    private boolean prepareGoals( ReportPlugin reportPlugin, PluginDescriptor pluginDescriptor,
+                               List<GoalWithConf> goalsWithConfiguration )
+    {
         if ( reportPlugin.getReportSets().isEmpty() && reportPlugin.getReports().isEmpty() )
         {
             // by default, use every goal, which will be filtered later to only keep reporting goals
-            userDefinedReports = false;
             List<MojoDescriptor> mojoDescriptors = pluginDescriptor.getMojos();
             for ( MojoDescriptor mojoDescriptor : mojoDescriptors )
             {
-                goalsWithConfiguration.add( new GoalWithConf( mojoDescriptor.getGoal(),
+                goalsWithConfiguration.add( new GoalWithConf( reportPlugin, pluginDescriptor, mojoDescriptor.getGoal(),
                                                               mojoDescriptor.getConfiguration() ) );
             }
+
+            return false;
+        }
+
+        Set<String> goals = new HashSet<String>();
+        for ( String report : reportPlugin.getReports() )
+        {
+            if ( goals.add( report ) )
+            {
+                goalsWithConfiguration.add( new GoalWithConf( reportPlugin, pluginDescriptor, report,
+                                                              reportPlugin.getConfiguration() ) );
+            }
+            else
+            {
+                logger.warn( report + " report is declared twice in default reports" );
+            }
         }
-        else
+
+        for ( ReportSet reportSet : reportPlugin.getReportSets() )
         {
-            Set<String> goals = new HashSet<String>();
-            for ( String report : reportPlugin.getReports() )
+            goals = new HashSet<String>();
+            for ( String report : reportSet.getReports() )
             {
                 if ( goals.add( report ) )
                 {
-                    goalsWithConfiguration.add( new GoalWithConf( report, reportPlugin.getConfiguration() ) );
+                    goalsWithConfiguration.add( new GoalWithConf( reportPlugin, pluginDescriptor, report,
+                                                                  reportSet.getConfiguration() ) );
                 }
                 else
                 {
-                    logger.warn( report + " report is declared twice in default reports" );
-                }
-            }
-
-            for ( ReportSet reportSet : reportPlugin.getReportSets() )
-            {
-                goals = new HashSet<String>();
-                for ( String report : reportSet.getReports() )
-                {
-                    if ( goals.add( report ) )
-                    {
-                        goalsWithConfiguration.add( new GoalWithConf( report, reportSet.getConfiguration() ) );
-                    }
-                    else
-                    {
-                        logger.warn( report + " report is declared twice in " + reportSet.getId() + " reportSet" );
-                    }
+                    logger.warn( report + " report is declared twice in " + reportSet.getId() + " reportSet" );
                 }
             }
         }
 
+        return true;
+    }
 
-        // step 3: prepare the reports
-        List<MavenReportExecution> reports = new ArrayList<MavenReportExecution>();
-        for ( GoalWithConf report : goalsWithConfiguration )
+    private MavenReportExecution prepareReportExecution( MavenReportExecutorRequest mavenReportExecutorRequest,
+                                                         GoalWithConf report, boolean hasUserDefinedReports )
+        throws Exception
+    {
+        ReportPlugin reportPlugin = report.getReportPlugin();
+        PluginDescriptor pluginDescriptor = report.getPluginDescriptor();
+
+        MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( report.getGoal() );
+        if ( mojoDescriptor == null )
         {
-            MojoDescriptor mojoDescriptor = pluginDescriptor.getMojo( report.getGoal() );
-            if ( mojoDescriptor == null )
-            {
-                throw new MojoNotFoundException( report.getGoal(), pluginDescriptor );
-            }
+            throw new MojoNotFoundException( report.getGoal(), pluginDescriptor );
+        }
 
-            MavenProject project = mavenReportExecutorRequest.getProject();
-            if ( !userDefinedReports && mojoDescriptor.isAggregator() && !canAggregate( project ) )
-            {
-                // aggregator mojos automatically added from plugin are only run at execution root
-                continue;
-            }
+        MavenProject project = mavenReportExecutorRequest.getProject();
+        if ( !hasUserDefinedReports && mojoDescriptor.isAggregator() && !canAggregate( project ) )
+        {
+            // aggregator mojos automatically added from plugin are only run at execution root
+            return null;
+        }
 
-            MojoExecution mojoExecution = new MojoExecution( plugin, report.getGoal(), null );
+        MojoExecution mojoExecution = new MojoExecution( pluginDescriptor.getPlugin(), report.getGoal(), null );
 
-            mojoExecution.setMojoDescriptor( mojoDescriptor );
+        mojoExecution.setMojoDescriptor( mojoDescriptor );
 
-            mavenPluginManagerHelper.setupPluginRealm( pluginDescriptor, mavenReportExecutorRequest.getMavenSession(),
-                                                       Thread.currentThread().getContextClassLoader(), IMPORTS,
-                                                       EXCLUDES );
+        mavenPluginManagerHelper.setupPluginRealm( pluginDescriptor, mavenReportExecutorRequest.getMavenSession(),
+                                                   Thread.currentThread().getContextClassLoader(), IMPORTS,
+                                                   EXCLUDES );
 
-            if ( !isMavenReport( mojoExecution, pluginDescriptor ) )
+        if ( !isMavenReport( mojoExecution, pluginDescriptor ) )
+        {
+            if ( hasUserDefinedReports )
             {
-                if ( userDefinedReports )
-                {
-                    // reports were explicitly written in the POM
-                    logger.warn( "ignoring " + mojoExecution.getPlugin().getId() + ':' + report.getGoal()
-                        + " goal since it is not a report: should be removed from reporting configuration in POM" );
-                }
-                continue;
+                // reports were explicitly written in the POM
+                logger.warn( "ignoring " + mojoExecution.getPlugin().getId() + ':' + report.getGoal()
+                    + " goal since it is not a report: should be removed from reporting configuration in POM" );
             }
+            return null;
+        }
 
-            Xpp3Dom pluginMgmtConfiguration = null;
-            if ( project.getBuild() != null && project.getBuild().getPluginManagement() != null )
-            {
-                Plugin pluginMgmt = find( reportPlugin, project.getBuild().getPluginManagement().getPlugins() );
+        Xpp3Dom pluginMgmtConfiguration = null;
+        if ( project.getBuild() != null && project.getBuild().getPluginManagement() != null )
+        {
+            Plugin pluginMgmt = find( reportPlugin, project.getBuild().getPluginManagement().getPlugins() );
 
-                if ( pluginMgmt != null )
-                {
-                    pluginMgmtConfiguration = (Xpp3Dom) pluginMgmt.getConfiguration();
-                }
+            if ( pluginMgmt != null )
+            {
+                pluginMgmtConfiguration = (Xpp3Dom) pluginMgmt.getConfiguration();
             }
+        }
 
-            mojoExecution.setConfiguration( mergeConfiguration( mojoDescriptor.getMojoConfiguration(),
-                                                                pluginMgmtConfiguration,
-                                                                reportPlugin.getConfiguration(),
-                                                                report.getConfiguration(),
-                                                                mojoDescriptor.getParameterMap().keySet() ) );
+        mojoExecution.setConfiguration( mergeConfiguration( mojoDescriptor.getMojoConfiguration(),
+                                                            pluginMgmtConfiguration,
+                                                            reportPlugin.getConfiguration(),
+                                                            report.getConfiguration(),
+                                                            mojoDescriptor.getParameterMap().keySet() ) );
 
-            MavenReport mavenReport =
-                getConfiguredMavenReport( mojoExecution, pluginDescriptor, mavenReportExecutorRequest );
+        MavenReport mavenReport =
+            getConfiguredMavenReport( mojoExecution, pluginDescriptor, mavenReportExecutorRequest );
 
-            MavenReportExecution mavenReportExecution =
-                new MavenReportExecution( report.getGoal(), mojoExecution.getPlugin(), mavenReport,
-                                          pluginDescriptor.getClassRealm() );
+        MavenReportExecution mavenReportExecution =
+            new MavenReportExecution( report.getGoal(), mojoExecution.getPlugin(), mavenReport,
+                                      pluginDescriptor.getClassRealm() );
 
-            lifecycleExecutor.calculateForkedExecutions( mojoExecution,
-                                                         mavenReportExecutorRequest.getMavenSession() );
+        lifecycleExecutor.calculateForkedExecutions( mojoExecution,
+                                                     mavenReportExecutorRequest.getMavenSession() );
 
-            if ( !mojoExecution.getForkedExecutions().isEmpty() )
-            {
-                String reportDescription = pluginDescriptor.getArtifactId() + ":" + report.getGoal() + " report";
+        if ( !mojoExecution.getForkedExecutions().isEmpty() )
+        {
+            String reportDescription = pluginDescriptor.getArtifactId() + ":" + report.getGoal() + " report";
 
-                String execution;
-                if ( StringUtils.isNotEmpty( mojoDescriptor.getExecutePhase() ) )
-                {
-                    // forked phase
-                    execution = "'"
-                        + ( StringUtils.isEmpty( mojoDescriptor.getExecuteLifecycle() ) ? ""
-                                        : ( '[' + mojoDescriptor.getExecuteLifecycle() + ']' ) )
-                        + mojoDescriptor.getExecutePhase() + "' forked phase execution";
-                }
-                else
-                {
-                    // forked goal
-                    execution = "'" + mojoDescriptor.getExecuteGoal() + "' forked goal execution";
-                }
+            String execution;
+            if ( StringUtils.isNotEmpty( mojoDescriptor.getExecutePhase() ) )
+            {
+                // forked phase
+                execution = "'"
+                    + ( StringUtils.isEmpty( mojoDescriptor.getExecuteLifecycle() ) ? ""
+                                    : ( '[' + mojoDescriptor.getExecuteLifecycle() + ']' ) )
+                    + mojoDescriptor.getExecutePhase() + "' forked phase execution";
+            }
+            else
+            {
+                // forked goal
+                execution = "'" + mojoDescriptor.getExecuteGoal() + "' forked goal execution";
+            }
 
-                logger.info( "preparing " + reportDescription + " requires " + execution );
+            logger.info( "preparing " + reportDescription + " requires " + execution );
 
-                lifecycleExecutor.executeForkedExecutions( mojoExecution,
-                                                           mavenReportExecutorRequest.getMavenSession() );
+            lifecycleExecutor.executeForkedExecutions( mojoExecution,
+                                                       mavenReportExecutorRequest.getMavenSession() );
 
-                logger.info( execution + " for " + reportDescription + " preparation done" );
-            }
-
-            // ok, report is ready to generate
-            reports.add( mavenReportExecution );
+            logger.info( execution + " for " + reportDescription + " preparation done" );
         }
 
-        return reports;
+        return mavenReportExecution;
     }
 
     private boolean canAggregate( MavenProject project )
@@ -654,12 +672,29 @@ public class DefaultMavenReportExecutor
 
         private final PlexusConfiguration configuration;
 
-        public GoalWithConf( String goal, PlexusConfiguration configuration )
+        private final ReportPlugin reportPlugin;
+
+        private final PluginDescriptor pluginDescriptor;
+
+        public GoalWithConf( ReportPlugin reportPlugin, PluginDescriptor pluginDescriptor, String goal,
+                             PlexusConfiguration configuration )
         {
+            this.reportPlugin = reportPlugin;
+            this.pluginDescriptor = pluginDescriptor;
             this.goal = goal;
             this.configuration = configuration;
         }
 
+        public ReportPlugin getReportPlugin()
+        {
+            return reportPlugin;
+        }
+
+        public PluginDescriptor getPluginDescriptor()
+        {
+            return pluginDescriptor;
+        }
+
         public String getGoal()
         {
             return goal;