All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kr.motd.maven.os.RepositorySessionInjector Maven / Gradle / Ivy

Go to download

A Maven extension/plugin that generates various useful platform-dependent project properties normalized from ${os.name} and ${os.arch}.

There is a newer version: 1.7.1
Show newest version
package kr.motd.maven.os;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Map;

import org.apache.maven.execution.MavenSession;
import org.codehaus.plexus.logging.Logger;

final class RepositorySessionInjector {
    @SuppressWarnings("unchecked")
    static void injectRepositorySession(
            Logger logger, MavenSession session, Map dict) {
        // Inject repository session properties.
        try {
            Map repoSessionProps;
            // Due to repackaging of Aether in Maven 3.1, session.getRepositorySession()
            // will return either org.eclipse.aether.RepositorySystemSession (Maven 3.1+)
            // or org.sonatype.aether.RepositorySystemSession (Maven 3.0.x)
            // depending on the version of Maven that executes the project.
            // Both interfaces have getSystemProperties() accessor method that returns Map.
            Object repoSession = session.getRepositorySession();
            Class cls = repoSession.getClass();
            final Method getSystemPropertiesMethod = cls.getDeclaredMethod("getSystemProperties");
            repoSessionProps = (Map) getSystemPropertiesMethod.invoke(repoSession);
            try {
                repoSessionProps.putAll(dict);
            } catch (Exception e) {
                // Time to hack: RepositorySystemSession.getSystemProperties() returned an immutable map.
                Field f = cls.getDeclaredField("systemProperties");
                f.setAccessible(true);
                repoSessionProps = (Map) f.get(repoSession);
                repoSessionProps.putAll(dict);
            }
        } catch (Throwable t) {
            logger.warn("Failed to inject repository session properties.", t);
        }
    }

    private RepositorySessionInjector() {}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy