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/02/13 08:57:24 UTC

[maven] 01/02: [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 mvn4
in repository https://gitbox.apache.org/repos/asf/maven.git

commit 44b8c1851393f7d38a119d4f24da8407cd45d35f
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  |  3 ++
 .../internal/DefaultLifecyclePluginAnalyzer.java   |  2 +-
 .../maven/lifecycle/internal/MojoExecutor.java     | 28 +++++++++----
 .../plugin/DefaultMojosExecutionStrategy.java      | 46 ++++++++++++++++++++++
 .../apache/maven/plugin/MojoExecutionRunner.java   | 36 +++++++++++++++++
 .../maven/plugin/MojosExecutionStrategy.java       | 45 +++++++++++++++++++++
 .../plugin/internal/DefaultMavenPluginManager.java | 15 +++++++
 .../lifecycle/internal/stub/MojoExecutorStub.java  | 18 ++++-----
 8 files changed, 175 insertions(+), 18 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 1160e88..b8f0c39 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
@@ -117,6 +117,9 @@ public class DefaultLifecycles
         return phaseToLifecycleMap;
     }
 
+    /**
+     * Returns an ordered list of lifecycles
+     */
     public List<Lifecycle> getLifeCycles()
     {
         List<String> lifecycleIds = Arrays.asList( 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 65843f8..0a936ad 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
@@ -110,7 +110,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() );
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 a06cd9f..28847ca 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
@@ -48,6 +48,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;
@@ -80,17 +82,21 @@ public class MojoExecutor
 
     private final ReadWriteLock aggregatorLock = new ReentrantReadWriteLock();
 
+    private final MojosExecutionStrategy mojosExecutionStrategy;
+
     @Inject
     public MojoExecutor(
             BuildPluginManager pluginManager,
             MavenPluginManager mavenPluginManager,
             LifecycleDependencyResolver lifeCycleDependencyResolver,
-            ExecutionEventCatapult eventCatapult )
+            ExecutionEventCatapult eventCatapult,
+            MojosExecutionStrategy mojosExecutionStrategy )
     {
         this.pluginManager = pluginManager;
         this.mavenPluginManager = mavenPluginManager;
         this.lifeCycleDependencyResolver = lifeCycleDependencyResolver;
         this.eventCatapult = eventCatapult;
+        this.mojosExecutionStrategy = mojosExecutionStrategy;
     }
 
     public DependencyContext newDependencyContext( MavenSession session, List<MojoExecution> mojoExecutions )
@@ -148,21 +154,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 6d55988..8b3e176 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
@@ -493,6 +493,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/internal/stub/MojoExecutorStub.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/MojoExecutorStub.java
index feedd1e..57eacb1 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
@@ -21,14 +21,15 @@ import org.apache.maven.lifecycle.internal.DependencyContext;
 import org.apache.maven.lifecycle.internal.ExecutionEventCatapult;
 import org.apache.maven.lifecycle.internal.LifecycleDependencyResolver;
 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.plugin.BuildPluginManager;
 import org.apache.maven.plugin.MavenPluginManager;
 import org.apache.maven.model.Plugin;
 import org.apache.maven.plugin.MojoExecution;
+import org.apache.maven.plugin.MojosExecutionStrategy;
 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;
@@ -47,24 +48,23 @@ public class MojoExecutorStub
             BuildPluginManager pluginManager,
             MavenPluginManager mavenPluginManager,
             LifecycleDependencyResolver lifeCycleDependencyResolver,
-            ExecutionEventCatapult eventCatapult )
+            ExecutionEventCatapult eventCatapult,
+            MojosExecutionStrategy mojosExecutionStrategy )
     {
-        super( pluginManager, mavenPluginManager, lifeCycleDependencyResolver, eventCatapult );
+        super( pluginManager, mavenPluginManager, lifeCycleDependencyResolver, eventCatapult, mojosExecutionStrategy );
     }
 
     @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;
     }