diff --git a/README.md b/README.md index 40bebe9..3bd817d 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,15 @@ # jshell-gradle-plugin -This gradle plugin helps you to explore your code and dependency in your gradle project with in jshell -- the official Java REPL tool. + +This gradle plugin helps you to explore your code and dependencies in your gradle project +with in [jshell](https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm) -- the official Java REPL tool. Hosted on https://plugins.gradle.org/plugin/net.java.openjdk.shinyafox.jshell.gradle.plugin + ## Getting started + To use this plugin, add following to your `build.gradle`: + ``` buildscript { repositories { @@ -31,11 +36,22 @@ plugins { Task `jshell` is now enabled, which execute jshell with your classes and dependencies after compiling your code. -Currently we need to run the task with some hacks for JDK9. -Add a path for your jdk9 to `JAVA_HOME` and some options to JAVA_OPTS, and run task `jshell` with `--no-daemon --console plain` for gradle options. +You need to run the task `jshell` with the options `--no-daemon --console plain`. Following is an example with gradlew: + ``` -JAVA_HOME=/path/to/your/jdk9 \ -JAVA_OPTS="--add-exports jdk.jshell/jdk.internal.jshell.tool=ALL-UNNAMED --add-opens java.base/java.lang=ALL-UNNAMED"\ ./gradlew --no-daemon --console plain jshell ``` + +If you see this warning and the jshell console does not detect your classes: + +> :jshell task :classes not found, be sure to compile the project first + +Means the `classes` task needed to compile your project before launch `jshell` +does not exist, just append the task needed to compile the project, +some time is the same `classes` task but is not detected in multi-modules +projects, so you need to add it explicitly in the Gradle command: + +``` +./gradlew --no-daemon --console plain classes jshell +``` diff --git a/src/main/groovy/net/java/openjdk/shinyafox/jshell/gradle/plugin/JShellGradlePlugin.groovy b/src/main/groovy/net/java/openjdk/shinyafox/jshell/gradle/plugin/JShellGradlePlugin.groovy index 3308414..1aef91f 100644 --- a/src/main/groovy/net/java/openjdk/shinyafox/jshell/gradle/plugin/JShellGradlePlugin.groovy +++ b/src/main/groovy/net/java/openjdk/shinyafox/jshell/gradle/plugin/JShellGradlePlugin.groovy @@ -1,32 +1,36 @@ package net.java.openjdk.shinyafox.jshell.gradle.plugin -import java.io.File -import jdk.internal.jshell.tool.JShellToolProvider - +import jdk.jshell.tool.JavaShellToolBuilder import org.gradle.api.Plugin import org.gradle.api.Project -import org.gradle.api.tasks.compile.JavaCompile - -import java.nio.file.Files -import java.nio.file.Paths +import org.gradle.api.tasks.JavaExec class JShellGradlePlugin implements Plugin { @Override void apply(Project project) { - project.tasks.create('jshell') - .dependsOn("classes") - .doLast { - def path - project.tasks.withType(JavaCompile) { - path = classpath.findAll{ it.exists() }.join( - System.properties['os.name'].toLowerCase().contains('windows') ? ';' : ':' - ) + def jshellTask = project.tasks.create('jshell') + def classesTask = project.tasks.find { it.name == "classes" } + if (classesTask) { + jshellTask.dependsOn classesTask + } else { + // Some multi-module projects may not have the :classes task + jshellTask.logger.warn ":jshell task :classes not found, be sure to compile the project first" + } + jshellTask.doLast { + Set pathSet = [] + project.tasks.withType(JavaExec) { + pathSet.addAll(classpath.findAll{ it.exists() }) + } + project.subprojects.each { + it.tasks.withType(JavaExec) { + pathSet.addAll(classpath.findAll{ it.exists() }) + } } - Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader()) // promote class loader - JShellToolProvider.main((String[])["--class-path", path].toArray()); + def path = pathSet.join(System.properties['os.name'].toLowerCase().contains('windows') ? ';' : ':') + jshellTask.logger.debug(":jshell executing with --class-path \"{}\"", path) + JavaShellToolBuilder.builder().run((String[])["--class-path", path].toArray()) } } - }