diff --git a/pom.xml b/pom.xml index be4ceb1..5469945 100644 --- a/pom.xml +++ b/pom.xml @@ -62,15 +62,21 @@ + + org.apache.maven + maven-api-spi + 4.0.0-alpha-13 + provided + 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,12 +117,31 @@ + + 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 @@ -134,8 +159,9 @@ + org.apache.maven.plugins maven-plugin-plugin - 3.6.0 + 3.12.0 os true @@ -155,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 @@ -196,7 +200,7 @@ org.apache.maven.plugins maven-invoker-plugin - 3.2.2 + 3.6.0 true ${project.build.directory}/it 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("------------------------------------------------------------------------"); 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) {