Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add toolchain java path to environment variables in ExecMojo #455

Merged
18 changes: 18 additions & 0 deletions src/main/java/org/codehaus/mojo/exec/ExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 toolchain feature is used)
michalm2000 marked this conversation as resolved.
Show resolved Hide resolved
*/
slawekjaranowski marked this conversation as resolved.
Show resolved Hide resolved
@Parameter(property = "exec.toolchainJavaHomeEnvName", defaultValue = "TOOLCHAIN_JAVA_HOME")
private String toolchainJavaHomeEnvName = "TOOLCHAIN_JAVA_HOME";

@Component
private ToolchainManager toolchainManager;

Expand Down Expand Up @@ -488,6 +496,16 @@ private Map<String, String> 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<String> keys = new TreeSet<>(enviro.keySet());
for (String key : keys) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/codehaus/mojo/exec/DummyJdkToolchain.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
46 changes: 43 additions & 3 deletions src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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");

Expand Down Expand Up @@ -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);
Expand Down
43 changes: 43 additions & 0 deletions src/test/projects/project20/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.cb.maven.plugins.exec</groupId>
<artifactId>project20</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Maven Exec Plugin</name>
<inceptionYear>2005</inceptionYear>

<licenses>
<license>
<name>Apache License 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<test.name>${project.artifactId} project</test.name>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchainJavaHomeEnvName>CUSTOM_NAME_FOR_TOOLCHAIN_JAVA</toolchainJavaHomeEnvName>
<executable>${basedir}/src/test/projects/project20/testscript.sh</executable>
</configuration>
</plugin>
</plugins>
</build>

</project>
1 change: 1 addition & 0 deletions src/test/projects/project20/testscript.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo $CUSTOM_NAME_FOR_TOOLCHAIN_JAVA > testfile.txt
43 changes: 43 additions & 0 deletions src/test/projects/project21/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<project>
<modelVersion>4.0.0</modelVersion>
<groupId>org.cb.maven.plugins.exec</groupId>
<artifactId>project21</artifactId>
<version>0.1</version>
<packaging>jar</packaging>
<name>Maven Exec Plugin</name>
<inceptionYear>2005</inceptionYear>

<licenses>
<license>
<name>Apache License 2</name>
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
<distribution>repo</distribution>
</license>
</licenses>

<properties>
<test.name>${project.artifactId} project</test.name>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<executions>
<execution>
<phase>test</phase>
<goals>
<goal>exec</goal>
</goals>
</execution>
</executions>
<configuration>
<toolchainJavaHomeEnvName>CUSTOM_NAME_FOR_TOOLCHAIN_JAVA</toolchainJavaHomeEnvName>
<executable>${basedir}/src/test/projects/project21/testscript.bat</executable>
</configuration>
</plugin>
</plugins>
</build>

</project>
1 change: 1 addition & 0 deletions src/test/projects/project21/testscript.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
echo|set /p="123" > test.txt
Loading