diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java index d0cae34c5fd0..cf0b09b4f40c 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupContext.java @@ -24,6 +24,7 @@ import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.Set; import java.util.function.Consumer; import java.util.function.Function; @@ -72,6 +73,8 @@ protected LookupContext(InvokerRequest invokerRequest) { // this one "evolves" as process progresses (instance is immutable but instances are replaced) public ProtoSession protoSession; + // here we track which user properties we pushed to Java System Properties (internal only) + public Set pushedUserProperties; public Logger logger; public ILoggerFactory loggerFactory; diff --git a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java index b4c3b57484f6..23c93bd535bc 100644 --- a/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java +++ b/impl/maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java @@ -124,6 +124,7 @@ public int invoke(InvokerRequest invokerRequest) throws InvokerException { protected int doInvoke(C context) throws Exception { pushCoreProperties(context); + pushUserProperties(context); validate(context); prepare(context); configureLogging(context); @@ -133,7 +134,7 @@ protected int doInvoke(C context) throws Exception { preCommands(context); container(context); postContainer(context); - pushUserProperties(context); + pushUserProperties(context); // after PropertyContributor SPI lookup(context); init(context); postCommands(context); @@ -165,12 +166,27 @@ protected void pushCoreProperties(C context) throws Exception { context.invokerRequest.installationDirectory().toString()); } + /** + * Note: this method is called twice from {@link #doInvoke(LookupContext)} and modifies context. First invocation + * when {@link LookupContext#pushedUserProperties} is null will push user properties IF key does not already + * exist among Java System Properties, and collects all they key it pushes. Second invocation happens AFTER + * {@link PropertyContributor} SPI invocation, and "refreshes" already pushed user properties by re-writing them + * as SPI may have modified them. + */ protected void pushUserProperties(C context) throws Exception { ProtoSession protoSession = context.protoSession; HashSet sys = new HashSet<>(protoSession.getSystemProperties().keySet()); - protoSession.getUserProperties().entrySet().stream() - .filter(k -> !sys.contains(k.getKey())) - .forEach(k -> System.setProperty(k.getKey(), k.getValue())); + if (context.pushedUserProperties == null) { + context.pushedUserProperties = new HashSet<>(); + protoSession.getUserProperties().entrySet().stream() + .filter(k -> !sys.contains(k.getKey())) + .peek(k -> context.pushedUserProperties.add(k.getKey())) + .forEach(k -> System.setProperty(k.getKey(), k.getValue())); + } else { + protoSession.getUserProperties().entrySet().stream() + .filter(k -> context.pushedUserProperties.contains(k.getKey()) || !sys.contains(k.getKey())) + .forEach(k -> System.setProperty(k.getKey(), k.getValue())); + } } protected void validate(C context) throws Exception {} diff --git a/impl/maven-core/src/main/java/org/apache/maven/execution/ExecutionEvent.java b/impl/maven-core/src/main/java/org/apache/maven/execution/ExecutionEvent.java index 4b8d1ec11aa6..895faef03060 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/execution/ExecutionEvent.java +++ b/impl/maven-core/src/main/java/org/apache/maven/execution/ExecutionEvent.java @@ -29,6 +29,9 @@ public interface ExecutionEvent { /** * The possible types of execution events. + * + * Note: do not modify this enum, or, make sure that this enum and + * {@link org.apache.maven.api.EventType} have same elements in same order. */ enum Type { ProjectDiscoveryStarted, diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultEvent.java b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultEvent.java index 2f03e6654333..7003824de62f 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultEvent.java +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/DefaultEvent.java @@ -30,15 +30,17 @@ public class DefaultEvent implements Event { private final InternalMavenSession session; private final ExecutionEvent delegate; + private final EventType eventType; - public DefaultEvent(InternalMavenSession session, ExecutionEvent delegate) { + public DefaultEvent(InternalMavenSession session, ExecutionEvent delegate, EventType eventType) { this.session = session; this.delegate = delegate; + this.eventType = eventType; } @Override public EventType getType() { - return EventType.valueOf(delegate.getType().name()); + return eventType; } @Override diff --git a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/EventSpyImpl.java b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/EventSpyImpl.java index 850e1b5dceb5..757b08a7eedb 100644 --- a/impl/maven-core/src/main/java/org/apache/maven/internal/impl/EventSpyImpl.java +++ b/impl/maven-core/src/main/java/org/apache/maven/internal/impl/EventSpyImpl.java @@ -18,40 +18,35 @@ */ package org.apache.maven.internal.impl; -import javax.inject.Inject; import javax.inject.Named; import javax.inject.Singleton; import java.util.Collection; import org.apache.maven.api.Event; +import org.apache.maven.api.EventType; import org.apache.maven.api.Listener; import org.apache.maven.eventspy.EventSpy; import org.apache.maven.execution.ExecutionEvent; +/** + * Bridges between Maven3 events and Maven4 events. + */ @Named @Singleton public class EventSpyImpl implements EventSpy { - - private DefaultSessionFactory sessionFactory; - - @Inject - EventSpyImpl(DefaultSessionFactory sessionFactory) { - this.sessionFactory = sessionFactory; - } - @Override public void init(Context context) throws Exception {} @Override public void onEvent(Object arg) throws Exception { - if (arg instanceof ExecutionEvent) { - ExecutionEvent ee = (ExecutionEvent) arg; + if (arg instanceof ExecutionEvent ee) { InternalMavenSession session = InternalMavenSession.from(ee.getSession().getSession()); + EventType eventType = convert(ee.getType()); Collection listeners = session.getListeners(); if (!listeners.isEmpty()) { - Event event = new DefaultEvent(session, ee); + Event event = new DefaultEvent(session, ee, eventType); for (Listener listener : listeners) { listener.onEvent(event); } @@ -59,6 +54,13 @@ public void onEvent(Object arg) throws Exception { } } + /** + * Simple "conversion" from Maven3 event type enum to Maven4 enum. + */ + protected EventType convert(ExecutionEvent.Type type) { + return EventType.values()[type.ordinal()]; + } + @Override public void close() throws Exception {} }