Skip to content

Commit

Permalink
Fix #401 - Maven v4 compatibility
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski committed Feb 8, 2024
1 parent 198c289 commit 0134866
Show file tree
Hide file tree
Showing 13 changed files with 151 additions and 110 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,5 @@ jobs:
name: Verify
uses: apache/maven-gh-actions-shared/.github/workflows/maven-verify.yml@v3
with:
jdk-matrix: '[ "8", "17", "21" ]'
ff-maven: "3.9.5" # Maven version for fail-fast-build
maven-matrix: '[ "3.6.3", "3.8.8", "3.9.5" ]' # Maven versions matrix for verify builds
ff-maven: "3.9.6" # Maven version for fail-fast-build
maven-matrix: '[ "3.6.3", "3.9.6", "4.0.0-alpha-12" ]' # Maven versions matrix for verify builds
26 changes: 7 additions & 19 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,6 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-annotations</artifactId>
<version>2.2.0</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-exec</artifactId>
Expand Down Expand Up @@ -261,6 +254,9 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<proc>none</proc>
</configuration>
<executions>
<execution>
<id>default-testCompile</id>
Expand Down Expand Up @@ -296,18 +292,6 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-component-metadata</artifactId>
<version>2.2.0</version>
<executions>
<execution>
<goals>
<goal>generate-metadata</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
Expand Down Expand Up @@ -352,6 +336,10 @@
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.eclipse.sisu</groupId>
<artifactId>sisu-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ assert buildLogLines[infoMessageLineNumber - 1] == "[one, two, three]"
// Verify that subsequent lines contain the beginning of the thrown SystemExitException stack trace
assert buildLogLines[infoMessageLineNumber + 1].startsWith("[WARNING]")
assert buildLogLines[infoMessageLineNumber + 2].contains("SystemExitException: System::exit was called with return code 123")
assert buildLogLines[infoMessageLineNumber + 3].contains("SystemExitManager.exit (SystemExitManager.java")
assert buildLogLines[infoMessageLineNumber + 3].matches(".*SystemExitManager.exit ?\\(SystemExitManager.java.*")
5 changes: 5 additions & 0 deletions src/it/projects/setup-parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@
<artifactId>maven-resources-plugin</artifactId>
<version>@maven-resources-plugin.version@</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<version>3.1.0</version>
</plugin>
</plugins>
</pluginManagement>
</build>
Expand Down
1 change: 1 addition & 0 deletions src/it/projects/toolchains-paths/invoker.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
invoker.goals = --toolchains toolchains.xml validate
58 changes: 58 additions & 0 deletions src/it/projects/toolchains-paths/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<project
xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>org.codehaus.mojo.exec.it</groupId>
<artifactId>parent</artifactId>
<version>0.1</version>
</parent>

<artifactId>toolchains-paths</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-toolchains-plugin</artifactId>
<configuration>
<toolchains>
<paths>
<id>my-paths</id>
</paths>
</toolchains>
</configuration>
<executions>
<execution>
<goals>
<goal>toolchain</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>@project.version@</version>
<extensions>true</extensions>
<configuration>
<executable>test</executable>
<toolchain>paths</toolchain>
</configuration>
<executions>
<execution>
<id>execution</id>
<goals>
<goal>exec</goal>
</goals>
<phase>validate</phase>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2 changes: 2 additions & 0 deletions src/it/projects/toolchains-paths/scripts/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#! /bin/sh
echo Hello from exec-maven-plugin
1 change: 1 addition & 0 deletions src/it/projects/toolchains-paths/scripts/test.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo Hello from exec-maven-plugin
14 changes: 14 additions & 0 deletions src/it/projects/toolchains-paths/toolchains.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF8"?>
<toolchains>
<toolchain>
<type>paths</type>
<provides>
<id>my-paths</id>
</provides>
<configuration>
<paths>
<path>scripts</path>
</paths>
</configuration>
</toolchain>
</toolchains>
5 changes: 5 additions & 0 deletions src/it/projects/toolchains-paths/verify.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
File log = new File(basedir, 'build.log')

assert log.exists()
assert log.getText().contains( "[INFO] Toolchain in exec-maven-plugin: Paths[scripts]" )
assert log.getText().contains( "Hello from exec-maven-plugin" )
24 changes: 8 additions & 16 deletions src/main/java/org/codehaus/mojo/exec/ExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@
import org.apache.maven.artifact.resolver.filter.IncludesArtifactFilter;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.plugin.MojoExecutionException;
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.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
import org.codehaus.plexus.util.StringUtils;
import org.codehaus.plexus.util.cli.CommandLineException;
import org.codehaus.plexus.util.cli.CommandLineUtils;
Expand Down Expand Up @@ -344,6 +344,9 @@ public class ExecMojo extends AbstractExecMojo {
@Parameter(property = "exec.asyncDestroyOnShutdown", defaultValue = "true")
private boolean asyncDestroyOnShutdown = true;

@Component
private ToolchainManager toolchainManager;

public static final String CLASSPATH_TOKEN = "%classpath";

public static final String MODULEPATH_TOKEN = "%modulepath";
Expand Down Expand Up @@ -936,22 +939,11 @@ public int[] getSuccessCodes() {
}

private Toolchain getToolchain() {
Toolchain tc = null;

try {
if (session != null) // session is null in tests..
{
ToolchainManager toolchainManager =
(ToolchainManager) session.getContainer().lookup(ToolchainManager.ROLE);

if (toolchainManager != null) {
tc = toolchainManager.getToolchainFromBuildContext(toolchain, session);
}
}
} catch (ComponentLookupException componentLookupException) {
// just ignore, could happen in pre-2.0.9 builds..
// session and toolchainManager can be null in tests ...
if (session != null && toolchainManager != null) {
return toolchainManager.getToolchainFromBuildContext(toolchain, session);
}
return tc;
return null;
}

/**
Expand Down
45 changes: 33 additions & 12 deletions src/main/java/org/codehaus/mojo/exec/PathsToolchain.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,38 +19,59 @@
* under the License.
*/

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.apache.maven.toolchain.DefaultToolchain;
import org.apache.maven.toolchain.ToolchainPrivate;
import org.apache.maven.toolchain.model.ToolchainModel;
import org.codehaus.plexus.logging.Logger;

/**
* Searches a list of configured paths for the requested tool.
*
* @author Markus KARG (markus@headcrashing.eu)
*/
class PathsToolchain extends DefaultToolchain {
class PathsToolchain implements ToolchainPrivate {
private final ToolchainModel model;

private List<String> paths;

public PathsToolchain(final ToolchainModel model, final Logger logger) {
super(model, "paths", logger); // NOI18N
public PathsToolchain(ToolchainModel model) {
this.model = model;
}

@Override
public ToolchainModel getModel() {
return model;
}

public List<String> getPaths() {
return this.paths;
@Override
public String getType() {
return model.getType();
}

public void setPaths(final List<String> paths) {
public void setPaths(List<String> paths) {
this.paths = paths;
}

@Override
public String toString() {
return "Paths" + this.getPaths(); // NOI18N
private List<String> getPaths() {
return paths != null ? paths : Collections.emptyList();
}

@Override
public String findTool(final String toolName) {
return ExecMojo.findExecutable(toolName, this.paths);
return ExecMojo.findExecutable(toolName, getPaths());
}

@Override
public boolean matchesRequirements(Map<String, String> requirements) {
return requirements.entrySet().stream()
.anyMatch(entry -> Objects.equals(model.getProvides().get(entry.getKey()), entry.getValue()));
}

@Override
public String toString() {
return "Paths" + getPaths(); // NOI18N
}
}
Loading

0 comments on commit 0134866

Please sign in to comment.