Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Maven 4 and JDK 21 at build time #74

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 34 additions & 30 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,21 @@
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-api-spi</artifactId>
<version>4.0.0-alpha-13</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>3.5.3</version>
<version>3.5.4</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<version>3.5.3</version>
<version>3.5.4</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down Expand Up @@ -111,12 +117,31 @@

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-enforcer-plugin</artifactId>
<version>3.4.1</version>
<executions>
<execution>
<id>enforce-java</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireJavaVersion>
<version>17</version>
</requireJavaVersion>
</rules>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<version>3.13.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<release>1.8</release>
<debug>true</debug>
<optimize>true</optimize>
</configuration>
Expand All @@ -134,8 +159,9 @@
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.6.0</version>
<version>3.12.0</version>
<configuration>
<goalPrefix>os</goalPrefix>
<skipErrorNoDescriptorsFound>true</skipErrorNoDescriptorsFound>
Expand All @@ -155,31 +181,9 @@
</execution>
</executions>
</plugin>
<plugin>
<!-- ensure that only methods available in java 1.6 can
be used even when compiling with java 1.7+ -->
<groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.18</version>
<configuration>
<signature>
<groupId>org.codehaus.mojo.signature</groupId>
<artifactId>java18</artifactId>
<version>1.0</version>
</signature>
</configuration>
<executions>
<execution>
<phase>process-classes</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.2.0</version>
<version>3.3.0</version>
<configuration>
<archive>
<manifestEntries>
Expand All @@ -196,7 +200,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.2.2</version>
<version>3.6.0</version>
<configuration>
<debug>true</debug>
<cloneProjectsTo>${project.build.directory}/it</cloneProjectsTo>
Expand Down
36 changes: 26 additions & 10 deletions src/main/java/kr/motd/maven/os/DetectExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import javax.annotation.Nullable;
import javax.inject.Inject;
import javax.inject.Singleton;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import


import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand All @@ -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<String, String> 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<String, String> getProperties(MavenSession session) throws MavenExecutionException {
final Properties sessionProps = new Properties();
sessionProps.putAll(session.getSystemProperties());
sessionProps.putAll(session.getUserProperties());
Expand All @@ -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;
}

/**
Expand Down
100 changes: 100 additions & 0 deletions src/main/java/kr/motd/maven/os/DetectPropertyContributor.java
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<String> getClassifierWithLikes(Map<String, String> 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<String, String> map;

private SimpleSystemPropertyOperations(Map<String, String> 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));
}
}

}
1 change: 1 addition & 0 deletions src/main/java/kr/motd/maven/os/Detector.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public Detector(SystemPropertyOperationProvider systemPropertyOperationProvider,
}

protected void detect(Properties props, List<String> classifierWithLikes) {
new Throwable().printStackTrace();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For debugging only? Shall we may remove this before merging?

log("------------------------------------------------------------------------");
log("Detecting the operating system and CPU architecture");
log("------------------------------------------------------------------------");
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/kr/motd/maven/os/RepositorySessionInjector.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,8 +34,27 @@ static void injectRepositorySession(
final Field f = cls.getDeclaredField("systemProperties");
f.setAccessible(true);
repoSessionProps = (Map<String, String>) f.get(repoSession);
for (Map.Entry<String, String> e : dict.entrySet()) {
repoSessionProps.put(e.getKey(), e.getValue());
try {
for (Map.Entry<String, String> 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<String, String> 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) {
Expand Down