From 5621b1daba4f0c4ad345bfe9faa40ab294171e19 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Tue, 21 May 2024 10:15:15 +0200 Subject: [PATCH] Fix DefaultLifecycleRegistry so that lookups are lazy --- .../impl/DefaultLifecycleRegistry.java | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleRegistry.java b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleRegistry.java index fca253e17a47..1101ba02da24 100644 --- a/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleRegistry.java +++ b/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultLifecycleRegistry.java @@ -23,6 +23,7 @@ import javax.inject.Provider; import javax.inject.Singleton; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.HashSet; @@ -30,6 +31,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; +import java.util.Optional; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -38,7 +40,9 @@ import org.apache.maven.api.model.Plugin; import org.apache.maven.api.services.LifecycleRegistry; import org.apache.maven.api.services.LookupException; +import org.apache.maven.api.spi.ExtensibleEnumProvider; import org.apache.maven.api.spi.LifecycleProvider; +import org.apache.maven.lifecycle.mapping.LifecyclePhase; import org.codehaus.plexus.PlexusContainer; import org.codehaus.plexus.component.repository.exception.ComponentLookupException; @@ -52,9 +56,9 @@ */ @Named @Singleton -public class DefaultLifecycleRegistry - extends ExtensibleEnumRegistries.DefaultExtensibleEnumRegistry - implements LifecycleRegistry { +public class DefaultLifecycleRegistry implements LifecycleRegistry { + + private final List providers; public DefaultLifecycleRegistry() { this(Collections.emptyList()); @@ -62,7 +66,9 @@ public DefaultLifecycleRegistry() { @Inject public DefaultLifecycleRegistry(List providers) { - super(providers, new CleanLifecycle(), new DefaultLifecycle(), new SiteLifecycle(), new WrapperLifecycle()); + List p = new ArrayList<>(providers); + p.add(() -> List.of(new CleanLifecycle(), new DefaultLifecycle(), new SiteLifecycle(), new WrapperLifecycle())); + this.providers = p; // validate lifecycle for (Lifecycle lifecycle : this) { Set set = new HashSet<>(); @@ -77,12 +83,17 @@ public DefaultLifecycleRegistry(List providers) { @Override public Iterator iterator() { - return values.values().iterator(); + return stream().toList().iterator(); } @Override public Stream stream() { - return values.values().stream(); + return providers.stream().map(ExtensibleEnumProvider::provides).flatMap(Collection::stream); + } + + @Override + public Optional lookup(String id) { + return Optional.empty(); } @Named @@ -121,22 +132,26 @@ public String id() { @Override public Collection phases() { - return lifecycle.getDefaultLifecyclePhases().entrySet().stream() - .map(e -> new Phase() { + return lifecycle.getPhases().stream() + .map(name -> (Phase) new Phase() { @Override public String name() { - return e.getKey(); + return name; } @Override public List plugins() { - Map plugins = new LinkedHashMap<>(); - DefaultPackagingRegistry.parseLifecyclePhaseDefinitions( - plugins, e.getKey(), e.getValue()); - return plugins.values().stream().toList(); + Map lfPhases = lifecycle.getDefaultLifecyclePhases(); + LifecyclePhase phase = lfPhases != null ? lfPhases.get(name) : null; + if (phase != null) { + Map plugins = new LinkedHashMap<>(); + DefaultPackagingRegistry.parseLifecyclePhaseDefinitions(plugins, name, phase); + return plugins.values().stream().toList(); + } + return List.of(); } }) - .collect(Collectors.toCollection(java.util.ArrayList::new)); + .toList(); } }; }