From d26765b59123053bea289a15a000f967a985858f Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 11 Jan 2024 12:00:06 +0100 Subject: [PATCH] Support Maven 4, fixes #72 --- .../maven/os/RepositorySessionInjector.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/main/java/kr/motd/maven/os/RepositorySessionInjector.java b/src/main/java/kr/motd/maven/os/RepositorySessionInjector.java index 94895ec..43134e0 100644 --- a/src/main/java/kr/motd/maven/os/RepositorySessionInjector.java +++ b/src/main/java/kr/motd/maven/os/RepositorySessionInjector.java @@ -2,6 +2,8 @@ import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.Collections; +import java.util.HashMap; import java.util.Map; import org.apache.maven.execution.MavenSession; @@ -32,8 +34,27 @@ static void injectRepositorySession( final Field f = cls.getDeclaredField("systemProperties"); f.setAccessible(true); repoSessionProps = (Map) f.get(repoSession); - for (Map.Entry e : dict.entrySet()) { - repoSessionProps.put(e.getKey(), e.getValue()); + try { + for (Map.Entry e : dict.entrySet()) { + repoSessionProps.put(e.getKey(), e.getValue()); + } + } catch (Exception ex2) { + // In Maven 4, DefaultCloseableSession uses an immutable map + // but DefaultRepositorySystemSession may also have an immutable map + repoSessionProps = new HashMap<>(repoSessionProps); + for (Map.Entry e : dict.entrySet()) { + repoSessionProps.put(e.getKey(), e.getValue()); + } + repoSessionProps = Collections.unmodifiableMap(repoSessionProps); + f.set(repoSession, repoSessionProps); + try { + // This is to support DefaultRepositorySystemSession + final Field fv = cls.getDeclaredField("systemPropertiesView"); + fv.setAccessible(true); + fv.set(repoSession, repoSessionProps); + } catch (Exception ex3) { + // ignore + } } } } catch (Throwable t) {