You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2024/11/29 20:53:31 UTC

(maven) branch master updated: [MNG-8394] Event bridge and properties fix (#1937)

This is an automated email from the ASF dual-hosted git repository.

gnodet pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/maven.git


The following commit(s) were added to refs/heads/master by this push:
     new 80387c597c [MNG-8394] Event bridge and properties fix (#1937)
80387c597c is described below

commit 80387c597c12847b980e22f1b52877629baadd09
Author: Tamas Cservenak <ta...@cservenak.net>
AuthorDate: Fri Nov 29 21:53:26 2024 +0100

    [MNG-8394] Event bridge and properties fix (#1937)
---
 .../apache/maven/cling/invoker/LookupContext.java  |  3 +++
 .../apache/maven/cling/invoker/LookupInvoker.java  | 24 ++++++++++++++++----
 .../org/apache/maven/execution/ExecutionEvent.java |  3 +++
 .../apache/maven/internal/impl/DefaultEvent.java   |  6 +++--
 .../apache/maven/internal/impl/EventSpyImpl.java   | 26 ++++++++++++----------
 5 files changed, 44 insertions(+), 18 deletions(-)

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 d0cae34c5f..cf0b09b4f4 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.Collections;
 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 @@ public class LookupContext implements AutoCloseable {
 
     // 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<String> 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 b4c3b57484..23c93bd535 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 abstract class LookupInvoker<C extends LookupContext> implements Invoker
 
     protected int doInvoke(C context) throws Exception {
         pushCoreProperties(context);
+        pushUserProperties(context);
         validate(context);
         prepare(context);
         configureLogging(context);
@@ -133,7 +134,7 @@ public abstract class LookupInvoker<C extends LookupContext> implements Invoker
         preCommands(context);
         container(context);
         postContainer(context);
-        pushUserProperties(context);
+        pushUserProperties(context); // after PropertyContributor SPI
         lookup(context);
         init(context);
         postCommands(context);
@@ -165,12 +166,27 @@ public abstract class LookupInvoker<C extends LookupContext> implements Invoker
                 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<String> 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 4b8d1ec11a..895faef030 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 2f03e66543..7003824de6 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 @@ import org.apache.maven.execution.ExecutionEvent;
 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 850e1b5dce..757b08a7ee 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<Listener> 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 class EventSpyImpl implements EventSpy {
         }
     }
 
+    /**
+     * 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 {}
 }