Skip to content

Commit 168b368

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

File tree

6 files changed

+76
-19
lines changed

6 files changed

+76
-19
lines changed

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.net.URL;
3030
import java.nio.file.Files;
3131
import java.nio.file.Path;
32+
import java.nio.file.Paths;
3233
import java.util.ArrayList;
3334
import java.util.Arrays;
3435
import java.util.Collection;
@@ -339,7 +340,8 @@ public class ExecMojo extends AbstractExecMojo {
339340
private boolean asyncDestroyOnShutdown = true;
340341

341342
/**
342-
* Name of environment variable that will contain path to java executable provided by the toolchain (if the toolchain w
343+
* @since 3.5.0
344+
* Name of environment variable that will contain path to java executable provided by the toolchain (works only if toolchain feature is used)
343345
*/
344346
@Parameter(property = "exec.toolchainJavaHomeEnvName", defaultValue = "TOOLCHAIN_JAVA_HOME")
345347
private String toolchainJavaHomeEnvName = "TOOLCHAIN_JAVA_HOME";
@@ -495,10 +497,12 @@ private Map<String, String> handleSystemEnvVariables() throws MojoExecutionExcep
495497
}
496498

497499
Toolchain tc = getToolchain();
498-
if (tc != null) {
500+
if (tc != null && "jdk".equals(tc.getType())) {
499501
String toolchainJava = tc.findTool("java");
500502
if (toolchainJava != null) {
501-
enviro.put(toolchainJavaHomeEnvName, toolchainJava.replaceFirst("bin/java$", ""));
503+
String toolchainJavaHome =
504+
Paths.get(toolchainJava).getParent().getParent().toString();
505+
enviro.put(toolchainJavaHomeEnvName, toolchainJavaHome);
502506
}
503507
}
504508

src/test/java/org/codehaus/mojo/exec/DummyToolchain.java src/test/java/org/codehaus/mojo/exec/DummyJdkToolchain.java

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
package org.codehaus.mojo.exec;
22

3-
import org.apache.commons.lang3.NotImplementedException;
43
import org.apache.maven.toolchain.Toolchain;
54

6-
public class DummyToolchain implements Toolchain {
5+
public class DummyJdkToolchain implements Toolchain {
76

87
private final String testJavaPath;
98

10-
public DummyToolchain(String testJavaPath) {
9+
public DummyJdkToolchain(String testJavaPath) {
1110
this.testJavaPath = testJavaPath;
1211
}
1312

1413
@Override
1514
public String getType() {
16-
throw new NotImplementedException("testToolchain");
15+
return "jdk";
1716
}
1817

1918
@Override

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

+21-11
Original file line numberDiff line numberDiff line change
@@ -295,21 +295,31 @@ public void test_exec_receives_all_parameters() throws MojoExecutionException {
295295

296296
public void testToolchainJavaHomePropertySetWhenToolchainIsUsed() throws Exception {
297297
// 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);
298+
File basedir;
299+
String testJavaPath;
300+
File pom;
301+
302+
if (OS.isFamilyWindows()) {
303+
testJavaPath = "\\path\\to\\java\\home";
304+
pom = new File(getBasedir(), "src\\test\\projects\\project21\\pom.xml");
305+
when(toolchainManager.getToolchainFromBuildContext(any(), eq(session)))
306+
.thenReturn(new DummyJdkToolchain(testJavaPath + "\\bin\\java"));
307+
} else {
308+
testJavaPath = "/path/to/java/home";
309+
pom = new File(getBasedir(), "src/test/projects/project20/pom.xml");
310+
when(toolchainManager.getToolchainFromBuildContext(any(), eq(session)))
311+
.thenReturn(new DummyJdkToolchain(testJavaPath + "/bin/java"));
312+
}
302313

303-
setVariableValueToObject(mojo, "session", session);
304-
setVariableValueToObject(mojo, "toolchainManager", toolchainManager);
305-
when(toolchainManager.getToolchainFromBuildContext(any(), eq(session)))
306-
.thenReturn(new DummyToolchain(testJavaPath + "/bin/java"));
314+
ExecMojo execMojo = (ExecMojo) lookupMojo("exec", pom);
315+
setVariableValueToObject(execMojo, "session", session);
316+
setVariableValueToObject(execMojo, "toolchainManager", toolchainManager);
307317

308-
File basedir = new File("target");
309-
mojo.setBasedir(basedir);
318+
basedir = new File("target");
319+
execMojo.setBasedir(basedir);
310320

311321
// when
312-
mojo.execute();
322+
execMojo.execute();
313323

314324
// then
315325
Path resultFilePath = basedir.toPath().resolve("testfile.txt");
+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
echo $CUSTOM_NAME_FOR_TOOLCHAIN_JAVA > testfile.txt
1+
echo $CUSTOM_NAME_FOR_TOOLCHAIN_JAVA > testfile.txt

src/test/projects/project21/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>project21</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/project21/testscript.bat</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|set /p="123" > test.txt

0 commit comments

Comments
 (0)