From 48b646051e35035ee5a5707ca4cda78a4e9dd1a5 Mon Sep 17 00:00:00 2001 From: Scott Stark Date: Tue, 12 Mar 2019 16:32:10 -0700 Subject: [PATCH 1/6] Add toolchaing support to DevMojo with java.home fallback --- bom/pom.xml | 12 ++++ devtools/maven/pom.xml | 4 ++ .../main/java/io/quarkus/maven/DevMojo.java | 61 +++++++++++++++++-- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/bom/pom.xml b/bom/pom.xml index 8c17bb94f3c89..73333afe3887c 100644 --- a/bom/pom.xml +++ b/bom/pom.xml @@ -70,6 +70,7 @@ 11.0.0.Final 3.5.4 1.1.1 + 3.0-alpha-2 27.0.1-jre 3.0.24 3.5.2 @@ -940,6 +941,17 @@ maven-settings-builder ${maven-core.version} + + org.apache.maven + maven-toolchain + ${maven-toolchain.version} + + + org.codehaus.plexus + plexus-classworlds + + + org.apache.maven.plugin-tools diff --git a/devtools/maven/pom.xml b/devtools/maven/pom.xml index 194302607110e..b1c6ee5954541 100644 --- a/devtools/maven/pom.xml +++ b/devtools/maven/pom.xml @@ -46,6 +46,10 @@ org.apache.maven maven-core + + org.apache.maven + maven-toolchain + org.apache.maven.plugin-tools diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index abeb7e6861a33..12f28ae48843f 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -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; @@ -30,8 +34,8 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import io.quarkus.maven.components.MavenVersionEnforcer; 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; @@ -39,12 +43,17 @@ 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; -import io.quarkus.maven.components.MavenVersionEnforcer; import io.quarkus.maven.utilities.MojoUtils; /** @@ -116,6 +125,22 @@ 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; + } + + @SuppressWarnings("UnusedDeclaration") + public void setSession(MavenSession session) { + this.session = session; + } + @Override public void execute() throws MojoFailureException, MojoExecutionException { mavenVersionEnforcer.ensureMavenVersion(getLog(), session); @@ -148,7 +173,9 @@ public void execute() throws MojoFailureException, MojoExecutionException { try { List 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 @@ -302,6 +329,30 @@ public void run() { } } + protected String findJavaTool() { + String java = null; + + if (getToolchainManager() != null) { + Toolchain toolchain = getToolchainManager().getToolchainFromBuildContext("jdk", getSession()); + if (toolchain != null) { + java = toolchain.findTool("java"); + } + getLog().debug("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 = "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(); From 7ce6ac77a65bf76fd132f38624587ee5edaf67c7 Mon Sep 17 00:00:00 2001 From: Scott M Stark Date: Tue, 12 Mar 2019 23:27:07 -0700 Subject: [PATCH 2/6] Add a windows exe search method --- .../main/java/io/quarkus/maven/DevMojo.java | 70 ++++++++++++++++++- 1 file changed, 67 insertions(+), 3 deletions(-) diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index 12f28ae48843f..282ad651e13b4 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -174,7 +174,7 @@ public void execute() throws MojoFailureException, MojoExecutionException { try { List args = new ArrayList<>(); String javaTool = findJavaTool(); - getLog().info("Using javaTool: " + javaTool); + getLog().debug("Using javaTool: " + javaTool); args.add(javaTool); if (debug == null) { // debug mode not specified @@ -332,12 +332,13 @@ public void run() { 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().debug("JVM from toolchain: " + 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) @@ -346,7 +347,30 @@ protected String findJavaTool() { getLog().debug("Checking: " + javaCheck.getAbsolutePath()); if (!javaCheck.canExecute()) { getLog().debug(javaCheck.getAbsolutePath() + " is not executable"); - java = "java"; + + 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); @@ -366,4 +390,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; + } + } + } From ce1e886377b425ed654d5980d347d00610dbe85e Mon Sep 17 00:00:00 2001 From: Scott Stark Date: Wed, 13 Mar 2019 06:44:18 -0700 Subject: [PATCH 3/6] Additional cleanup Additional cleanup --- .../main/java/io/quarkus/maven/DevMojo.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index 282ad651e13b4..4a2c4071addfa 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -34,7 +34,10 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; +import io.quarkus.dev.ClassLoaderCompiler; +import io.quarkus.dev.DevModeMain; import io.quarkus.maven.components.MavenVersionEnforcer; +import io.quarkus.maven.utilities.MojoUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; @@ -52,10 +55,6 @@ import org.apache.maven.toolchain.Toolchain; import org.apache.maven.toolchain.ToolchainManager; -import io.quarkus.dev.ClassLoaderCompiler; -import io.quarkus.dev.DevModeMain; -import io.quarkus.maven.utilities.MojoUtils; - /** * The dev mojo, that runs a quarkus app in a forked process *

@@ -136,11 +135,6 @@ public MavenSession getSession() { return session; } - @SuppressWarnings("UnusedDeclaration") - public void setSession(MavenSession session) { - this.session = session; - } - @Override public void execute() throws MojoFailureException, MojoExecutionException { mavenVersionEnforcer.ensureMavenVersion(getLog(), session); @@ -174,7 +168,7 @@ public void execute() throws MojoFailureException, MojoExecutionException { try { List args = new ArrayList<>(); String javaTool = findJavaTool(); - getLog().debug("Using javaTool: " + javaTool); + getLog().info("Using javaTool: " + javaTool); args.add(javaTool); if (debug == null) { // debug mode not specified @@ -329,6 +323,13 @@ 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; @@ -337,8 +338,8 @@ protected String findJavaTool() { Toolchain toolchain = getToolchainManager().getToolchainFromBuildContext("jdk", getSession()); if (toolchain != null) { java = toolchain.findTool("java"); + getLog().info("JVM from toolchain: " + 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) From babeadfef35d3a4dd883421f6e233e150b9b796f Mon Sep 17 00:00:00 2001 From: Scott Stark Date: Wed, 13 Mar 2019 07:17:13 -0700 Subject: [PATCH 4/6] Rerun formatter --- .../maven/src/main/java/io/quarkus/maven/DevMojo.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index 4a2c4071addfa..a3e4c663cd278 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -152,8 +152,8 @@ public void execute() throws MojoFailureException, MojoExecutionException { } if (!found) { getLog().warn("The quarkus-maven-plugin build goal was not configured for this project, " + - "skipping quarkus:dev as this is assumed to be a support library. If you want to run quarkus dev" + - " on this project make sure the quarkus-maven-plugin is configured with a build goal."); + "skipping quarkus:dev as this is assumed to be a support library. If you want to run quarkus dev" + + " on this project make sure the quarkus-maven-plugin is configured with a build goal."); return; } @@ -173,7 +173,7 @@ public void execute() throws MojoFailureException, MojoExecutionException { 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 - try (Socket socket = new Socket(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }), 5005)) { + try (Socket socket = new Socket(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 5005)) { getLog().error("Port 5005 in use, not starting in debug mode"); } catch (IOException e) { args.add("-Xdebug"); @@ -253,7 +253,7 @@ public void execute() throws MojoFailureException, MojoExecutionException { path = new File(jarPath); } else if (classFile.getProtocol().equals("file")) { String filePath = classFile.getPath().substring(0, - classFile.getPath().lastIndexOf(DevModeMain.class.getName().replace('.', '/'))); + classFile.getPath().lastIndexOf(DevModeMain.class.getName().replace('.', '/'))); path = new File(filePath); } else { throw new MojoFailureException("Unsupported DevModeMain artifact URL:" + classFile); @@ -328,6 +328,7 @@ public void run() { * 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() { From 5b8950d76a8c343ac9f7aa04f8e2e9a214ed549d Mon Sep 17 00:00:00 2001 From: Scott Stark Date: Wed, 13 Mar 2019 08:07:03 -0700 Subject: [PATCH 5/6] Try reformatting again --- .../maven/src/main/java/io/quarkus/maven/DevMojo.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index a3e4c663cd278..265e4ce093e05 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -152,8 +152,8 @@ public void execute() throws MojoFailureException, MojoExecutionException { } if (!found) { getLog().warn("The quarkus-maven-plugin build goal was not configured for this project, " + - "skipping quarkus:dev as this is assumed to be a support library. If you want to run quarkus dev" + - " on this project make sure the quarkus-maven-plugin is configured with a build goal."); + "skipping quarkus:dev as this is assumed to be a support library. If you want to run quarkus dev" + + " on this project make sure the quarkus-maven-plugin is configured with a build goal."); return; } @@ -173,7 +173,7 @@ public void execute() throws MojoFailureException, MojoExecutionException { 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 - try (Socket socket = new Socket(InetAddress.getByAddress(new byte[]{127, 0, 0, 1}), 5005)) { + try (Socket socket = new Socket(InetAddress.getByAddress(new byte[] { 127, 0, 0, 1 }), 5005)) { getLog().error("Port 5005 in use, not starting in debug mode"); } catch (IOException e) { args.add("-Xdebug"); @@ -253,7 +253,7 @@ public void execute() throws MojoFailureException, MojoExecutionException { path = new File(jarPath); } else if (classFile.getProtocol().equals("file")) { String filePath = classFile.getPath().substring(0, - classFile.getPath().lastIndexOf(DevModeMain.class.getName().replace('.', '/'))); + classFile.getPath().lastIndexOf(DevModeMain.class.getName().replace('.', '/'))); path = new File(filePath); } else { throw new MojoFailureException("Unsupported DevModeMain artifact URL:" + classFile); From 9d2e3de5b8815c9b9188f02c800d00c05015108a Mon Sep 17 00:00:00 2001 From: Scott Stark Date: Wed, 13 Mar 2019 08:07:43 -0700 Subject: [PATCH 6/6] Update import ordering --- .../maven/src/main/java/io/quarkus/maven/DevMojo.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java index 265e4ce093e05..7bb34616e305c 100644 --- a/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java +++ b/devtools/maven/src/main/java/io/quarkus/maven/DevMojo.java @@ -34,10 +34,6 @@ import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; -import io.quarkus.dev.ClassLoaderCompiler; -import io.quarkus.dev.DevModeMain; -import io.quarkus.maven.components.MavenVersionEnforcer; -import io.quarkus.maven.utilities.MojoUtils; import org.apache.maven.artifact.Artifact; import org.apache.maven.execution.MavenSession; import org.apache.maven.model.Plugin; @@ -55,6 +51,11 @@ import org.apache.maven.toolchain.Toolchain; import org.apache.maven.toolchain.ToolchainManager; +import io.quarkus.dev.ClassLoaderCompiler; +import io.quarkus.dev.DevModeMain; +import io.quarkus.maven.components.MavenVersionEnforcer; +import io.quarkus.maven.utilities.MojoUtils; + /** * The dev mojo, that runs a quarkus app in a forked process *