diff --git a/src/main/java/org/codehaus/mojo/exec/ExecMojo.java b/src/main/java/org/codehaus/mojo/exec/ExecMojo.java index 740fb301..9c7969b7 100644 --- a/src/main/java/org/codehaus/mojo/exec/ExecMojo.java +++ b/src/main/java/org/codehaus/mojo/exec/ExecMojo.java @@ -29,6 +29,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; @@ -338,6 +339,13 @@ public class ExecMojo extends AbstractExecMojo { @Parameter(property = "exec.asyncDestroyOnShutdown", defaultValue = "true") private boolean asyncDestroyOnShutdown = true; + /** + * @since 3.5.0 + * Name of environment variable that will contain path to java executable provided by the toolchain (works only if JDK toolchain feature is used) + */ + @Parameter(property = "exec.toolchainJavaHomeEnvName", defaultValue = "TOOLCHAIN_JAVA_HOME") + private String toolchainJavaHomeEnvName = "TOOLCHAIN_JAVA_HOME"; + @Component private ToolchainManager toolchainManager; @@ -488,6 +496,16 @@ private Map handleSystemEnvVariables() throws MojoExecutionExcep } } + Toolchain tc = getToolchain(); + if (tc != null && "jdk".equals(tc.getType())) { + String toolchainJava = tc.findTool("java"); + if (toolchainJava != null) { + String toolchainJavaHome = + Paths.get(toolchainJava).getParent().getParent().toString(); + enviro.put(toolchainJavaHomeEnvName, toolchainJavaHome); + } + } + if (this.getLog().isDebugEnabled()) { Set keys = new TreeSet<>(enviro.keySet()); for (String key : keys) { diff --git a/src/test/java/org/codehaus/mojo/exec/DummyJdkToolchain.java b/src/test/java/org/codehaus/mojo/exec/DummyJdkToolchain.java new file mode 100644 index 00000000..17534c34 --- /dev/null +++ b/src/test/java/org/codehaus/mojo/exec/DummyJdkToolchain.java @@ -0,0 +1,22 @@ +package org.codehaus.mojo.exec; + +import org.apache.maven.toolchain.Toolchain; + +public class DummyJdkToolchain implements Toolchain { + + private final String testJavaPath; + + public DummyJdkToolchain(String testJavaPath) { + this.testJavaPath = testJavaPath; + } + + @Override + public String getType() { + return "jdk"; + } + + @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..651e5302 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,40 @@ public void test_exec_receives_all_parameters() throws MojoExecutionException { Paths.get("target", "dist", "mails").toFile().exists()); } + public void testToolchainJavaHomePropertySetWhenToolchainIsUsed() throws Exception { + // given + File basedir; + String testJavaPath; + File pom; + + if (OS.isFamilyWindows()) { + testJavaPath = "\\path\\to\\java\\home"; + pom = new File(getBasedir(), "src\\test\\projects\\project21\\pom.xml"); + when(toolchainManager.getToolchainFromBuildContext(any(), eq(session))) + .thenReturn(new DummyJdkToolchain(testJavaPath + "\\bin\\java")); + } else { + testJavaPath = "/path/to/java/home"; + pom = new File(getBasedir(), "src/test/projects/project20/pom.xml"); + when(toolchainManager.getToolchainFromBuildContext(any(), eq(session))) + .thenReturn(new DummyJdkToolchain(testJavaPath + "/bin/java")); + } + + ExecMojo execMojo = (ExecMojo) lookupMojo("exec", pom); + setVariableValueToObject(execMojo, "session", session); + setVariableValueToObject(execMojo, "toolchainManager", toolchainManager); + + basedir = new File("target"); + execMojo.setBasedir(basedir); + + // when + execMojo.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..0149d707 --- /dev/null +++ b/src/test/projects/project20/testscript.sh @@ -0,0 +1 @@ +echo $CUSTOM_NAME_FOR_TOOLCHAIN_JAVA > testfile.txt diff --git a/src/test/projects/project21/pom.xml b/src/test/projects/project21/pom.xml new file mode 100644 index 00000000..b4753a07 --- /dev/null +++ b/src/test/projects/project21/pom.xml @@ -0,0 +1,43 @@ + + 4.0.0 + org.cb.maven.plugins.exec + project21 + 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/project21/testscript.bat + + + + + + diff --git a/src/test/projects/project21/testscript.bat b/src/test/projects/project21/testscript.bat new file mode 100755 index 00000000..58f8ee35 --- /dev/null +++ b/src/test/projects/project21/testscript.bat @@ -0,0 +1 @@ +echo %CUSTOM_NAME_FOR_TOOLCHAIN_JAVA% > testfile.txt