Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: tison <wander4096@gmail.com>
  • Loading branch information
tisonkun committed Sep 23, 2024
1 parent 3cb25e9 commit 9a92482
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<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 {
// Detect the OS and CPU architecture.
final Properties sessionProps = new Properties();
sessionProps.putAll(session.getSystemProperties());
Expand All @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, String> 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<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 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));
}
}
}
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;
import org.codehaus.plexus.logging.Logger;
Expand All @@ -28,7 +30,24 @@ static void injectRepositorySession(Logger logger, MavenSession session, Map<Str
final Field f = cls.getDeclaredField("systemProperties");
f.setAccessible(true);
repoSessionProps = (Map<String, String>) 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@
<role>org.apache.maven.AbstractMavenLifecycleParticipant</role>
<role-hint>detect-os</role-hint>
<implementation>com.tisonkun.os.maven.DetectExtension</implementation>
<description />
<description/>
<isolated-realm>false</isolated-realm>
</component>
<component>
<role>org.apache.maven.api.spi.PropertyContributor</role>
<role-hint>detect-os</role-hint>
<implementation>com.tisonkun.os.maven.DetectPropertyContributor</implementation>
<description/>
<isolated-realm>false</isolated-realm>
</component>
</components>
Expand Down

0 comments on commit 9a92482

Please sign in to comment.