You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2022/03/09 10:35:15 UTC

[maven] branch maven-3.9.x updated: [MNG-7391] add execution strategy+runner required by Maven Build Cache

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch maven-3.9.x
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/maven-3.9.x by this push:
     new 1954d51  [MNG-7391] add execution strategy+runner required by Maven Build Cache
1954d51 is described below

commit 1954d51ff2841045b4af8a515ad0719805269d8d
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Fri Nov 26 13:41:27 2021 +0100

    [MNG-7391] add execution strategy+runner required by Maven Build Cache
---
 .../apache/maven/lifecycle/DefaultLifecycles.java  |  5 ++-
 .../internal/DefaultLifecyclePluginAnalyzer.java   | 24 +----------
 .../maven/lifecycle/internal/MojoExecutor.java     | 25 ++++++++----
 .../plugin/DefaultMojosExecutionStrategy.java      | 46 ++++++++++++++++++++++
 .../apache/maven/plugin/MojoExecutionRunner.java   | 36 +++++++++++++++++
 .../maven/plugin/MojosExecutionStrategy.java       | 45 +++++++++++++++++++++
 .../plugin/internal/DefaultMavenPluginManager.java | 15 +++++++
 .../maven/lifecycle/DefaultLifecyclesTest.java     |  8 ++--
 .../lifecycle/internal/stub/MojoExecutorStub.java  | 13 +++---
 9 files changed, 175 insertions(+), 42 deletions(-)

diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java
index a7d9fac..1a13365 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/DefaultLifecycles.java
@@ -43,7 +43,7 @@ import java.util.Set;
 @Component( role = DefaultLifecycles.class )
 public class DefaultLifecycles
 {
-    public static final String[] STANDARD_LIFECYCLES = { "default", "clean", "site" };
+    public static final String[] STANDARD_LIFECYCLES = { "clean", "default", "site" };
 
     // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml")
 
@@ -108,6 +108,9 @@ public class DefaultLifecycles
         return phaseToLifecycleMap;
     }
 
+    /**
+     * Returns an ordered list of lifecycles
+     */
     public List<Lifecycle> getLifeCycles()
     {
         // ensure canonical order of standard lifecycles
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java
index 67a9057..8c99d67 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/DefaultLifecyclePluginAnalyzer.java
@@ -35,9 +35,6 @@ import org.codehaus.plexus.logging.Logger;
 import org.codehaus.plexus.util.StringUtils;
 import org.codehaus.plexus.util.xml.Xpp3Dom;
 
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Comparator;
 import java.util.HashSet;
 import java.util.LinkedHashMap;
 import java.util.List;
@@ -98,7 +95,7 @@ public class DefaultLifecyclePluginAnalyzer
 
         Map<Plugin, Plugin> plugins = new LinkedHashMap<>();
 
-        for ( Lifecycle lifecycle : getOrderedLifecycles() )
+        for ( Lifecycle lifecycle : defaultLifeCycles.getLifeCycles() )
         {
             org.apache.maven.lifecycle.mapping.Lifecycle lifecycleConfiguration =
                 lifecycleMappingForPackaging.getLifecycles().get( lifecycle.getId() );
@@ -131,25 +128,6 @@ public class DefaultLifecyclePluginAnalyzer
         return plugins.keySet();
     }
 
-    private List<Lifecycle> getOrderedLifecycles()
-    {
-        // NOTE: The lifecycle order can affect implied execution ids so we better be deterministic.
-
-        List<Lifecycle> lifecycles = new ArrayList<>( defaultLifeCycles.getLifeCycles() );
-
-        Collections.sort( lifecycles, new Comparator<Lifecycle>()
-        {
-
-            public int compare( Lifecycle l1, Lifecycle l2 )
-            {
-                return l1.getId().compareTo( l2.getId() );
-            }
-
-        } );
-
-        return lifecycles;
-    }
-
     private void parseLifecyclePhaseDefinitions( Map<Plugin, Plugin> plugins, String phase, LifecyclePhase goals )
     {
         String modelId = "org.apache.maven:maven-core:" + this.getClass().getPackage().getImplementationVersion()
diff --git a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
index cf97c8c..f81c713 100644
--- a/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
+++ b/maven-core/src/main/java/org/apache/maven/lifecycle/internal/MojoExecutor.java
@@ -30,6 +30,8 @@ import org.apache.maven.plugin.BuildPluginManager;
 import org.apache.maven.plugin.MavenPluginManager;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.MojoExecutionException;
+import org.apache.maven.plugin.MojoExecutionRunner;
+import org.apache.maven.plugin.MojosExecutionStrategy;
 import org.apache.maven.plugin.MojoFailureException;
 import org.apache.maven.plugin.PluginConfigurationException;
 import org.apache.maven.plugin.PluginIncompatibleException;
@@ -85,6 +87,9 @@ public class MojoExecutor
 
     private final ReadWriteLock aggregatorLock = new ReentrantReadWriteLock();
 
+    @Requirement
+    private MojosExecutionStrategy mojosExecutionStrategy;
+
     public MojoExecutor()
     {
     }
@@ -144,21 +149,27 @@ public class MojoExecutor
         return Collections.unmodifiableCollection( scopes );
     }
 
-    public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )
+    public void execute( final MavenSession session,
+                         final List<MojoExecution> mojoExecutions,
+                         final ProjectIndex projectIndex )
         throws LifecycleExecutionException
 
     {
-        DependencyContext dependencyContext = newDependencyContext( session, mojoExecutions );
+        final DependencyContext dependencyContext = newDependencyContext( session, mojoExecutions );
 
-        PhaseRecorder phaseRecorder = new PhaseRecorder( session.getCurrentProject() );
+        final PhaseRecorder phaseRecorder = new PhaseRecorder( session.getCurrentProject() );
 
-        for ( MojoExecution mojoExecution : mojoExecutions )
+        mojosExecutionStrategy.execute( mojoExecutions, session, new MojoExecutionRunner()
         {
-            execute( session, mojoExecution, projectIndex, dependencyContext, phaseRecorder );
-        }
+            @Override
+            public void run( MojoExecution mojoExecution ) throws LifecycleExecutionException
+            {
+                MojoExecutor.this.execute( session, mojoExecution, projectIndex, dependencyContext, phaseRecorder );
+            }
+        } );
     }
 
-    public void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex,
+    private void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex,
                          DependencyContext dependencyContext, PhaseRecorder phaseRecorder )
         throws LifecycleExecutionException
     {
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/DefaultMojosExecutionStrategy.java b/maven-core/src/main/java/org/apache/maven/plugin/DefaultMojosExecutionStrategy.java
new file mode 100644
index 0000000..9507c7a
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/plugin/DefaultMojosExecutionStrategy.java
@@ -0,0 +1,46 @@
+package org.apache.maven.plugin;
+
+/*
+ * 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.execution.MavenSession;
+import org.apache.maven.lifecycle.LifecycleExecutionException;
+
+import javax.inject.Named;
+import javax.inject.Singleton;
+import java.util.List;
+
+/**
+ * Default mojo execution strategy. It just iterates over mojo executions and runs one by one
+ */
+@Named
+@Singleton
+public class DefaultMojosExecutionStrategy implements MojosExecutionStrategy
+{
+    @Override
+    public void execute( List<MojoExecution> mojos, MavenSession session, MojoExecutionRunner mojoRunner )
+            throws LifecycleExecutionException
+    {
+        for ( MojoExecution mojoExecution : mojos )
+        {
+            mojoRunner.run( mojoExecution );
+        }
+
+    }
+}
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/MojoExecutionRunner.java b/maven-core/src/main/java/org/apache/maven/plugin/MojoExecutionRunner.java
new file mode 100644
index 0000000..314e041
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/plugin/MojoExecutionRunner.java
@@ -0,0 +1,36 @@
+package org.apache.maven.plugin;
+
+/*
+ * 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.lifecycle.LifecycleExecutionException;
+
+/**
+ * Provides context for mojo execution. Invocation of #run will result in actual execution
+ */
+public interface MojoExecutionRunner
+{
+    /**
+     * Runs mojo execution
+     *
+     * @param execution mojo execution
+     * @throws LifecycleExecutionException
+     */
+    void run( MojoExecution execution ) throws LifecycleExecutionException;
+}
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/MojosExecutionStrategy.java b/maven-core/src/main/java/org/apache/maven/plugin/MojosExecutionStrategy.java
new file mode 100644
index 0000000..e4babf6
--- /dev/null
+++ b/maven-core/src/main/java/org/apache/maven/plugin/MojosExecutionStrategy.java
@@ -0,0 +1,45 @@
+package org.apache.maven.plugin;
+
+/*
+ * 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.execution.MavenSession;
+import org.apache.maven.lifecycle.LifecycleExecutionException;
+
+import java.util.List;
+
+/**
+ * Interface allows overriding default mojo execution strategy For example it is possible wrap some mojo execution to
+ * decorate default functionality or skip some executions
+ */
+public interface MojosExecutionStrategy
+{
+
+    /**
+     * Entry point to the execution strategy
+     *
+     * @param mojos             list of mojos representing a project build
+     * @param session           current session
+     * @param mojoExecutionRunner mojo execution task which must be invoked by a strategy to actually run it
+     * @throws LifecycleExecutionException
+     */
+    void execute( List<MojoExecution> mojos, MavenSession session, MojoExecutionRunner mojoExecutionRunner )
+            throws LifecycleExecutionException;
+
+}
diff --git a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
index 8630471..546f004 100644
--- a/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
+++ b/maven-core/src/main/java/org/apache/maven/plugin/internal/DefaultMavenPluginManager.java
@@ -498,6 +498,21 @@ public class DefaultMavenPluginManager
 
         ClassRealm pluginRealm = pluginDescriptor.getClassRealm();
 
+        if ( pluginRealm == null )
+        {
+            try
+            {
+                setupPluginRealm( pluginDescriptor, session, null, null, null );
+            }
+            catch ( PluginResolutionException e )
+            {
+                String msg = "Cannot setup plugin realm [mojoDescriptor=" + mojoDescriptor.getId()
+                        + ", pluginDescriptor=" + pluginDescriptor.getId() + "]";
+                throw new PluginConfigurationException( pluginDescriptor, msg, e );
+            }
+            pluginRealm = pluginDescriptor.getClassRealm();
+        }
+
         if ( logger.isDebugEnabled() )
         {
             logger.debug( "Configuring mojo " + mojoDescriptor.getId() + " from plugin realm " + pluginRealm );
diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java
index 690532c..d7a84d1 100644
--- a/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java
+++ b/maven-core/src/test/java/org/apache/maven/lifecycle/DefaultLifecyclesTest.java
@@ -50,9 +50,11 @@ public class DefaultLifecyclesTest
     {
         final List<Lifecycle> cycles = defaultLifeCycles.getLifeCycles();
         assertNotNull( cycles );
-        final Lifecycle lifecycle = cycles.get( 0 );
-        assertEquals( "default", lifecycle.getId() );
-        assertEquals( 23, lifecycle.getPhases().size() );
+        final Lifecycle lifecycle0 = cycles.get( 0 );
+        assertEquals( "clean", lifecycle0.getId() );
+        final Lifecycle lifecycle1 = cycles.get( 1 );
+        assertEquals( "default", lifecycle1.getId() );
+        assertEquals( 23, lifecycle1.getPhases().size() );
 
     }
 
diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/MojoExecutorStub.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/MojoExecutorStub.java
index 763893e..350dccd 100644
--- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/MojoExecutorStub.java
+++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/MojoExecutorStub.java
@@ -17,14 +17,13 @@ package org.apache.maven.lifecycle.internal.stub;
 
 import org.apache.maven.execution.MavenSession;
 import org.apache.maven.lifecycle.LifecycleExecutionException;
-import org.apache.maven.lifecycle.internal.DependencyContext;
 import org.apache.maven.lifecycle.internal.MojoExecutor;
-import org.apache.maven.lifecycle.internal.PhaseRecorder;
 import org.apache.maven.lifecycle.internal.ProjectIndex;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MojoExecution;
 import org.apache.maven.plugin.descriptor.MojoDescriptor;
 import org.apache.maven.plugin.descriptor.PluginDescriptor;
+import org.apache.maven.project.MavenProject;
 
 import java.util.ArrayList;
 import java.util.Collections;
@@ -40,18 +39,16 @@ public class MojoExecutorStub
     public List<MojoExecution> executions = Collections.synchronizedList( new ArrayList<MojoExecution>() );
 
     @Override
-    public void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex,
-                         DependencyContext dependencyContext, PhaseRecorder phaseRecorder )
+    public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )
         throws LifecycleExecutionException
     {
-        executions.add( mojoExecution );
+        executions.addAll( mojoExecutions );
     }
 
     @Override
-    public void execute( MavenSession session, List<MojoExecution> mojoExecutions, ProjectIndex projectIndex )
-        throws LifecycleExecutionException
+    public List<MavenProject> executeForkedExecutions( MojoExecution mojoExecution, MavenSession session, ProjectIndex projectIndex ) throws LifecycleExecutionException
     {
-        executions.addAll(mojoExecutions);
+        return null;
     }