kr.motd.maven.os.DetectExtension Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of os-maven-plugin Show documentation
Show all versions of os-maven-plugin Show documentation
A Maven extension/plugin that generates various useful platform-dependent project properties normalized from
${os.name} and ${os.arch}.
/*
* Copyright 2014 Trustin Heuiseung Lee.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package kr.motd.maven.os;
import org.apache.maven.AbstractMavenLifecycleParticipant;
import org.apache.maven.MavenExecutionException;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Build;
import org.apache.maven.model.Dependency;
import org.apache.maven.model.DependencyManagement;
import org.apache.maven.model.Exclusion;
import org.apache.maven.model.Model;
import org.apache.maven.model.ModelBase;
import org.apache.maven.model.Plugin;
import org.apache.maven.project.MavenProject;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.logging.Logger;
import org.codehaus.plexus.util.InterpolationFilterReader;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
/**
* Detects the current operating system and architecture, normalizes them, and sets them to various project
* properties.
*
* - {@code os.detected.name} - normalized {@code os.name} (e.g. {@code linux}, {@code osx})
* - {@code os.detected.arch} - normalized {@code os.arch} (e.g. {@code x86_64}, {@code x86_32})
* - {@code os.detected.version} - {@code os.detected.version.major}.{@code os.detected.version.minor}
* - {@code os.detected.version.major} - major part of {@code os.version} (integer value)
* - {@code os.detected.version.minor} - minor part of {@code os.version} (integer value)
* - {@code os.detected.classifier} - a shortcut for {@code 'os.detectedName'.'os.detectedArch'}
* (e.g. {@code linux-x86_64}). If the property {@code ${os.detection.classifierWithLikes}} is set,
* the first value for which a corresponding {@code os.detected.release.like.{variant}} property
* exists will be appended to the classifier (e.g. building on ubuntu with
* {@code os.detection.classifierWithLikes = "debian,rhel"} would result in
* {@code os.detected.classifier = "linux-x86_64-debian"}).
* - {@code os.detected.release} - provides the ID for the linux release (if available).
* - {@code os.detected.release.version} - provides version ID for this linux release. Only
* available if ${os.detected.release} is also available.
* - {@code os.detected.release.like.{variant}} - Identifies a linux release that this release is
* "like" (for example, ubuntu is "like" debian). Only available if ${os.detected.release} is also
* available. An entry will always be made for os.detected.release.like.${os.detected.release}.
*
*/
@Component(role = AbstractMavenLifecycleParticipant.class, hint = "detect-os")
public class DetectExtension extends AbstractMavenLifecycleParticipant {
@Requirement
@SuppressWarnings("UnusedDeclaration")
private Logger logger;
private final Detector detector = new Detector() {
@Override
protected void log(String message) {
logger.info(message);
}
@Override
protected void logProperty(String name, String value) {
if (logger.isInfoEnabled()) {
logger.info(name + ": " + value);
}
}
};
@Override
public void afterProjectsRead(MavenSession session) throws MavenExecutionException {
// Detect the OS and CPU architecture.
Properties sessionProps = new Properties();
sessionProps.putAll(session.getSystemProperties());
sessionProps.putAll(session.getUserProperties());
try {
detector.detect(sessionProps, getClassifierWithLikes(session));
} catch (DetectionException e) {
throw new MavenExecutionException(e.getMessage(), session.getCurrentProject().getFile());
}
// Generate the dictionary.
Map dict = new LinkedHashMap();
dict.put(Detector.DETECTED_NAME, sessionProps.getProperty(Detector.DETECTED_NAME));
dict.put(Detector.DETECTED_ARCH, sessionProps.getProperty(Detector.DETECTED_ARCH));
dict.put(Detector.DETECTED_CLASSIFIER, sessionProps.getProperty(Detector.DETECTED_CLASSIFIER));
for (Map.Entry
© 2015 - 2025 Weber Informatics LLC | Privacy Policy