From 491526a211f07e20da5e64029db4b2697c0d64af Mon Sep 17 00:00:00 2001 From: mmazur Date: Mon, 21 Oct 2024 13:43:50 +0200 Subject: [PATCH] Add toolchain java path to environment variables in ExecMojo - added tests and various enhancements --- .../java/org/codehaus/mojo/exec/ExecMojo.java | 12 +++++- .../codehaus/mojo/exec/DummyToolchain.java | 23 ++++++++++ .../org/codehaus/mojo/exec/ExecMojoTest.java | 36 ++++++++++++++-- src/test/projects/project20/pom.xml | 43 +++++++++++++++++++ src/test/projects/project20/testscript.sh | 1 + 5 files changed, 110 insertions(+), 5 deletions(-) create mode 100644 src/test/java/org/codehaus/mojo/exec/DummyToolchain.java create mode 100644 src/test/projects/project20/pom.xml create mode 100755 src/test/projects/project20/testscript.sh diff --git a/src/main/java/org/codehaus/mojo/exec/ExecMojo.java b/src/main/java/org/codehaus/mojo/exec/ExecMojo.java index 52e9999a..e9fb964d 100644 --- a/src/main/java/org/codehaus/mojo/exec/ExecMojo.java +++ b/src/main/java/org/codehaus/mojo/exec/ExecMojo.java @@ -87,7 +87,6 @@ public class ExecMojo extends AbstractExecMojo { * Trying to recognize whether the given {@link #executable} might be a {@code java} binary. */ private static final Pattern ENDS_WITH_JAVA = Pattern.compile("^.*java(\\.exe|\\.bin)?$", Pattern.CASE_INSENSITIVE); - private static final String TOOLCHAIN_JAVA_ENV_NAME = "TOOLCHAIN_JAVA"; /** *

@@ -339,6 +338,12 @@ public class ExecMojo extends AbstractExecMojo { @Parameter(property = "exec.asyncDestroyOnShutdown", defaultValue = "true") private boolean asyncDestroyOnShutdown = true; + /** + * Name of environment variable that will contain path to java executable provided by the toolchain (if the toolchain w + */ + @Parameter(property = "exec.toolchainJavaHomeEnvName", defaultValue = "TOOLCHAIN_JAVA_HOME") + private String toolchainJavaHomeEnvName = "TOOLCHAIN_JAVA_HOME"; + @Component private ToolchainManager toolchainManager; @@ -491,7 +496,10 @@ private Map handleSystemEnvVariables() throws MojoExecutionExcep Toolchain tc = getToolchain(); if (tc != null) { - enviro.put(TOOLCHAIN_JAVA_ENV_NAME, tc.findTool("java")); + String toolchainJava = tc.findTool("java"); + if (toolchainJava != null) { + enviro.put(toolchainJavaHomeEnvName, toolchainJava.replaceFirst("bin/java$", "")); + } } if (this.getLog().isDebugEnabled()) { diff --git a/src/test/java/org/codehaus/mojo/exec/DummyToolchain.java b/src/test/java/org/codehaus/mojo/exec/DummyToolchain.java new file mode 100644 index 00000000..c3dc747d --- /dev/null +++ b/src/test/java/org/codehaus/mojo/exec/DummyToolchain.java @@ -0,0 +1,23 @@ +package org.codehaus.mojo.exec; + +import org.apache.commons.lang3.NotImplementedException; +import org.apache.maven.toolchain.Toolchain; + +public class DummyToolchain implements Toolchain { + + private final String testJavaPath; + + public DummyToolchain(String testJavaPath) { + this.testJavaPath = testJavaPath; + } + + @Override + public String getType() { + throw new NotImplementedException("testToolchain"); + } + + @Override + public String findTool(String s) { + return "java".equals(s) ? testJavaPath : null; + } +} diff --git a/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java b/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java index 61d7052c..b1937ad9 100644 --- a/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java +++ b/src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java @@ -12,7 +12,9 @@ import java.io.File; import java.io.IOException; import java.io.OutputStream; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; +import java.nio.file.Path; import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; @@ -28,17 +30,21 @@ import org.apache.maven.execution.MavenSession; import org.apache.maven.plugin.MojoExecutionException; import org.apache.maven.plugin.testing.AbstractMojoTestCase; -import org.mockito.Mock; +import org.apache.maven.toolchain.ToolchainManager; import static java.util.Collections.emptyMap; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; /** * @author Jerome Lacoste * @version $Id$ */ public class ExecMojoTest extends AbstractMojoTestCase { - @Mock - private MavenSession session; + private MavenSession session = mock(MavenSession.class); + private ToolchainManager toolchainManager = mock(ToolchainManager.class); private static final File LOCAL_REPO = new File("src/test/repository"); @@ -287,6 +293,30 @@ public void test_exec_receives_all_parameters() throws MojoExecutionException { Paths.get("target", "dist", "mails").toFile().exists()); } + public void testToolchainJavaHomePropertySetWhenToolchainIsUsed() throws Exception { + // given + String testJavaPath = "/path/to/java/home"; + + File pom = new File(getBasedir(), "src/test/projects/project20/pom.xml"); + ExecMojo mojo = (ExecMojo) lookupMojo("exec", pom); + + setVariableValueToObject(mojo, "session", session); + setVariableValueToObject(mojo, "toolchainManager", toolchainManager); + when(toolchainManager.getToolchainFromBuildContext(any(), eq(session))) + .thenReturn(new DummyToolchain(testJavaPath + "/bin/java")); + + File basedir = new File("target"); + mojo.setBasedir(basedir); + + // when + mojo.execute(); + + // then + Path resultFilePath = basedir.toPath().resolve("testfile.txt"); + String result = new String(Files.readAllBytes(resultFilePath), StandardCharsets.UTF_8); + assertTrue(result.contains(testJavaPath)); + } + private void checkMojo(String expectedCommandLine) { assertEquals(1, mojo.getAmountExecutedCommandLines()); CommandLine commandline = mojo.getExecutedCommandline(0); diff --git a/src/test/projects/project20/pom.xml b/src/test/projects/project20/pom.xml new file mode 100644 index 00000000..14046809 --- /dev/null +++ b/src/test/projects/project20/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + org.cb.maven.plugins.exec + project20 + 0.1 + jar + Maven Exec Plugin + 2005 + + + + Apache License 2 + http://www.apache.org/licenses/LICENSE-2.0.txt + repo + + + + + ${project.artifactId} project + + + + + + org.codehaus.mojo + exec-maven-plugin + + + test + + exec + + + + + CUSTOM_NAME_FOR_TOOLCHAIN_JAVA + ${basedir}/src/test/projects/project20/testscript.sh + + + + + + diff --git a/src/test/projects/project20/testscript.sh b/src/test/projects/project20/testscript.sh new file mode 100755 index 00000000..965695c9 --- /dev/null +++ b/src/test/projects/project20/testscript.sh @@ -0,0 +1 @@ +echo $CUSTOM_NAME_FOR_TOOLCHAIN_JAVA > testfile.txt \ No newline at end of file