Skip to content

Commit

Permalink
Add toolchain java path to environment variables in ExecMojo - added …
Browse files Browse the repository at this point in the history
…tests and various enhancements
  • Loading branch information
mmazur authored and slawekjaranowski committed Oct 21, 2024
1 parent 491526a commit 168b368
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 19 deletions.
10 changes: 7 additions & 3 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 @@ -339,7 +340,8 @@ public class ExecMojo extends AbstractExecMojo {
private boolean asyncDestroyOnShutdown = true;

/**
* Name of environment variable that will contain path to java executable provided by the toolchain (if the toolchain w
* @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)
*/
@Parameter(property = "exec.toolchainJavaHomeEnvName", defaultValue = "TOOLCHAIN_JAVA_HOME")
private String toolchainJavaHomeEnvName = "TOOLCHAIN_JAVA_HOME";
Expand Down Expand Up @@ -495,10 +497,12 @@ private Map<String, String> handleSystemEnvVariables() throws MojoExecutionExcep
}

Toolchain tc = getToolchain();
if (tc != null) {
if (tc != null && "jdk".equals(tc.getType())) {
String toolchainJava = tc.findTool("java");
if (toolchainJava != null) {
enviro.put(toolchainJavaHomeEnvName, toolchainJava.replaceFirst("bin/java$", ""));
String toolchainJavaHome =
Paths.get(toolchainJava).getParent().getParent().toString();
enviro.put(toolchainJavaHomeEnvName, toolchainJavaHome);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package org.codehaus.mojo.exec;

import org.apache.commons.lang3.NotImplementedException;
import org.apache.maven.toolchain.Toolchain;

public class DummyToolchain implements Toolchain {
public class DummyJdkToolchain implements Toolchain {

private final String testJavaPath;

public DummyToolchain(String testJavaPath) {
public DummyJdkToolchain(String testJavaPath) {
this.testJavaPath = testJavaPath;
}

@Override
public String getType() {
throw new NotImplementedException("testToolchain");
return "jdk";
}

@Override
Expand Down
32 changes: 21 additions & 11 deletions src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -295,21 +295,31 @@ public void test_exec_receives_all_parameters() throws MojoExecutionException {

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);
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"));
}

setVariableValueToObject(mojo, "session", session);
setVariableValueToObject(mojo, "toolchainManager", toolchainManager);
when(toolchainManager.getToolchainFromBuildContext(any(), eq(session)))
.thenReturn(new DummyToolchain(testJavaPath + "/bin/java"));
ExecMojo execMojo = (ExecMojo) lookupMojo("exec", pom);
setVariableValueToObject(execMojo, "session", session);
setVariableValueToObject(execMojo, "toolchainManager", toolchainManager);

File basedir = new File("target");
mojo.setBasedir(basedir);
basedir = new File("target");
execMojo.setBasedir(basedir);

// when
mojo.execute();
execMojo.execute();

// then
Path resultFilePath = basedir.toPath().resolve("testfile.txt");
Expand Down
2 changes: 1 addition & 1 deletion src/test/projects/project20/testscript.sh
Original file line number Diff line number Diff line change
@@ -1 +1 @@
echo $CUSTOM_NAME_FOR_TOOLCHAIN_JAVA > testfile.txt
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

0 comments on commit 168b368

Please sign in to comment.