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 16cd225 commit 491526a
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 5 deletions.
12 changes: 10 additions & 2 deletions src/main/java/org/codehaus/mojo/exec/ExecMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

/**
* <p>
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -491,7 +496,10 @@ private Map<String, String> 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()) {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/codehaus/mojo/exec/DummyToolchain.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
36 changes: 33 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,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);
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

0 comments on commit 491526a

Please sign in to comment.