Skip to content

Commit

Permalink
Add options support
Browse files Browse the repository at this point in the history
  • Loading branch information
johnpoth committed May 27, 2019
1 parent 7844a6e commit c2bcfc2
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 2 deletions.
27 changes: 26 additions & 1 deletion README.adoc
Expand Up @@ -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

Expand Down Expand Up @@ -51,3 +73,6 @@ $mvn jshell:run
| For an introduction type: /help intro
jshell>
----


Works on Java 9, 10 and 11+ Enjoy!
74 changes: 73 additions & 1 deletion src/main/java/com/github/johnpoth/jshell/JShellMojo.java
Expand Up @@ -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;
Expand All @@ -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<String> scripts = new ArrayList<>();

// additional options that may be added in future Java releases.
@Parameter(property = "jshell.options")
private List<String> options = new ArrayList<>();

public void execute() throws MojoExecutionException {
String cp;
if (testClasspath) {
Expand All @@ -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;
}
}

0 comments on commit c2bcfc2

Please sign in to comment.