You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@maven.apache.org by gn...@apache.org on 2023/05/30 07:58:12 UTC
[maven-toolchains-plugin] 01/01: JDK Toolchains discovery mojos
This is an automated email from the ASF dual-hosted git repository.
gnodet pushed a commit to branch discovered-toolchains
in repository https://gitbox.apache.org/repos/asf/maven-toolchains-plugin.git
commit 93918e25160e3d1988302f3b3b5107a41321c7d8
Author: Guillaume Nodet <gn...@gmail.com>
AuthorDate: Tue May 30 09:58:02 2023 +0200
JDK Toolchains discovery mojos
---
.../toolchain/DisplayDiscoveredToolchainsMojo.java | 62 ++++++++++
.../toolchain/GenerateToolchainsXmlMojo.java | 64 ++++++++++
.../maven/plugins/toolchain/JdkToolchainMojo.java | 132 +++++++++++++++++++++
3 files changed, 258 insertions(+)
diff --git a/src/main/java/org/apache/maven/plugins/toolchain/DisplayDiscoveredToolchainsMojo.java b/src/main/java/org/apache/maven/plugins/toolchain/DisplayDiscoveredToolchainsMojo.java
new file mode 100644
index 0000000..db2f5ca
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/toolchain/DisplayDiscoveredToolchainsMojo.java
@@ -0,0 +1,62 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.toolchain;
+
+import java.util.List;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.toolchain.model.PersistedToolchains;
+import org.apache.maven.toolchain.model.ToolchainModel;
+import org.codehaus.plexus.PlexusContainer;
+import org.codehaus.plexus.util.xml.Xpp3Dom;
+
+@Mojo(name = "display-discovered-toolchains", requiresProject = false)
+public class DisplayDiscoveredToolchainsMojo extends AbstractMojo {
+
+ @Component
+ PlexusContainer container;
+
+ @Component
+ Log log;
+
+ @Override
+ public void execute() throws MojoFailureException {
+ try {
+ Object discoverer = container.lookup("org.apache.maven.toolchain.discovery.ToolchainDiscoverer");
+ PersistedToolchains toolchains = (PersistedToolchains)
+ discoverer.getClass().getMethod("discoverToolchains").invoke(discoverer);
+ List<ToolchainModel> models = toolchains.getToolchains();
+ log.info("Discovered " + models.size() + " JDK toolchains:");
+ for (ToolchainModel model : models) {
+ log.info(" - "
+ + ((Xpp3Dom) model.getConfiguration())
+ .getChild("jdkHome")
+ .getValue());
+ log.info(" provides:");
+ model.getProvides().forEach((k, v) -> log.info(" " + k + ": " + v));
+ }
+ } catch (Exception e) {
+ throw new MojoFailureException("Unable to retrieve discovered toolchains", e);
+ }
+ }
+}
diff --git a/src/main/java/org/apache/maven/plugins/toolchain/GenerateToolchainsXmlMojo.java b/src/main/java/org/apache/maven/plugins/toolchain/GenerateToolchainsXmlMojo.java
new file mode 100644
index 0000000..3e73b8d
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/toolchain/GenerateToolchainsXmlMojo.java
@@ -0,0 +1,64 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.toolchain;
+
+import java.io.StringWriter;
+import java.io.Writer;
+import java.nio.file.Files;
+import java.nio.file.Path;
+
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.toolchain.model.PersistedToolchains;
+import org.apache.maven.toolchain.model.io.xpp3.MavenToolchainsXpp3Writer;
+import org.codehaus.plexus.PlexusContainer;
+
+@Mojo(name = "generate-toolchains-xml", requiresProject = false)
+public class GenerateToolchainsXmlMojo extends AbstractMojo {
+
+ @Component
+ PlexusContainer container;
+
+ @Parameter
+ Path file;
+
+ @Override
+ public void execute() throws MojoFailureException {
+ try {
+ Object discoverer = container.lookup("org.apache.maven.toolchain.discovery.ToolchainDiscoverer");
+ PersistedToolchains toolchains = (PersistedToolchains)
+ discoverer.getClass().getMethod("discoverToolchains").invoke(discoverer);
+ if (file != null) {
+ Files.createDirectories(file.getParent());
+ try (Writer writer = Files.newBufferedWriter(file)) {
+ new MavenToolchainsXpp3Writer().write(writer, toolchains);
+ }
+ } else {
+ StringWriter writer = new StringWriter();
+ new MavenToolchainsXpp3Writer().write(writer, toolchains);
+ System.out.println(writer);
+ }
+ } catch (Exception e) {
+ throw new MojoFailureException("Unable to generate toolchains.xml", e);
+ }
+ }
+}
diff --git a/src/main/java/org/apache/maven/plugins/toolchain/JdkToolchainMojo.java b/src/main/java/org/apache/maven/plugins/toolchain/JdkToolchainMojo.java
new file mode 100644
index 0000000..37fbf0b
--- /dev/null
+++ b/src/main/java/org/apache/maven/plugins/toolchain/JdkToolchainMojo.java
@@ -0,0 +1,132 @@
+/*
+ * 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.
+ */
+package org.apache.maven.plugins.toolchain;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Optional;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.apache.maven.execution.MavenSession;
+import org.apache.maven.plugin.AbstractMojo;
+import org.apache.maven.plugin.MojoFailureException;
+import org.apache.maven.plugin.logging.Log;
+import org.apache.maven.plugins.annotations.Component;
+import org.apache.maven.plugins.annotations.LifecyclePhase;
+import org.apache.maven.plugins.annotations.Mojo;
+import org.apache.maven.plugins.annotations.Parameter;
+import org.apache.maven.toolchain.MisconfiguredToolchainException;
+import org.apache.maven.toolchain.ToolchainManagerPrivate;
+import org.apache.maven.toolchain.ToolchainPrivate;
+
+/**
+ * Check that toolchains requirements are met by currently configured toolchains and
+ * store the selected toolchains in build context for later retrieval by other plugins.
+ *
+ * @author mkleint
+ */
+@Mojo(name = "jdk-toolchain", defaultPhase = LifecyclePhase.VALIDATE)
+public class JdkToolchainMojo extends AbstractMojo {
+
+ public enum JdkMode {
+ /** always ignore the current JDK */
+ Ignore,
+ /** to not use a toolchain if the toolchains that would be selected is the current JDK */
+ IfSame,
+ /** favor the current JDK if it matches the requirements */
+ IfMatch
+ }
+
+ /**
+ */
+ @Component
+ private ToolchainManagerPrivate toolchainManager;
+
+ /**
+ * The current build session instance. This is used for toolchain manager API calls.
+ */
+ @Component
+ private MavenSession session;
+
+ @Component
+ private Log log;
+
+ @Parameter
+ private String version;
+
+ @Parameter
+ private String runtimeName;
+
+ @Parameter
+ private String runtimeVersion;
+
+ @Parameter
+ private String vendor;
+
+ @Parameter(property = "toolchain.jdk.mode", defaultValue = "IfMatch")
+ private JdkMode jdkMode = JdkMode.IfMatch;
+
+ @Override
+ public void execute() throws MojoFailureException {
+ if (version == null && runtimeName == null && runtimeVersion == null && vendor == null) {
+ return;
+ }
+
+ Map<String, String> requirements = new HashMap<>();
+ Optional.ofNullable(version).ifPresent(v -> requirements.put("version", v));
+ Optional.ofNullable(runtimeName).ifPresent(v -> requirements.put("runtime.name", v));
+ Optional.ofNullable(runtimeVersion).ifPresent(v -> requirements.put("runtime.version", v));
+ Optional.ofNullable(vendor).ifPresent(v -> requirements.put("vendor", v));
+
+ Map<String, String> currentRequirements = new HashMap<>(requirements);
+ currentRequirements.put("current", "true");
+
+ if (jdkMode == JdkMode.IfMatch) {
+ List<ToolchainPrivate> toolchains = getToolchains(currentRequirements);
+ if (!toolchains.isEmpty()) {
+ log.info("Not using an external toolchain as the current JDK matches the requirements.");
+ return;
+ }
+ }
+ List<ToolchainPrivate> toolchains = getToolchains(requirements);
+ if (toolchains.isEmpty()) {
+ throw new MojoFailureException("Cannot find matching JDK toolchain definition" + System.lineSeparator()
+ + "Please make sure you define the required toolchains in your ~/.m2/toolchains.xml file.");
+ }
+ ToolchainPrivate toolchain = toolchains.get(0);
+ if (jdkMode == JdkMode.IfSame && toolchain.matchesRequirements(currentRequirements)) {
+ log.info("Not using an external toolchain as the current JDK has been selected.");
+ return;
+ }
+ toolchainManager.storeToolchainToBuildContext(toolchain, session);
+ log.info("Found matching JDK toolchain: " + toolchain);
+ }
+
+ private List<ToolchainPrivate> getToolchains(Map<String, String> requirements) throws MojoFailureException {
+ try {
+ return Stream.of(toolchainManager.getToolchainsForType("jdk", session))
+ .filter(tc -> tc.matchesRequirements(requirements))
+ .collect(Collectors.toList());
+ } catch (MisconfiguredToolchainException e) {
+ throw new MojoFailureException("Unable to retrieve matching toolchains", e);
+ }
+ }
+}