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

Restore Maven 4 extension #11

Merged
merged 1 commit into from
Sep 27, 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
1 change: 1 addition & 0 deletions licenserc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ excludes = [
"plugin-gradle/src/main/java/com/tisonkun/os/gradle/OSDetector.java",
"plugin-maven/src/main/java/com/tisonkun/os/maven/DetectExtension.java",
"plugin-maven/src/main/java/com/tisonkun/os/maven/DetectMojo.java",
"plugin-maven/src/main/java/com/tisonkun/os/maven/DetectPropertyContributor.java",
"plugin-maven/src/main/java/com/tisonkun/os/maven/RepositorySessionInjector.java",
]

Expand Down
1 change: 1 addition & 0 deletions plugin-maven/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ tasks.withType<GenerateHelpMojoSourcesTask>().configureEach {
}

dependencies {
compileOnly("org.apache.maven:maven-api-spi:4.0.0-alpha-13")
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.9")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@
@Component(role = AbstractMavenLifecycleParticipant.class, hint = "detect-os")
public class DetectExtension extends AbstractMavenLifecycleParticipant {

private static boolean disable;

/**
* When running in Maven 4, the interpolation of existing projects can be very slow.
* This allows disabling the interpolation of existing projects, as this Maven 4
* extension provides the properties early enough so that they are available for
* interpolation.
*/
public static void disable() {
disable = true;
}

private final Logger logger;
private final Detector detector;

Expand All @@ -95,6 +107,9 @@ public void afterProjectsRead(MavenSession session) throws MavenExecutionExcepti
}

private void injectProperties(MavenSession session) throws MavenExecutionException {
if (disable) {
return;
}
final Map<String, String> dict = getProperties(session);
// Inject the current session.
injectSession(session, dict);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* Copyright 2024 gnodet <[email protected]>
*
* 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 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;

/**
* Set Maven session user 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();

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 @@ -25,5 +25,12 @@
<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>
</component-set>