From c2bcfc21b37c10b56ea2d9ceecac8cf65aaadaaa Mon Sep 17 00:00:00 2001 From: jpoth Date: Mon, 27 May 2019 17:07:01 +0200 Subject: [PATCH] Add options support --- README.adoc | 27 ++++++- .../github/johnpoth/jshell/JShellMojo.java | 74 ++++++++++++++++++- 2 files changed, 99 insertions(+), 2 deletions(-) diff --git a/README.adoc b/README.adoc index 9bc2ec4..3cee297 100644 --- a/README.adoc +++ b/README.adoc @@ -23,7 +23,29 @@ jshell> To you use your project's test classpath, add *-DtestClasspath* to the command. -Works on Java 9, 10 and 11+ Enjoy! +== Options + +[cols="1v,1v,1v"] +|=== +|Name |Description |Default value| + +class-path|Explicitly set JShell's class path|| + +module-path|Set JShell's module path|| + +add-modules|Add modules from JShell's module path|| + +add-exports|Explicitly add exports|| + +scripts|Add startup scripts to JShell ',' delimited|| + +useProjectClasspath|Use project class path in JShell|True| + +options| Add other options to JShell. See https://docs.oracle.com/javase/9/tools/jshell.htm#GUID-C337353B-074A-431C-993F-60C226163F00__OPTIONSFORJSHELL-AF4AC615[docs]|| +|=== + +Reminder: plugin parameters when passed through the command line should be prefixed with 'jshell.' e.g: + *mvn com.github.johnpoth:jshell-maven-plugin:1.2:run -Djshell.scripts="script0,script1"* == Adding to pom.xml @@ -51,3 +73,6 @@ $mvn jshell:run | For an introduction type: /help intro jshell> ---- + + +Works on Java 9, 10 and 11+ Enjoy! \ No newline at end of file diff --git a/src/main/java/com/github/johnpoth/jshell/JShellMojo.java b/src/main/java/com/github/johnpoth/jshell/JShellMojo.java index f165707..664fecc 100644 --- a/src/main/java/com/github/johnpoth/jshell/JShellMojo.java +++ b/src/main/java/com/github/johnpoth/jshell/JShellMojo.java @@ -18,6 +18,7 @@ import javax.tools.Tool; import java.io.File; +import java.util.ArrayList; import java.util.List; import java.util.Optional; import java.util.ServiceLoader; @@ -42,6 +43,28 @@ public class JShellMojo extends AbstractMojo @Parameter(defaultValue = "false", property = "testClasspath") private boolean testClasspath; + @Parameter(defaultValue = "true", property = "jshell.useProjectClasspath") + private boolean useProjectClasspath; + + @Parameter(property = "jshell.class-path") + private String classpath; + + @Parameter(property = "jshell.module-path") + private String modulepath; + + @Parameter(property = "jshell.add-modules") + private String addModules; + + @Parameter(property = "jshell.add-exports") + private String addExports; + + @Parameter(property = "jshell.scripts") + private List scripts = new ArrayList<>(); + + // additional options that may be added in future Java releases. + @Parameter(property = "jshell.options") + private List options = new ArrayList<>(); + public void execute() throws MojoExecutionException { String cp; if (testClasspath) { @@ -62,10 +85,59 @@ public void execute() throws MojoExecutionException { .findAny() .orElseThrow(() -> new RuntimeException("No JShell service providers found!")) .get(); - String[] args = new String[]{"--class-path", cp}; + String[] args = addArguments(cp); jshell.run(System.in, System.out, System.err, args); } finally { Thread.currentThread().setContextClassLoader(contextClassLoader); } } + + private String[] addArguments(String cp) { + int size = getArgumentsSize(); + String[] args = new String [size + options.size() + scripts.size()]; + int i = 0; + if (useProjectClasspath) { + args[i++] = "--class-path"; + args[i++] = cp; + } else if (classpath != null ){ + args[i++] = "--class-path"; + args[i++] = classpath; + } + if (modulepath != null){ + args[i++] = "--module-path"; + args[i++] = modulepath; + } + if (addModules!= null){ + args[i++] = "--add-modules"; + args[i++] = modulepath; + } + if (addExports!= null){ + args[i++] = "--add-exports"; + args[i++] = modulepath; + } + for (String option : this.options) { + args[i++] = option; + } + for (String script : scripts) { + args[i++] = script; + } + return args; + } + + private int getArgumentsSize() { + int size = 0; + if (useProjectClasspath || classpath != null) { + size += 2; + } + if (modulepath != null) { + size += 2; + } + if (addModules != null) { + size += 2; + } + if (addExports != null) { + size += 2; + } + return size; + } }