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 a7d9facb2e90..1a13365c8c0b 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 @@ @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 Map getPhaseToLifecycleMap() return phaseToLifecycleMap; } + /** + * Returns an ordered list of lifecycles + */ public List 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 67a9057d6e4e..8c99d6729bfb 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.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 Set getPluginsBoundByDefaultToAllLifecycles( String packaging ) Map 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 @@ else if ( lifecycle.getDefaultLifecyclePhases() != null ) return plugins.keySet(); } - private List getOrderedLifecycles() - { - // NOTE: The lifecycle order can affect implied execution ids so we better be deterministic. - - List lifecycles = new ArrayList<>( defaultLifeCycles.getLifeCycles() ); - - Collections.sort( lifecycles, new Comparator() - { - - public int compare( Lifecycle l1, Lifecycle l2 ) - { - return l1.getId().compareTo( l2.getId() ); - } - - } ); - - return lifecycles; - } - private void parseLifecyclePhaseDefinitions( Map 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 cf97c8cab954..f81c71376d19 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.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 @@ else if ( Artifact.SCOPE_TEST.equals( classpath ) ) return Collections.unmodifiableCollection( scopes ); } - public void execute( MavenSession session, List mojoExecutions, ProjectIndex projectIndex ) + public void execute( final MavenSession session, + final List 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 000000000000..9507c7ad17c0 --- /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 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 000000000000..314e041a71b2 --- /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 000000000000..e4babf68d7db --- /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 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 86304714df1c..546f00435a36 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 T getConfiguredMojo( Class mojoInterface, MavenSession session, Mo 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 690532c77d85..d7a84d15a48c 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 void testLifecycle() { final List 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 763893e6ce9a..350dccd44b72 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 @@ 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 executions = Collections.synchronizedList( new ArrayList() ); @Override - public void execute( MavenSession session, MojoExecution mojoExecution, ProjectIndex projectIndex, - DependencyContext dependencyContext, PhaseRecorder phaseRecorder ) + public void execute( MavenSession session, List mojoExecutions, ProjectIndex projectIndex ) throws LifecycleExecutionException { - executions.add( mojoExecution ); + executions.addAll( mojoExecutions ); } @Override - public void execute( MavenSession session, List mojoExecutions, ProjectIndex projectIndex ) - throws LifecycleExecutionException + public List executeForkedExecutions( MojoExecution mojoExecution, MavenSession session, ProjectIndex projectIndex ) throws LifecycleExecutionException { - executions.addAll(mojoExecutions); + return null; }