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 9849fffea3..c29791119a 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 @@ -31,6 +31,8 @@ import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -51,20 +53,17 @@ public class DefaultLifecycles // @Configuration(source="org/apache/maven/lifecycle/lifecycles.xml") - private final Map lifecyclesMap; + private final PlexusContainer plexusContainer; public DefaultLifecycles() { - this.lifecyclesMap = null; + this.plexusContainer = null; } @Inject - public DefaultLifecycles( Map lifecyclesMap ) + public DefaultLifecycles( PlexusContainer plexusContainer ) { - // Must keep the lifecyclesMap as is. - // During initialization it only contains the default lifecycles. - // However, extensions might add custom lifecycles later on. - this.lifecyclesMap = lifecyclesMap; + this.plexusContainer = plexusContainer; } /** @@ -139,6 +138,8 @@ public class DefaultLifecycles } }; + Map lifecyclesMap = lookupLifecycles(); + // ensure canonical order of standard lifecycles return lifecyclesMap.values().stream() .peek( l -> Objects.requireNonNull( l.getId(), "A lifecycle must have an id." ) ) @@ -146,6 +147,26 @@ public class DefaultLifecycles .collect( Collectors.toList() ); } + private Map lookupLifecycles() + { + // TODO: Remove the following code when maven-compat is gone + // This code is here to ensure maven-compat's EmptyLifecycleExecutor keeps on working. + if ( plexusContainer == null ) + { + return new HashMap<>(); + } + + // Lifecycles cannot be cached as extensions might add custom lifecycles later in the execution. + try + { + return plexusContainer.lookupMap( Lifecycle.class ); + } + catch ( ComponentLookupException e ) + { + throw new IllegalStateException( "Unable to lookup lifecycles from the plexus container", e ); + } + } + public String getLifecyclePhaseList() { return getLifeCycles().stream() 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 046227a604..8ad2785ae8 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 @@ -19,10 +19,13 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; import javax.inject.Inject; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.testing.PlexusTest; import org.junit.jupiter.api.Test; @@ -30,6 +33,8 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.arrayWithSize; import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.is; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @author Kristian Rosenvold @@ -82,6 +87,7 @@ public class DefaultLifecyclesTest @Test public void testCustomLifecycle() + throws ComponentLookupException { List myLifecycles = new ArrayList<>(); Lifecycle myLifecycle = new Lifecycle( "etl", @@ -90,8 +96,12 @@ public class DefaultLifecyclesTest myLifecycles.add( myLifecycle ); myLifecycles.addAll( defaultLifeCycles.getLifeCycles() ); - DefaultLifecycles dl = new DefaultLifecycles( myLifecycles.stream() - .collect( Collectors.toMap( l -> l.getId(), l -> l ) ) ); + Map lifeCycles = myLifecycles.stream() + .collect( Collectors.toMap( Lifecycle::getId, l -> l ) ); + PlexusContainer mockedPlexusContainer = mock( PlexusContainer.class ); + when( mockedPlexusContainer.lookupMap( Lifecycle.class ) ).thenReturn( lifeCycles ); + + DefaultLifecycles dl = new DefaultLifecycles( mockedPlexusContainer ); assertThat( dl.getLifeCycles().get( 0 ).getId(), is( "clean" ) ); assertThat( dl.getLifeCycles().get( 1 ).getId(), is( "default" ) ); diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleExecutionPlanCalculatorTest.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleExecutionPlanCalculatorTest.java index 28d552afec..dc5f922d16 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleExecutionPlanCalculatorTest.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/LifecycleExecutionPlanCalculatorTest.java @@ -23,6 +23,7 @@ import org.apache.maven.lifecycle.internal.stub.DefaultLifecyclesStub; import org.apache.maven.lifecycle.internal.stub.PluginPrefixResolverStub; import org.apache.maven.lifecycle.internal.stub.PluginVersionResolverStub; import org.apache.maven.lifecycle.internal.stub.ProjectDependencyGraphStub; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -63,6 +64,7 @@ public class LifecycleExecutionPlanCalculatorTest // Maybe also make one with LifeCycleTasks public static LifecycleExecutionPlanCalculator createExecutionPlaceCalculator( MojoDescriptorCreator mojoDescriptorCreator ) + throws ComponentLookupException { LifecyclePluginResolver lifecyclePluginResolver = new LifecyclePluginResolver( new PluginVersionResolverStub() ); return new DefaultLifecycleExecutionPlanCalculator( new BuildPluginManagerStub(), diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java index 1d3f8fd5d9..5d78a6bfa2 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/DefaultLifecyclesStub.java @@ -17,6 +17,8 @@ package org.apache.maven.lifecycle.internal.stub; import org.apache.maven.lifecycle.DefaultLifecycles; import org.apache.maven.lifecycle.Lifecycle; +import org.codehaus.plexus.PlexusContainer; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import java.util.Arrays; import java.util.HashMap; @@ -25,6 +27,8 @@ import java.util.List; import java.util.Map; import static org.apache.maven.lifecycle.internal.stub.LifecycleExecutionPlanCalculatorStub.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @author Kristian Rosenvold @@ -33,6 +37,7 @@ import static org.apache.maven.lifecycle.internal.stub.LifecycleExecutionPlanCal public class DefaultLifecyclesStub { public static DefaultLifecycles createDefaultLifecycles() + throws ComponentLookupException { List stubDefaultCycle = @@ -58,7 +63,11 @@ public class DefaultLifecyclesStub lifeCycles.put( s, lifecycle ); } - return new DefaultLifecycles( lifeCycles ); + + PlexusContainer mockedContainer = mock( PlexusContainer.class ); + when( mockedContainer.lookupMap( Lifecycle.class ) ).thenReturn( lifeCycles ); + + return new DefaultLifecycles( mockedContainer ); } } \ No newline at end of file diff --git a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/LifecycleExecutionPlanCalculatorStub.java b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/LifecycleExecutionPlanCalculatorStub.java index f7514b788b..80770d5924 100644 --- a/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/LifecycleExecutionPlanCalculatorStub.java +++ b/maven-core/src/test/java/org/apache/maven/lifecycle/internal/stub/LifecycleExecutionPlanCalculatorStub.java @@ -16,6 +16,7 @@ package org.apache.maven.lifecycle.internal.stub; import org.apache.maven.execution.MavenSession; +import org.apache.maven.lifecycle.DefaultLifecycles; import org.apache.maven.lifecycle.LifecycleNotFoundException; import org.apache.maven.lifecycle.LifecyclePhaseNotFoundException; import org.apache.maven.lifecycle.MavenExecutionPlan; @@ -38,6 +39,7 @@ import org.apache.maven.plugin.descriptor.PluginDescriptor; import org.apache.maven.plugin.prefix.NoPluginFoundForPrefixException; import org.apache.maven.plugin.version.PluginVersionResolutionException; import org.apache.maven.project.MavenProject; +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.codehaus.plexus.util.xml.Xpp3Dom; import java.util.ArrayList; @@ -205,7 +207,20 @@ public class LifecycleExecutionPlanCalculatorStub { final List planItemList = ExecutionPlanItem.createExecutionPlanItems( project, mojoExecutions ); - return new MavenExecutionPlan( planItemList, DefaultLifecyclesStub.createDefaultLifecycles() ); + return new MavenExecutionPlan( planItemList, getDefaultLifecycles()); + } + + private static DefaultLifecycles getDefaultLifecycles() + { + try + { + return DefaultLifecyclesStub.createDefaultLifecycles(); + } + catch ( ComponentLookupException e ) + { + // ignore + return null; + } } private static MojoExecution createMojoExecution( String goal, String executionId, MojoDescriptor mojoDescriptor )