diff --git a/plugin-maven/src/main/java/com/tisonkun/os/maven/DetectExtension.java b/plugin-maven/src/main/java/com/tisonkun/os/maven/DetectExtension.java index 8f40595..ba3057f 100644 --- a/plugin-maven/src/main/java/com/tisonkun/os/maven/DetectExtension.java +++ b/plugin-maven/src/main/java/com/tisonkun/os/maven/DetectExtension.java @@ -69,6 +69,14 @@ */ @Component(role = AbstractMavenLifecycleParticipant.class, hint = "detect-os") public class DetectExtension extends AbstractMavenLifecycleParticipant { + /** + * Describe why. + */ + private static boolean disable; + + public static void disable() { + disable = true; + } private final Logger logger; private final Detector detector; @@ -95,6 +103,24 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti } private void injectProperties(MavenSession session) throws MavenExecutionException { + // Bail out of disabled + if (disable) { + return; + } + 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 { // Detect the OS and CPU architecture. final Properties sessionProps = new Properties(); sessionProps.putAll(session.getSystemProperties()); @@ -118,15 +144,7 @@ private void injectProperties(MavenSession session) throws MavenExecutionExcepti } } - // 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/plugin-maven/src/main/java/com/tisonkun/os/maven/DetectPropertyContributor.java b/plugin-maven/src/main/java/com/tisonkun/os/maven/DetectPropertyContributor.java new file mode 100644 index 0000000..d101ee4 --- /dev/null +++ b/plugin-maven/src/main/java/com/tisonkun/os/maven/DetectPropertyContributor.java @@ -0,0 +1,80 @@ +package com.tisonkun.os.maven; + +import com.tisonkun.os.core.Detector; +import com.tisonkun.os.core.FileOperationProvider; +import com.tisonkun.os.core.SystemPropertyOperationProvider; +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; +import javax.inject.Inject; +import org.apache.maven.api.spi.PropertyContributor; +import org.codehaus.plexus.component.annotations.Component; +import org.codehaus.plexus.logging.Logger; + +@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(); + + final Properties props = new Properties(); + props.putAll(map); + + final Detector detector = + new Detector(new SimpleSystemPropertyOperations(map), new SimpleFileOperations(), logger::info); + 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 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/plugin-maven/src/main/java/com/tisonkun/os/maven/RepositorySessionInjector.java b/plugin-maven/src/main/java/com/tisonkun/os/maven/RepositorySessionInjector.java index 1c556c8..8668ac6 100644 --- a/plugin-maven/src/main/java/com/tisonkun/os/maven/RepositorySessionInjector.java +++ b/plugin-maven/src/main/java/com/tisonkun/os/maven/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; import org.codehaus.plexus.logging.Logger; @@ -28,7 +30,24 @@ static void injectRepositorySession(Logger logger, MavenSession session, Map) f.get(repoSession); - repoSessionProps.putAll(dict); + try { + repoSessionProps.putAll(dict); + } catch (Exception ex2) { + // In Maven 4, DefaultCloseableSession uses an immutable map + // but DefaultRepositorySystemSession may also have an immutable map + repoSessionProps = new HashMap<>(repoSessionProps); + repoSessionProps.putAll(dict); + 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) { logger.warn("Failed to inject repository session properties.", t); diff --git a/plugin-maven/src/main/resources/META-INF/plexus/components.xml b/plugin-maven/src/main/resources/META-INF/plexus/components.xml index 220dd88..a8f5533 100644 --- a/plugin-maven/src/main/resources/META-INF/plexus/components.xml +++ b/plugin-maven/src/main/resources/META-INF/plexus/components.xml @@ -22,7 +22,14 @@ org.apache.maven.AbstractMavenLifecycleParticipant detect-os com.tisonkun.os.maven.DetectExtension - + + false + + + org.apache.maven.api.spi.PropertyContributor + detect-os + com.tisonkun.os.maven.DetectPropertyContributor + false