Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve the java executable location logic, #241 #1452

Merged
merged 6 commits into from
Mar 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
<jboss-metadata-web.version>11.0.0.Final</jboss-metadata-web.version>
<maven-core.version>3.5.4</maven-core.version>
<maven-resolver.version>1.1.1</maven-resolver.version>
<maven-toolchain.version>3.0-alpha-2</maven-toolchain.version>
<guava.version>27.0.1-jre</guava.version>
<plexus-utils.version>3.0.24</plexus-utils.version>
<maven-plugin-annotations.version>3.5.2</maven-plugin-annotations.version>
Expand Down Expand Up @@ -940,6 +941,17 @@
<artifactId>maven-settings-builder</artifactId>
<version>${maven-core.version}</version>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-toolchain</artifactId>
<version>${maven-toolchain.version}</version>
<exclusions>
<exclusion>
<groupId>org.codehaus.plexus</groupId>
<artifactId>plexus-classworlds</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
Expand Down
4 changes: 4 additions & 0 deletions devtools/maven/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@
<groupId>org.apache.maven</groupId>
<artifactId>maven-core</artifactId>
</dependency>
<dependency>
<groupId>org.apache.maven</groupId>
<artifactId>maven-toolchain</artifactId>
</dependency>

<dependency>
<groupId>org.apache.maven.plugin-tools</groupId>
Expand Down
126 changes: 122 additions & 4 deletions devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -31,16 +35,21 @@
import java.util.zip.ZipOutputStream;

import org.apache.maven.artifact.Artifact;
import org.apache.maven.artifact.versioning.DefaultArtifactVersion;
import org.apache.maven.execution.MavenSession;
import org.apache.maven.model.Plugin;
import org.apache.maven.model.PluginExecution;
import org.apache.maven.model.Resource;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.plugin.MojoFailureException;
import org.apache.maven.plugins.annotations.*;
import org.apache.maven.plugins.annotations.Component;
import org.apache.maven.plugins.annotations.LifecyclePhase;
import org.apache.maven.plugins.annotations.Mojo;
import org.apache.maven.plugins.annotations.Parameter;
import org.apache.maven.plugins.annotations.ResolutionScope;
import org.apache.maven.project.MavenProject;
import org.apache.maven.toolchain.Toolchain;
import org.apache.maven.toolchain.ToolchainManager;

import io.quarkus.dev.ClassLoaderCompiler;
import io.quarkus.dev.DevModeMain;
Expand Down Expand Up @@ -116,6 +125,17 @@ public class DevMojo extends AbstractMojo {
@Parameter(defaultValue = "${preventnoverify}")
private boolean preventnoverify = false;

@Component
private ToolchainManager toolchainManager;

public ToolchainManager getToolchainManager() {
return toolchainManager;
}

public MavenSession getSession() {
return session;
}

@Override
public void execute() throws MojoFailureException, MojoExecutionException {
mavenVersionEnforcer.ensureMavenVersion(getLog(), session);
Expand Down Expand Up @@ -148,7 +168,9 @@ public void execute() throws MojoFailureException, MojoExecutionException {

try {
List<String> args = new ArrayList<>();
args.add("java");
String javaTool = findJavaTool();
getLog().info("Using javaTool: " + javaTool);
args.add(javaTool);
if (debug == null) {
// debug mode not specified
// make sure 5005 is not used, we don't want to just fail if something else is using it
Expand Down Expand Up @@ -302,6 +324,62 @@ public void run() {
}
}

/**
* Search for the java command in the order:
* 1. maven-toolchains plugin configuration
* 2. java.home location
* 3. java[.exe] on the system path
*
* @return the java command to use
*/
protected String findJavaTool() {
String java = null;

// See if a toolchain is configured
if (getToolchainManager() != null) {
Toolchain toolchain = getToolchainManager().getToolchainFromBuildContext("jdk", getSession());
if (toolchain != null) {
java = toolchain.findTool("java");
getLog().info("JVM from toolchain: " + java);
}
}
if (java == null) {
// use the same JVM as the one used to run Maven (the "java.home" one)
java = System.getProperty("java.home") + File.separator + "bin" + File.separator + "java";
File javaCheck = new File(java);
getLog().debug("Checking: " + javaCheck.getAbsolutePath());
if (!javaCheck.canExecute()) {
getLog().debug(javaCheck.getAbsolutePath() + " is not executable");

java = null;
// Try executable extensions if windows
if (OS.determineOS() == OS.WINDOWS && System.getenv().containsKey("PATHEXT")) {
String extpath = System.getenv("PATHEXT");
String[] exts = extpath.split(";");
for (String ext : exts) {
File winExe = new File(javaCheck.getAbsolutePath() + ext);
getLog().debug("Checking: " + winExe.getAbsolutePath());
if (winExe.canExecute()) {
java = winExe.getAbsolutePath();
getLog().debug("Executable: " + winExe.getAbsolutePath());
break;
}
}
}
// Fallback to java on the path
if (java == null) {
if (OS.determineOS() == OS.WINDOWS) {
java = "java.exe";
} else {
java = "java";
}
}
}
}
getLog().debug("findJavaTool, selected JVM: " + java);
return java;
}

private void addToClassPaths(StringBuilder classPathManifest, StringBuilder classPath, File file)
throws MalformedURLException {
URI uri = file.toPath().toAbsolutePath().toUri();
Expand All @@ -315,4 +393,44 @@ private void addToClassPaths(StringBuilder classPathManifest, StringBuilder clas
classPath.append(" ");
}

/**
* Enum to classify the os.name system property
*/
static enum OS {
WINDOWS, LINUX, MAC, OTHER;

private String version;

public String getVersion() {
return version;
}

public void setVersion(String version) {
this.version = version;
}

static OS determineOS() {
OS os = OS.OTHER;
String osName = System.getProperty("os.name");
osName = osName.toLowerCase();
if (osName.contains("windows")) {
os = OS.WINDOWS;
} else if (osName.contains("linux")
|| osName.contains("freebsd")
|| osName.contains("unix")
|| osName.contains("sunos")
|| osName.contains("solaris")
|| osName.contains("aix")) {
os = OS.LINUX;
} else if (osName.contains("mac os")) {
os = OS.MAC;
} else {
os = OS.OTHER;
}

os.setVersion(System.getProperty("os.version"));
return os;
}
}

}