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

fix: compatible with Maven 4 #9

Merged
merged 5 commits into from
Sep 24, 2024
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Check license header
uses: korandoru/hawkeye@v4
uses: korandoru/hawkeye@v5
- name: Setup Java
uses: actions/setup-java@v4
with:
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
# OS Detector

A detector for the OS name and architecture, providing a uniform classifier to be used in the names of native artifacts.
![Maven Central](https://img.shields.io/maven-central/v/com.tisonkun.os/os-detector-core.svg?logo=Apache+Maven&logoColor=blue)

This repository is an effort to provide the OS detection logic the same as [os-maven-plugin](https://github.com/trustin/os-maven-plugin/) as a standalone artifact, and redistribute the Maven plugin as well as [the Gradle plugin](https://github.com/google/osdetector-gradle-plugin) (developed by Google) based on such a core lib to align the manner.
A detector for the OS name and architecture. It provides a uniform classifier to be used in the names of native artifacts.

This repository is an effort to provide functionalities the same as [os-maven-plugin](https://github.com/trustin/os-maven-plugin/), but as a standalone artifact. It also redistributes the Maven plugin as well as [the Gradle plugin](https://github.com/google/osdetector-gradle-plugin) (developed by Google) based on such a core lib to ensure the manner is the same.

I'm seeking for merging these three efforts into one. Check [this issue](https://github.com/trustin/os-maven-plugin/issues/70#issuecomment-1906110062) for more information.

Currently, I'm actively maintaining this repository (lib, plugin-maven and plugin-gradle) for publicly testing, as well as bugfixes + improvements. You can use it as a production-ready solution since the original logics are battle-tested over the years.
Currently, I'm actively maintaining this repository (`lib`, `plugin-maven` and `plugin-gradle`) for publicly testing, as well as bugfixes + improvements. You can use it as a production-ready solution since the original logics are battle-tested over the years.

## Programmable

Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

plugins {
id("com.diffplug.spotless") version "6.24.0"
id("com.diffplug.spotless") version "6.25.0"
id("io.github.gradle-nexus.publish-plugin") version "1.3.0"
id("java")
}
Expand Down Expand Up @@ -47,7 +47,7 @@ subprojects {
trimTrailingWhitespace()
}
java {
palantirJavaFormat("2.36.0")
palantirJavaFormat("2.50.0")
importOrder("\\#|")
removeUnusedImports()
endWithNewline()
Expand Down
8 changes: 8 additions & 0 deletions dev/RELEASE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# How to release OS Detector

```shell
./gradlew publishToMavenLocal
./gradlew publishToSonatype closeAndReleaseSonatypeStagingRepository
./gradlew publishPlugins --validate-only
./gradlew publishPlugins
```
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
# limitations under the License.

group=com.tisonkun.os
version=0.3.0
version=0.4.0
8 changes: 4 additions & 4 deletions plugin-maven/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import de.benediktritter.maven.plugin.development.task.GenerateMavenPluginDescri
plugins {
id("java-publish")
// @see https://www.benediktritter.de/maven-plugin-development/
id("de.benediktritter.maven-plugin-development") version "0.4.2"
id("de.benediktritter.maven-plugin-development") version "0.4.3"
}

mavenPlugin {
Expand All @@ -37,12 +37,12 @@ tasks.withType<GenerateHelpMojoSourcesTask>().configureEach {
}

dependencies {
compileOnly("org.apache.maven:maven-plugin-api:3.9.6")
compileOnly("org.apache.maven:maven-plugin-api:3.9.9")
compileOnly("org.apache.maven.plugin-tools:maven-plugin-annotations:3.11.0")
implementation("org.apache.maven:maven-core:3.9.6")
implementation("org.apache.maven:maven-core:3.9.9")
implementation("org.codehaus.plexus:plexus-utils:4.0.0")
implementation(project(":lib"))
testImplementation("org.apache.maven.shared:maven-invoker:3.2.0")
testImplementation("org.apache.maven.shared:maven-invoker:3.3.0")
}

tasks.test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ public class DetectExtension extends AbstractMavenLifecycleParticipant {
private final Logger logger;
private final Detector detector;

/**
* Create a Maven extension instance with the platform specific logger.
*
* @param logger the platform specific logger
*/
@Inject
public DetectExtension(final Logger logger) {
this.logger = logger;
Expand All @@ -90,6 +95,18 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
}

private void injectProperties(MavenSession session) throws MavenExecutionException {
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 @@ -113,15 +130,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
Expand Up @@ -65,6 +65,11 @@ public class DetectMojo extends AbstractMojo {
private final Detector detector = new Detector(
new DefaultSystemPropertyOperations(), new DefaultFileOperations(), message -> getLog().info(message));

/**
* Create a mojo instance to detect OS information.
*/
public DetectMojo() {}

@Override
public void execute() throws MojoExecutionException {
try {
Expand Down
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,7 @@
<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>
</components>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class DetectExtensionTest {

@ParameterizedTest
@ValueSource(strings = {"mvn31", "mvn32", "mvn33", "mvn35", "mvn36", "mvn38", "mvn39"})
@ValueSource(strings = {"mvn31", "mvn32", "mvn33", "mvn35", "mvn36", "mvn38", "mvn39", "mvn40beta4"})
void testExtension(String mavenVersion) throws Exception {
final Properties properties = new Properties();
properties.put("os-detector-maven-plugin.version", System.getProperty("project.version"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you 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.
distributionUrl=https://repo.maven.apache.org/maven2/org/apache/maven/apache-maven/4.0.0-beta-4/apache-maven-4.0.0-beta-4-bin.zip
wrapperUrl=https://repo.maven.apache.org/maven2/org/apache/maven/wrapper/maven-wrapper/3.2.0/maven-wrapper-3.2.0.jar
Loading