mirror of https://github.com/apache/maven.git
[MNG-7310] Using the plexusContainer to prevent loading lifecycles from defined by extensions from other submodules.
This resolves #639.
This commit is contained in:
parent
4a06f1f5c3
commit
7e0e9f9fbe
|
@ -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<String, Lifecycle> lifecyclesMap;
|
||||
private final PlexusContainer plexusContainer;
|
||||
|
||||
public DefaultLifecycles()
|
||||
{
|
||||
this.lifecyclesMap = null;
|
||||
this.plexusContainer = null;
|
||||
}
|
||||
|
||||
@Inject
|
||||
public DefaultLifecycles( Map<String, Lifecycle> 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<String, Lifecycle> 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<String, Lifecycle> 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()
|
||||
|
|
|
@ -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<Lifecycle> 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<String, Lifecycle> 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" ) );
|
||||
|
|
|
@ -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(),
|
||||
|
|
|
@ -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<String> 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 );
|
||||
}
|
||||
|
||||
}
|
|
@ -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<ExecutionPlanItem> 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 )
|
||||
|
|
Loading…
Reference in New Issue