Skip to content

Commit 491526a

Browse files
mmazurslawekjaranowski
mmazur
authored andcommitted
Add toolchain java path to environment variables in ExecMojo - added tests and various enhancements
1 parent 16cd225 commit 491526a

File tree

5 files changed

+110
-5
lines changed

5 files changed

+110
-5
lines changed

src/main/java/org/codehaus/mojo/exec/ExecMojo.java

+10-2
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,6 @@ public class ExecMojo extends AbstractExecMojo {
8787
* Trying to recognize whether the given {@link #executable} might be a {@code java} binary.
8888
*/
8989
private static final Pattern ENDS_WITH_JAVA = Pattern.compile("^.*java(\\.exe|\\.bin)?$", Pattern.CASE_INSENSITIVE);
90-
private static final String TOOLCHAIN_JAVA_ENV_NAME = "TOOLCHAIN_JAVA";
9190

9291
/**
9392
* <p>
@@ -339,6 +338,12 @@ public class ExecMojo extends AbstractExecMojo {
339338
@Parameter(property = "exec.asyncDestroyOnShutdown", defaultValue = "true")
340339
private boolean asyncDestroyOnShutdown = true;
341340

341+
/**
342+
* Name of environment variable that will contain path to java executable provided by the toolchain (if the toolchain w
343+
*/
344+
@Parameter(property = "exec.toolchainJavaHomeEnvName", defaultValue = "TOOLCHAIN_JAVA_HOME")
345+
private String toolchainJavaHomeEnvName = "TOOLCHAIN_JAVA_HOME";
346+
342347
@Component
343348
private ToolchainManager toolchainManager;
344349

@@ -491,7 +496,10 @@ private Map<String, String> handleSystemEnvVariables() throws MojoExecutionExcep
491496

492497
Toolchain tc = getToolchain();
493498
if (tc != null) {
494-
enviro.put(TOOLCHAIN_JAVA_ENV_NAME, tc.findTool("java"));
499+
String toolchainJava = tc.findTool("java");
500+
if (toolchainJava != null) {
501+
enviro.put(toolchainJavaHomeEnvName, toolchainJava.replaceFirst("bin/java$", ""));
502+
}
495503
}
496504

497505
if (this.getLog().isDebugEnabled()) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package org.codehaus.mojo.exec;
2+
3+
import org.apache.commons.lang3.NotImplementedException;
4+
import org.apache.maven.toolchain.Toolchain;
5+
6+
public class DummyToolchain implements Toolchain {
7+
8+
private final String testJavaPath;
9+
10+
public DummyToolchain(String testJavaPath) {
11+
this.testJavaPath = testJavaPath;
12+
}
13+
14+
@Override
15+
public String getType() {
16+
throw new NotImplementedException("testToolchain");
17+
}
18+
19+
@Override
20+
public String findTool(String s) {
21+
return "java".equals(s) ? testJavaPath : null;
22+
}
23+
}

src/test/java/org/codehaus/mojo/exec/ExecMojoTest.java

+33-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
import java.io.File;
1313
import java.io.IOException;
1414
import java.io.OutputStream;
15+
import java.nio.charset.StandardCharsets;
1516
import java.nio.file.Files;
17+
import java.nio.file.Path;
1618
import java.nio.file.Paths;
1719
import java.util.ArrayList;
1820
import java.util.Arrays;
@@ -28,17 +30,21 @@
2830
import org.apache.maven.execution.MavenSession;
2931
import org.apache.maven.plugin.MojoExecutionException;
3032
import org.apache.maven.plugin.testing.AbstractMojoTestCase;
31-
import org.mockito.Mock;
33+
import org.apache.maven.toolchain.ToolchainManager;
3234

3335
import static java.util.Collections.emptyMap;
36+
import static org.mockito.ArgumentMatchers.any;
37+
import static org.mockito.ArgumentMatchers.eq;
38+
import static org.mockito.Mockito.mock;
39+
import static org.mockito.Mockito.when;
3440

3541
/**
3642
* @author Jerome Lacoste
3743
* @version $Id$
3844
*/
3945
public class ExecMojoTest extends AbstractMojoTestCase {
40-
@Mock
41-
private MavenSession session;
46+
private MavenSession session = mock(MavenSession.class);
47+
private ToolchainManager toolchainManager = mock(ToolchainManager.class);
4248

4349
private static final File LOCAL_REPO = new File("src/test/repository");
4450

@@ -287,6 +293,30 @@ public void test_exec_receives_all_parameters() throws MojoExecutionException {
287293
Paths.get("target", "dist", "mails").toFile().exists());
288294
}
289295

296+
public void testToolchainJavaHomePropertySetWhenToolchainIsUsed() throws Exception {
297+
// given
298+
String testJavaPath = "/path/to/java/home";
299+
300+
File pom = new File(getBasedir(), "src/test/projects/project20/pom.xml");
301+
ExecMojo mojo = (ExecMojo) lookupMojo("exec", pom);
302+
303+
setVariableValueToObject(mojo, "session", session);
304+
setVariableValueToObject(mojo, "toolchainManager", toolchainManager);
305+
when(toolchainManager.getToolchainFromBuildContext(any(), eq(session)))
306+
.thenReturn(new DummyToolchain(testJavaPath + "/bin/java"));
307+
308+
File basedir = new File("target");
309+
mojo.setBasedir(basedir);
310+
311+
// when
312+
mojo.execute();
313+
314+
// then
315+
Path resultFilePath = basedir.toPath().resolve("testfile.txt");
316+
String result = new String(Files.readAllBytes(resultFilePath), StandardCharsets.UTF_8);
317+
assertTrue(result.contains(testJavaPath));
318+
}
319+
290320
private void checkMojo(String expectedCommandLine) {
291321
assertEquals(1, mojo.getAmountExecutedCommandLines());
292322
CommandLine commandline = mojo.getExecutedCommandline(0);

src/test/projects/project20/pom.xml

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<project>
2+
<modelVersion>4.0.0</modelVersion>
3+
<groupId>org.cb.maven.plugins.exec</groupId>
4+
<artifactId>project20</artifactId>
5+
<version>0.1</version>
6+
<packaging>jar</packaging>
7+
<name>Maven Exec Plugin</name>
8+
<inceptionYear>2005</inceptionYear>
9+
10+
<licenses>
11+
<license>
12+
<name>Apache License 2</name>
13+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
14+
<distribution>repo</distribution>
15+
</license>
16+
</licenses>
17+
18+
<properties>
19+
<test.name>${project.artifactId} project</test.name>
20+
</properties>
21+
22+
<build>
23+
<plugins>
24+
<plugin>
25+
<groupId>org.codehaus.mojo</groupId>
26+
<artifactId>exec-maven-plugin</artifactId>
27+
<executions>
28+
<execution>
29+
<phase>test</phase>
30+
<goals>
31+
<goal>exec</goal>
32+
</goals>
33+
</execution>
34+
</executions>
35+
<configuration>
36+
<toolchainJavaHomeEnvName>CUSTOM_NAME_FOR_TOOLCHAIN_JAVA</toolchainJavaHomeEnvName>
37+
<executable>${basedir}/src/test/projects/project20/testscript.sh</executable>
38+
</configuration>
39+
</plugin>
40+
</plugins>
41+
</build>
42+
43+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
echo $CUSTOM_NAME_FOR_TOOLCHAIN_JAVA > testfile.txt

0 commit comments

Comments
 (0)