From 1641625902fec4d909e78b1cdb33b9d8a7dcb5cb Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 11 Jan 2024 12:28:28 +0100 Subject: [PATCH 1/4] Support Maven 4 and JDK 21 --- pom.xml | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/pom.xml b/pom.xml index be4ceb1..fba4cd8 100644 --- a/pom.xml +++ b/pom.xml @@ -65,12 +65,12 @@ org.apache.maven maven-plugin-api - 3.5.3 + 3.5.4 org.apache.maven maven-core - 3.5.3 + 3.5.4 provided @@ -111,6 +111,11 @@ + + org.apache.maven.plugins + maven-enforcer-plugin + 3.4.1 + maven-compiler-plugin 3.8.1 @@ -196,7 +201,7 @@ org.apache.maven.plugins maven-invoker-plugin - 3.2.2 + 3.6.0 true ${project.build.directory}/it From b4f26d65ee4de482792d2a2a0cffe8753450f903 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Thu, 11 Jan 2024 12:00:06 +0100 Subject: [PATCH 2/4] 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) { From 7809677990b198e2f0eb0497e85cfc4c08d87d7c Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Tue, 20 Feb 2024 18:11:26 +0100 Subject: [PATCH 3/4] Use maven 4.x extension point if available --- pom.xml | 6 ++ .../kr/motd/maven/os/DetectExtension.java | 36 +++++-- .../maven/os/DetectPropertyContributor.java | 100 ++++++++++++++++++ src/main/java/kr/motd/maven/os/Detector.java | 1 + 4 files changed, 133 insertions(+), 10 deletions(-) create mode 100644 src/main/java/kr/motd/maven/os/DetectPropertyContributor.java diff --git a/pom.xml b/pom.xml index fba4cd8..ffb4a3d 100644 --- a/pom.xml +++ b/pom.xml @@ -62,6 +62,12 @@ + + org.apache.maven + maven-api-spi + 4.0.0-alpha-13-SNAPSHOT + provided + org.apache.maven maven-plugin-api diff --git a/src/main/java/kr/motd/maven/os/DetectExtension.java b/src/main/java/kr/motd/maven/os/DetectExtension.java index 99be6f9..324dd53 100644 --- a/src/main/java/kr/motd/maven/os/DetectExtension.java +++ b/src/main/java/kr/motd/maven/os/DetectExtension.java @@ -25,6 +25,7 @@ import javax.annotation.Nullable; import javax.inject.Inject; +import javax.inject.Singleton; import org.apache.maven.AbstractMavenLifecycleParticipant; import org.apache.maven.MavenExecutionException; @@ -71,6 +72,7 @@ public class DetectExtension extends AbstractMavenLifecycleParticipant { private final Logger logger; private final Detector detector; + private static boolean disable; @Inject public DetectExtension(final Logger logger) { @@ -90,6 +92,10 @@ protected void logProperty(String name, String value) { }; } + public static void disable() { + disable = true; + } + @Override public void afterSessionStart(MavenSession session) throws MavenExecutionException { injectProperties(session); @@ -101,7 +107,26 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti } private void injectProperties(MavenSession session) throws MavenExecutionException { + // Bail out of disabled + if (disable) { + return; + } + // Detect the OS and CPU architecture. + final Map dict = getProperties(session); + + // Inject the current session. + injectSession(session, dict); + + /// Perform the interpolation for the properties of all dependencies. + if (session.getProjects() != null) { + for (MavenProject p : session.getProjects()) { + interpolate(dict, p); + } + } + } + + private Map getProperties(MavenSession session) throws MavenExecutionException { final Properties sessionProps = new Properties(); sessionProps.putAll(session.getSystemProperties()); sessionProps.putAll(session.getUserProperties()); @@ -122,16 +147,7 @@ private void injectProperties(MavenSession session) throws MavenExecutionExcepti dict.put(entry.getKey().toString(), entry.getValue().toString()); } } - - // Inject the current session. - injectSession(session, dict); - - /// Perform the interpolation for the properties of all dependencies. - if (session.getProjects() != null) { - for (MavenProject p : session.getProjects()) { - interpolate(dict, p); - } - } + return dict; } /** diff --git a/src/main/java/kr/motd/maven/os/DetectPropertyContributor.java b/src/main/java/kr/motd/maven/os/DetectPropertyContributor.java new file mode 100644 index 0000000..240e822 --- /dev/null +++ b/src/main/java/kr/motd/maven/os/DetectPropertyContributor.java @@ -0,0 +1,100 @@ +package kr.motd.maven.os; + +import org.apache.maven.api.spi.PropertyContributor; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.logging.Logger; + +import javax.inject.Inject; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.List; +import java.util.Map; +import java.util.Properties; + +@Component(role = PropertyContributor.class) +public class DetectPropertyContributor implements PropertyContributor { + + private final Logger logger; + + @Inject + DetectPropertyContributor(Logger logger) { + super(); + this.logger = logger; + } + + @Override + public void contribute(Map map) { + DetectExtension.disable(); + Properties props = new Properties(); + props.putAll(map); + Detector detector = new SimpleDetector(new SimpleSystemPropertyOperations(map), new SimpleFileOperations(), logger); + detector.detect(props, getClassifierWithLikes(map)); + } + + /** + * Inspects the session's user and project properties for the {@link + * DetectMojo#CLASSIFIER_WITH_LIKES_PROPERTY} and separates the property into a list. + */ + private static List getClassifierWithLikes(Map map) { + // Check to see if the project defined the + return DetectMojo.getClassifierWithLikes(map.get(DetectMojo.CLASSIFIER_WITH_LIKES_PROPERTY)); + } + + + private static class SimpleDetector extends Detector { + + private final Logger logger; + + private SimpleDetector(SystemPropertyOperationProvider systemPropertyOperationProvider, FileOperationProvider fileOperationProvider, Logger logger) { + super(systemPropertyOperationProvider, fileOperationProvider); + this.logger = logger; + } + + @Override + protected void log(String message) { + logger.info(message); + } + + @Override + protected void logProperty(String name, String value) { + if (logger.isInfoEnabled()) { + logger.info(name + ": " + value); + } + } + + } + + private static class SimpleSystemPropertyOperations implements SystemPropertyOperationProvider { + final Map map; + + private SimpleSystemPropertyOperations(Map map) { + this.map = map; + } + + @Override + public String getSystemProperty(String name) { + return System.getProperty(name); + } + + @Override + public String getSystemProperty(String name, String def) { + return System.getProperty(name, def); + } + + @Override + public String setSystemProperty(String name, String value) { + map.put(name, value); + return System.setProperty(name, value); + } + } + + private static class SimpleFileOperations implements FileOperationProvider { + @Override + public InputStream readFile(String fileName) throws IOException { + return Files.newInputStream(Paths.get(fileName)); + } + } + +} diff --git a/src/main/java/kr/motd/maven/os/Detector.java b/src/main/java/kr/motd/maven/os/Detector.java index 769e47b..155b732 100644 --- a/src/main/java/kr/motd/maven/os/Detector.java +++ b/src/main/java/kr/motd/maven/os/Detector.java @@ -70,6 +70,7 @@ public Detector(SystemPropertyOperationProvider systemPropertyOperationProvider, } protected void detect(Properties props, List classifierWithLikes) { + new Throwable().printStackTrace(); log("------------------------------------------------------------------------"); log("Detecting the operating system and CPU architecture"); log("------------------------------------------------------------------------"); From a33de872ef36d199bd588adb39ad5fda49b8dc40 Mon Sep 17 00:00:00 2001 From: Guillaume Nodet Date: Mon, 15 Apr 2024 09:52:24 +0200 Subject: [PATCH 4/4] Update to Maven 4.0.0-alpha-13, require JDK 17 at build time --- pom.xml | 49 +++++++++++++++++++++---------------------------- 1 file changed, 21 insertions(+), 28 deletions(-) diff --git a/pom.xml b/pom.xml index ffb4a3d..5469945 100644 --- a/pom.xml +++ b/pom.xml @@ -65,7 +65,7 @@ org.apache.maven maven-api-spi - 4.0.0-alpha-13-SNAPSHOT + 4.0.0-alpha-13 provided @@ -121,13 +121,27 @@ org.apache.maven.plugins maven-enforcer-plugin 3.4.1 + + + enforce-java + + enforce + + + + + 17 + + + + + maven-compiler-plugin - 3.8.1 + 3.13.0 - 1.8 - 1.8 + 1.8 true true @@ -145,8 +159,9 @@ + org.apache.maven.plugins maven-plugin-plugin - 3.6.0 + 3.12.0 os true @@ -166,31 +181,9 @@ - - - org.codehaus.mojo - animal-sniffer-maven-plugin - 1.18 - - - org.codehaus.mojo.signature - java18 - 1.0 - - - - - process-classes - - check - - - - maven-jar-plugin - 3.2.0 + 3.3.0