Skip to content

Commit

Permalink
Switch to v4 api (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
gnodet committed May 29, 2023
1 parent 606c27e commit 20b679e
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 188 deletions.
30 changes: 20 additions & 10 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ under the License.
</parent>

<artifactId>maven-toolchains-plugin</artifactId>
<version>3.1.1-SNAPSHOT</version>
<version>4.0.0-SNAPSHOT</version>
<packaging>maven-plugin</packaging>

<name>Apache Maven Toolchains Plugin</name>
Expand Down Expand Up @@ -67,33 +67,43 @@ under the License.
</distributionManagement>

<properties>
<javaVersion>8</javaVersion>
<mavenVersion>3.2.5</mavenVersion>
<maven.compiler.target>11</maven.compiler.target>
<mavenVersion>4.0.0-alpha-5</mavenVersion>
<project.build.outputTimestamp>2022-06-18T21:03:59Z</project.build.outputTimestamp>
</properties>

<dependencies>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-plugin-api</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
<artifactId>maven-api-core</artifactId>
<version>${mavenVersion}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
<artifactId>maven-plugin-annotations</artifactId>
<version>3.8.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.sisu</groupId>
<artifactId>org.eclipse.sisu.plexus</artifactId>
<version>0.9.0.M2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.8.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-invoker-plugin</artifactId>
<version>3.5.0</version>
</plugin>
<plugin>
<groupId>com.diffplug.spotless</groupId>
<artifactId>spotless-maven-plugin</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion src/it/missing-toolchain/verify.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@

content = new File( basedir, 'build.log' ).text

assert content.indexOf( "MojoFailureException: Cannot find matching toolchain definitions for the following toolchain types:" ) > 0
assert content.indexOf( "MojoException: Cannot find matching toolchain definitions for the following toolchain types:" ) > 0
4 changes: 2 additions & 2 deletions src/it/setup-custom-toolchain/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ under the License.
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>1.5.5</version>
<version>2.1.1</version>
<executions>
<execution>
<goals>
Expand All @@ -92,7 +92,7 @@ under the License.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-plugin-plugin</artifactId>
<version>3.3</version>
<version>3.8.1</version>
<executions>
<execution>
<id>default-descriptor</id>
Expand Down

This file was deleted.

71 changes: 38 additions & 33 deletions src/main/java/org/apache/maven/plugins/toolchain/ToolchainMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,42 +22,42 @@
import java.util.List;
import java.util.Map;

import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
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;
import org.apache.maven.api.Session;
import org.apache.maven.api.Toolchain;
import org.apache.maven.api.plugin.Log;
import org.apache.maven.api.plugin.MojoException;
import org.apache.maven.api.plugin.annotations.Component;
import org.apache.maven.api.plugin.annotations.LifecyclePhase;
import org.apache.maven.api.plugin.annotations.Mojo;
import org.apache.maven.api.plugin.annotations.Parameter;
import org.apache.maven.api.services.ToolchainManager;
import org.apache.maven.api.services.ToolchainManagerException;
import org.apache.maven.api.xml.XmlNode;

/**
* 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 = "toolchain",
defaultPhase = LifecyclePhase.VALIDATE,
configurator = "toolchains-requirement-configurator",
threadSafe = true)
public class ToolchainMojo extends AbstractMojo {
@Mojo(name = "toolchain", defaultPhase = LifecyclePhase.VALIDATE)
public class ToolchainMojo implements org.apache.maven.api.plugin.Mojo {

private static final Object LOCK = new Object();

/**
*/
@Component
private ToolchainManagerPrivate toolchainManagerPrivate;
private ToolchainManager toolchainManager;

/**
* The current build session instance. This is used for toolchain manager API calls.
*/
@Parameter(defaultValue = "${session}", readonly = true, required = true)
private MavenSession session;
@Component
private Session session;

@Component
private Log log;

/**
* Toolchains requirements, specified by one
Expand All @@ -68,16 +68,18 @@ public class ToolchainMojo extends AbstractMojo {
* element for each required toolchain.
*/
@Parameter(required = true)
private ToolchainsRequirement toolchains;
private XmlNode toolchains;

@Override
public void execute() throws MojoExecutionException, MojoFailureException {
public void execute() throws MojoException {
if (toolchains == null) {
// should not happen since parameter is required...
getLog().warn("No toolchains requirements configured.");
return;
}

ToolchainsRequirement toolchains = new ToolchainsRequirement(this.toolchains);

List<String> nonMatchedTypes = new ArrayList<>();

for (Map.Entry<String, Map<String, String>> entry :
Expand All @@ -101,7 +103,7 @@ public void execute() throws MojoExecutionException, MojoFailureException {

getLog().error(buff.toString());

throw new MojoFailureException(buff.toString() + System.lineSeparator()
throw new MojoException(buff.toString() + System.lineSeparator()
+ "Please make sure you define the required toolchains in your ~/.m2/toolchains.xml file.");
}
}
Expand All @@ -111,7 +113,7 @@ protected String getToolchainRequirementAsString(String type, Map<String, String

buff.append(type).append(" [");

if (params.size() == 0) {
if (params == null || params.size() == 0) {
buff.append(" any");
} else {
for (Map.Entry<String, String> param : params.entrySet()) {
Expand All @@ -125,14 +127,14 @@ protected String getToolchainRequirementAsString(String type, Map<String, String
return buff.toString();
}

protected boolean selectToolchain(String type, Map<String, String> params) throws MojoExecutionException {
protected boolean selectToolchain(String type, Map<String, String> params) throws MojoException {
getLog().info("Required toolchain: " + getToolchainRequirementAsString(type, params));
int typeFound = 0;

try {
ToolchainPrivate[] tcs = getToolchains(type);
List<Toolchain> tcs = getToolchains(type);

for (ToolchainPrivate tc : tcs) {
for (Toolchain tc : tcs) {
if (!type.equals(tc.getType())) {
// useful because of MNG-5716
continue;
Expand All @@ -145,14 +147,14 @@ protected boolean selectToolchain(String type, Map<String, String> params) throw

// store matching toolchain to build context
synchronized (LOCK) {
toolchainManagerPrivate.storeToolchainToBuildContext(tc, session);
toolchainManager.storeToolchainToBuildContext(session, tc);
}

return true;
}
}
} catch (MisconfiguredToolchainException ex) {
throw new MojoExecutionException("Misconfigured toolchains.", ex);
} catch (ToolchainManagerException ex) {
throw new MojoException("Misconfigured toolchains.", ex);
}

getLog().error("No toolchain " + ((typeFound == 0) ? "found" : ("matched from " + typeFound + " found"))
Expand All @@ -161,8 +163,11 @@ protected boolean selectToolchain(String type, Map<String, String> params) throw
return false;
}

private ToolchainPrivate[] getToolchains(String type)
throws MojoExecutionException, MisconfiguredToolchainException {
return toolchainManagerPrivate.getToolchainsForType(type, session);
private List<Toolchain> getToolchains(String type) {
return toolchainManager.getToolchainsForType(session, type);
}

public Log getLog() {
return log;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,37 @@
package org.apache.maven.plugins.toolchain;

import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import org.apache.maven.api.xml.XmlNode;

/**
* Type for plugin's <code>toolchain</code> attribute representing toolchains requirements.
*
* @author mkleint
* @see ToolchainConverter the custom Plexus converter to instantiate this class
*/
public final class ToolchainsRequirement {
Map<String, Map<String, String>> toolchains;

public ToolchainsRequirement(XmlNode node) {
Map<String, Map<String, String>> toolchains = new HashMap<>();
for (XmlNode child : node.getChildren()) {
Map<String, String> cfg = new LinkedHashMap<>();
for (XmlNode e : child.getChildren()) {
cfg.put(e.getName(), e.getValue());
}
toolchains.put(child.getName(), cfg);
}
this.toolchains = toolchains;
}

public ToolchainsRequirement(Map<String, Map<String, String>> toolchains) {
this.toolchains = toolchains;
}

public Map<String, Map<String, String>> getToolchains() {
return Collections.unmodifiableMap(toolchains);
}
Expand Down
Loading

0 comments on commit 20b679e

Please sign in to comment.