Skip to content

Commit 2a4a4aa

Browse files
Katrin ShechtmanKatrin Shechtman
authored andcommitted
Merge branch 'use-correct-java' of https://github.com/cstroe/scalatest-maven-plugin into cstroe-use-correct-java
2 parents 39a1124 + fe598ec commit 2a4a4aa

File tree

3 files changed

+53
-22
lines changed

3 files changed

+53
-22
lines changed

src/main/java/org/scalatest/tools/maven/AbstractScalaTestMojo.java

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,21 @@
22

33
import org.apache.maven.artifact.DependencyResolutionRequiredException;
44
import org.apache.maven.plugin.AbstractMojo;
5-
import org.apache.maven.plugin.MojoExecutionException;
65
import org.apache.maven.plugin.MojoFailureException;
76
import org.apache.maven.project.MavenProject;
8-
import org.codehaus.plexus.util.cli.CommandLineException;
9-
import org.codehaus.plexus.util.cli.CommandLineTimeOutException;
10-
import org.codehaus.plexus.util.cli.CommandLineUtils;
11-
import org.codehaus.plexus.util.cli.Commandline;
12-
import org.codehaus.plexus.util.cli.StreamConsumer;
7+
import org.codehaus.plexus.util.cli.*;
138

14-
import static org.scalatest.tools.maven.MojoUtils.*;
15-
16-
import java.io.*;
17-
import java.util.Arrays;
18-
import java.util.Collections;
19-
import java.util.List;
20-
import java.util.ArrayList;
21-
import java.util.Map;
22-
23-
import static java.util.Collections.singletonList;
24-
25-
import java.net.MalformedURLException;
26-
import java.net.URLClassLoader;
27-
import java.net.URL;
9+
import java.io.File;
2810
import java.lang.reflect.InvocationTargetException;
2911
import java.lang.reflect.Method;
12+
import java.net.MalformedURLException;
13+
import java.net.URL;
14+
import java.net.URLClassLoader;
15+
import java.util.*;
16+
17+
import static java.util.Collections.singletonList;
18+
import static org.apache.commons.lang3.StringUtils.isEmpty;
19+
import static org.scalatest.tools.maven.MojoUtils.*;
3020

3121
/**
3222
* Provides the base for all mojos.
@@ -270,7 +260,7 @@ private boolean runForkingOnce(String[] args) throws MojoFailureException {
270260

271261
final Commandline cli = new Commandline();
272262
cli.setWorkingDirectory(project.getBasedir());
273-
cli.setExecutable("java");
263+
cli.setExecutable(getJvm());
274264

275265
// Set up environment
276266
if (environmentVariables != null) {

src/main/java/org/scalatest/tools/maven/MojoUtils.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import java.util.List;
44
import java.util.ArrayList;
55
import java.io.File;
6-
import java.io.IOException;
6+
7+
import static org.apache.commons.lang3.StringUtils.isEmpty;
78

89
/**
910
* Provides internal utilities for the Mojo's operations.
@@ -121,4 +122,12 @@ static String[] concat(List<String>...lists){
121122
}
122123
return c.toArray(new String[c.size()]);
123124
}
125+
126+
static String getJvm() {
127+
if (!isEmpty(System.getProperty( "java.home" ))) {
128+
return System.getProperty( "java.home" ) + File.separator + "bin" + File.separator + "java";
129+
} else {
130+
return "java";
131+
}
132+
}
124133
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.scalatest.tools.maven
2+
3+
import org.junit.{After, Before, Test}
4+
5+
class MojoUtilsTest {
6+
private var savedJavaHome: Option[String] = _
7+
8+
@Before
9+
def save(): Unit = {
10+
savedJavaHome = Option(System.getProperty("java.home"))
11+
}
12+
13+
@After
14+
def restore(): Unit = {
15+
savedJavaHome match {
16+
case None => System.clearProperty("java.home")
17+
case Some(value) => System.setProperty("java.home", value)
18+
}
19+
}
20+
21+
@Test
22+
def getJvmHappyPath(): Unit = {
23+
System.setProperty("java.home", "/test/jvm")
24+
assert(MojoUtils.getJvm == "/test/jvm/bin/java")
25+
}
26+
27+
@Test
28+
def getJvmWithoutJavaHome(): Unit = {
29+
System.clearProperty("java.home")
30+
assert(MojoUtils.getJvm == "java")
31+
}
32+
}

0 commit comments

Comments
 (0)