You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by jv...@apache.org on 2015/01/16 02:44:51 UTC

maven git commit: MNG-5753: Allow plugin implementors to choose how they want the configuration created for a particular MojoExecution

Repository: maven
Updated Branches:
  refs/heads/master 32053c99d -> 36d491a64


MNG-5753: Allow plugin implementors to choose how they want the configuration created for a particular MojoExecution


Project: http://git-wip-us.apache.org/repos/asf/maven/repo
Commit: http://git-wip-us.apache.org/repos/asf/maven/commit/36d491a6
Tree: http://git-wip-us.apache.org/repos/asf/maven/tree/36d491a6
Diff: http://git-wip-us.apache.org/repos/asf/maven/diff/36d491a6

Branch: refs/heads/master
Commit: 36d491a64856e4ffcb16175753783b0ef991870b
Parents: 32053c9
Author: Jason van Zyl <ja...@tesla.io>
Authored: Fri Dec 19 16:19:23 2014 +0530
Committer: Jason van Zyl <ja...@tesla.io>
Committed: Thu Jan 15 15:16:59 2015 -0500

----------------------------------------------------------------------
 .../lifecycle/MojoExecutionConfigurator.java    |  43 ++++++
 ...DefaultLifecycleExecutionPlanCalculator.java | 137 +++++++------------
 .../DefaultMojoExecutionConfigurator.java       | 105 ++++++++++++++
 3 files changed, 197 insertions(+), 88 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/maven/blob/36d491a6/maven-core/src/main/java/org/apache/maven/lifecycle/MojoExecutionConfigurator.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/MojoExecutionConfigurator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/MojoExecutionConfigurator.java
new file mode 100644
index 0000000..9852df8
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/MojoExecutionConfigurator.java
@@ -0,0 +1,43 @@
+package org.apache.maven.lifecycle;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.project.MavenProject;
+
+/**
+ * A MojoExecutionConfigurator is responsible for creating the configuration for  Mojo  based on configuration for a Mojo in the MavenProject
+ * and the default configuration for the Mojo from the containing plugin's plugin.xml descriptor.
+ * 
+ * @provisional
+ * @author Jason van Zyl
+ */
+public interface MojoExecutionConfigurator
+{
+    /**
+     * Create the MojoExecution configuration based on configuration for a Mojo in the MavenProject and the
+     * default configuration for the Mojo from the containing plugin's plugin.xml descriptor.
+     * 
+     * @param project
+     * @param mojoExecution
+     * @param allowPluginLevelConfig
+     */
+    void configure( MavenProject project, MojoExecution mojoExecution, boolean allowPluginLevelConfig );
+}

http://git-wip-us.apache.org/repos/asf/maven/blob/36d491a6/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleExecutionPlanCalculator.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleExecutionPlanCalculator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleExecutionPlanCalculator.java
index c187c1d..a5db25f 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleExecutionPlanCalculator.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecycleExecutionPlanCalculator.java
@@ -35,9 +35,8 @@ import org.apache.maven.lifecycle.LifecycleMappingDelegate;
 import org.apache.maven.lifecycle.LifecycleNotFoundException;
 import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException;
 import org.apache.maven.lifecycle.MavenExecutionPlan;
+import org.apache.maven.lifecycle.MojoExecutionConfigurator;
 import org.apache.maven.lifecycle.internal.builder.BuilderCommon;
-import org.apache.maven.model.Plugin;
-import org.apache.maven.model.PluginExecution;
 import org.apache.maven.plugin.BuildPluginManager;
 import org.apache.maven.plugin.InvalidPluginDescriptorException;
 import org.apache.maven.plugin.MojoExecution;
@@ -60,6 +59,8 @@ import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 import org.codehaus.plexus.util.xml.pull.XmlPullParserException;
 
+import com.google.common.collect.ImmutableMap;
+
 /**
  * @since 3.0
  * @author Benjamin Bentmann
@@ -92,11 +93,15 @@ public class DefaultLifecycleExecutionPlanCalculator
     @Requirement
     private Map<String, LifecycleMappingDelegate> delegates;
 
+    @Requirement
+    private Map<String, MojoExecutionConfigurator> mojoExecutionConfigurators;
+
     @SuppressWarnings( { "UnusedDeclaration" } )
     public DefaultLifecycleExecutionPlanCalculator()
     {
     }
 
+    // Only used for testing
     public DefaultLifecycleExecutionPlanCalculator( BuildPluginManager pluginManager,
                                                     DefaultLifecycles defaultLifeCycles,
                                                     MojoDescriptorCreator mojoDescriptorCreator,
@@ -106,9 +111,13 @@ public class DefaultLifecycleExecutionPlanCalculator
         this.defaultLifeCycles = defaultLifeCycles;
         this.mojoDescriptorCreator = mojoDescriptorCreator;
         this.lifecyclePluginResolver = lifecyclePluginResolver;
+        this.mojoExecutionConfigurators =
+            ImmutableMap.of( "default", (MojoExecutionConfigurator) new DefaultMojoExecutionConfigurator() );
     }
 
-    public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks, boolean setup )
+    @Override
+    public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks,
+                                                      boolean setup )
         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
         NoPluginFoundForPrefixException, LifecycleNotFoundException, PluginVersionResolutionException
@@ -127,6 +136,7 @@ public class DefaultLifecycleExecutionPlanCalculator
         return new MavenExecutionPlan( planItem, defaultLifeCycles );
     }
 
+    @Override
     public MavenExecutionPlan calculateExecutionPlan( MavenSession session, MavenProject project, List<Object> tasks )
         throws PluginNotFoundException, PluginResolutionException, LifecyclePhaseNotFoundException,
         PluginDescriptorParsingException, MojoNotFoundException, InvalidPluginDescriptorException,
@@ -146,6 +156,7 @@ public class DefaultLifecycleExecutionPlanCalculator
         }
     }
 
+    @Override
     public void setupMojoExecution( MavenSession session, MavenProject project, MojoExecution mojoExecution )
         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
         MojoNotFoundException, InvalidPluginDescriptorException, NoPluginFoundForPrefixException,
@@ -157,22 +168,21 @@ public class DefaultLifecycleExecutionPlanCalculator
         {
             mojoDescriptor =
                 pluginManager.getMojoDescriptor( mojoExecution.getPlugin(), mojoExecution.getGoal(),
-                                                 project.getRemotePluginRepositories(),
-                                                 session.getRepositorySession() );
+                                                 project.getRemotePluginRepositories(), session.getRepositorySession() );
 
             mojoExecution.setMojoDescriptor( mojoDescriptor );
         }
 
-        populateMojoExecutionConfiguration( project, mojoExecution,
-                                            MojoExecution.Source.CLI.equals( mojoExecution.getSource() ) );
+        mojoExecutionConfigurator( mojoExecution ).configure( project,
+                                                              mojoExecution,
+                                                              MojoExecution.Source.CLI.equals( mojoExecution.getSource() ) );
 
         finalizeMojoConfiguration( mojoExecution );
 
         calculateForkedExecutions( mojoExecution, session, project, new HashSet<MojoDescriptor>() );
     }
 
-    public List<MojoExecution> calculateMojoExecutions( MavenSession session, MavenProject project,
-                                                         List<Object> tasks )
+    public List<MojoExecution> calculateMojoExecutions( MavenSession session, MavenProject project, List<Object> tasks )
         throws PluginNotFoundException, PluginResolutionException, PluginDescriptorParsingException,
         MojoNotFoundException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
         PluginVersionResolutionException, LifecyclePhaseNotFoundException
@@ -248,73 +258,6 @@ public class DefaultLifecycleExecutionPlanCalculator
         return delegate.calculateLifecycleMappings( session, project, lifecycle, lifecyclePhase );
     }
 
-    private void populateMojoExecutionConfiguration( MavenProject project, MojoExecution mojoExecution,
-                                                     boolean allowPluginLevelConfig )
-    {
-        String g = mojoExecution.getGroupId();
-
-        String a = mojoExecution.getArtifactId();
-
-        Plugin plugin = findPlugin( g, a, project.getBuildPlugins() );
-
-        if ( plugin == null && project.getPluginManagement() != null )
-        {
-            plugin = findPlugin( g, a, project.getPluginManagement().getPlugins() );
-        }
-
-        if ( plugin != null )
-        {
-            PluginExecution pluginExecution =
-                findPluginExecution( mojoExecution.getExecutionId(), plugin.getExecutions() );
-
-            Xpp3Dom pomConfiguration = null;
-
-            if ( pluginExecution != null )
-            {
-                pomConfiguration = (Xpp3Dom) pluginExecution.getConfiguration();
-            }
-            else if ( allowPluginLevelConfig )
-            {
-                pomConfiguration = (Xpp3Dom) plugin.getConfiguration();
-            }
-
-            Xpp3Dom mojoConfiguration = ( pomConfiguration != null ) ? new Xpp3Dom( pomConfiguration ) : null;
-
-            mojoConfiguration = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoConfiguration );
-
-            mojoExecution.setConfiguration( mojoConfiguration );
-        }
-    }
-
-    private Plugin findPlugin( String groupId, String artifactId, Collection<Plugin> plugins )
-    {
-        for ( Plugin plugin : plugins )
-        {
-            if ( artifactId.equals( plugin.getArtifactId() ) && groupId.equals( plugin.getGroupId() ) )
-            {
-                return plugin;
-            }
-        }
-
-        return null;
-    }
-
-    private PluginExecution findPluginExecution( String executionId, Collection<PluginExecution> executions )
-    {
-        if ( StringUtils.isNotEmpty( executionId ) )
-        {
-            for ( PluginExecution execution : executions )
-            {
-                if ( executionId.equals( execution.getId() ) )
-                {
-                    return execution;
-                }
-            }
-        }
-
-        return null;
-    }
-
     /**
      * Post-processes the effective configuration for the specified mojo execution. This step discards all parameters
      * from the configuration that are not applicable to the mojo and injects the default values for any missing
@@ -349,8 +292,7 @@ public class DefaultLifecycleExecutionPlanCalculator
 
                 Xpp3Dom parameterDefaults = defaultConfiguration.getChild( parameter.getName() );
 
-                parameterConfiguration =
-                    Xpp3Dom.mergeXpp3Dom( parameterConfiguration, parameterDefaults, Boolean.TRUE );
+                parameterConfiguration = Xpp3Dom.mergeXpp3Dom( parameterConfiguration, parameterDefaults, Boolean.TRUE );
 
                 if ( parameterConfiguration != null )
                 {
@@ -375,6 +317,7 @@ public class DefaultLifecycleExecutionPlanCalculator
         return MojoDescriptorCreator.convert( mojoDescriptor );
     }
 
+    @Override
     public void calculateForkedExecutions( MojoExecution mojoExecution, MavenSession session )
         throws MojoNotFoundException, PluginNotFoundException, PluginResolutionException,
         PluginDescriptorParsingException, NoPluginFoundForPrefixException, InvalidPluginDescriptorException,
@@ -420,8 +363,7 @@ public class DefaultLifecycleExecutionPlanCalculator
             }
             else
             {
-                forkedExecutions =
-                    calculateForkedGoal( mojoExecution, session, forkedProject, alreadyForkedExecutions );
+                forkedExecutions = calculateForkedGoal( mojoExecution, session, forkedProject, alreadyForkedExecutions );
             }
 
             mojoExecution.setForkedExecutions( BuilderCommon.getKey( forkedProject ), forkedExecutions );
@@ -441,8 +383,7 @@ public class DefaultLifecycleExecutionPlanCalculator
 
         String forkedPhase = mojoDescriptor.getExecutePhase();
 
-        Map<String, List<MojoExecution>> lifecycleMappings =
-            calculateLifecycleMappings( session, project, forkedPhase );
+        Map<String, List<MojoExecution>> lifecycleMappings = calculateLifecycleMappings( session, project, forkedPhase );
 
         for ( List<MojoExecution> forkedExecutions : lifecycleMappings.values() )
         {
@@ -458,7 +399,7 @@ public class DefaultLifecycleExecutionPlanCalculator
                     forkedExecution.setMojoDescriptor( forkedMojoDescriptor );
                 }
 
-                populateMojoExecutionConfiguration( project, forkedExecution, false );
+                mojoExecutionConfigurator( forkedExecution ).configure( project, forkedExecution, false );
             }
         }
 
@@ -553,7 +494,7 @@ public class DefaultLifecycleExecutionPlanCalculator
 
                         forkedExecution.setConfiguration( forkedConfiguration );
 
-                        populateMojoExecutionConfiguration( project, forkedExecution, true );
+                        mojoExecutionConfigurator( forkedExecution ).configure( project, forkedExecution, true );
 
                         forkedExecutions.add( forkedExecution );
                     }
@@ -575,10 +516,11 @@ public class DefaultLifecycleExecutionPlanCalculator
             }
         }
     }
+
     // org.apache.maven.plugins:maven-remote-resources-plugin:1.0:process
-    //TODO: take repo mans into account as one may be aggregating prefixes of many
-    //TODO: collect at the root of the repository, read the one at the root, and fetch remote if something is missing
-    //      or the user forces the issue
+    // TODO: take repo mans into account as one may be aggregating prefixes of many
+    // TODO: collect at the root of the repository, read the one at the root, and fetch remote if something is missing
+    // or the user forces the issue
 
     private List<MojoExecution> calculateForkedGoal( MojoExecution mojoExecution, MavenSession session,
                                                      MavenProject project,
@@ -606,7 +548,7 @@ public class DefaultLifecycleExecutionPlanCalculator
 
         MojoExecution forkedExecution = new MojoExecution( forkedMojoDescriptor, forkedGoal );
 
-        populateMojoExecutionConfiguration( project, forkedExecution, true );
+        mojoExecutionConfigurator( forkedExecution ).configure( project, forkedExecution, true );
 
         finalizeMojoConfiguration( forkedExecution );
 
@@ -615,5 +557,24 @@ public class DefaultLifecycleExecutionPlanCalculator
         return Collections.singletonList( forkedExecution );
     }
 
+    private MojoExecutionConfigurator mojoExecutionConfigurator( MojoExecution mojoExecution )
+    {
+        String configuratorId = mojoExecution.getMojoDescriptor().getComponentConfigurator();
+        if ( configuratorId == null )
+        {
+            configuratorId = "default";
+        }
+
+        MojoExecutionConfigurator mojoExecutionConfigurator = mojoExecutionConfigurators.get( configuratorId );
 
+        if ( mojoExecutionConfigurator == null )
+        {
+            //
+            // The plugin has a custom component configurator but does not have a custom mojo execution configurator
+            // so fall back to the default mojo execution configurator.
+            //
+            mojoExecutionConfigurator = mojoExecutionConfigurators.get( "default" );
+        }
+        return mojoExecutionConfigurator;
+    }
 }

http://git-wip-us.apache.org/repos/asf/maven/blob/36d491a6/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java
----------------------------------------------------------------------
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java
new file mode 100644
index 0000000..9947614
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultMojoExecutionConfigurator.java
@@ -0,0 +1,105 @@
+package org.apache.maven.lifecycle.internal;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import java.util.Collection;
+
+import org.apache.maven.lifecycle.MojoExecutionConfigurator;
+import org.apache.maven.model.Plugin;
+import org.apache.maven.model.PluginExecution;
+import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.project.MavenProject;
+import org.codehaus.plexus.component.annotations.Component;
+import org.codehaus.plexus.util.StringUtils;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+
+@Component( role = MojoExecutionConfigurator.class )
+public class DefaultMojoExecutionConfigurator
+    implements MojoExecutionConfigurator
+{
+
+    @Override
+    public void configure( MavenProject project, MojoExecution mojoExecution, boolean allowPluginLevelConfig )
+    {
+        String g = mojoExecution.getGroupId();
+
+        String a = mojoExecution.getArtifactId();
+
+        Plugin plugin = findPlugin( g, a, project.getBuildPlugins() );
+
+        if ( plugin == null && project.getPluginManagement() != null )
+        {
+            plugin = findPlugin( g, a, project.getPluginManagement().getPlugins() );
+        }
+
+        if ( plugin != null )
+        {
+            PluginExecution pluginExecution =
+                findPluginExecution( mojoExecution.getExecutionId(), plugin.getExecutions() );
+
+            Xpp3Dom pomConfiguration = null;
+
+            if ( pluginExecution != null )
+            {
+                pomConfiguration = (Xpp3Dom) pluginExecution.getConfiguration();
+            }
+            else if ( allowPluginLevelConfig )
+            {
+                pomConfiguration = (Xpp3Dom) plugin.getConfiguration();
+            }
+
+            Xpp3Dom mojoConfiguration = ( pomConfiguration != null ) ? new Xpp3Dom( pomConfiguration ) : null;
+
+            mojoConfiguration = Xpp3Dom.mergeXpp3Dom( mojoExecution.getConfiguration(), mojoConfiguration );
+
+            mojoExecution.setConfiguration( mojoConfiguration );
+        }
+    }
+
+    private Plugin findPlugin( String groupId, String artifactId, Collection<Plugin> plugins )
+    {
+        for ( Plugin plugin : plugins )
+        {
+            if ( artifactId.equals( plugin.getArtifactId() ) && groupId.equals( plugin.getGroupId() ) )
+            {
+                return plugin;
+            }
+        }
+
+        return null;
+    }
+
+    private PluginExecution findPluginExecution( String executionId, Collection<PluginExecution> executions )
+    {
+        if ( StringUtils.isNotEmpty( executionId ) )
+        {
+            for ( PluginExecution execution : executions )
+            {
+                if ( executionId.equals( execution.getId() ) )
+                {
+                    return execution;
+                }
+            }
+        }
+
+        return null;
+    }
+
+}