Skip to content

Commit 39f4879

Browse files
committed
Add startup options. Improve documentation
1 parent eec12fc commit 39f4879

2 files changed

Lines changed: 86 additions & 13 deletions

File tree

README.md

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,18 @@
11
JShell Plugin
22
=============
33

4-
This Gradle plugin helps you to explore your code and dependencies in your gradle project
4+
This **Gradle plugin** helps you to explore your code and dependencies in your gradle project
55
with in [jshell](https://docs.oracle.com/javase/9/jshell/introduction-jshell.htm),
66
the official Java REPL tool.
77

8-
Hosted on (NOT published yet): https://plugins.gradle.org/plugin/com.github.mrsarm.jshell.plugin
8+
Hosted on _(NOT published yet)_: https://plugins.gradle.org/plugin/com.github.mrsarm.jshell.plugin
9+
10+
- [Getting started](#getting-started)
11+
- [Startup options](#startup-options)
12+
- [Troubleshooting](#troubleshooting)
13+
- [System Requirements](#system-requirements)
14+
- [Build and Publish](#build-an-publish)
15+
- [About](#about)
916

1017

1118
Getting started
@@ -19,7 +26,7 @@ plugins {
1926
}
2027
```
2128

22-
or for Gradle < 2.1:
29+
or in Gradle < 2.1:
2330

2431
```groovy
2532
buildscript {
@@ -44,14 +51,54 @@ Following is an example:
4451

4552
$ gradle --no-daemon --console plain jshell
4653

54+
Startup options
55+
---------------
56+
57+
The shell console starts with the options
58+
`--startup DEFAULT --startup PRINTING` so always
59+
the following imports and functions are available:
60+
61+
- java.io
62+
- java.math
63+
- java.net
64+
- java.nio.file
65+
- java.util
66+
- java.util.concurrent
67+
- java.util.function
68+
- java.util.prefs
69+
- java.util.regex
70+
- java.util.stream
71+
- print (alias of `System.out.print`)
72+
- println (alias of `System.out.println`)
73+
74+
If the plugin founds at the root of the project a
75+
[JShell Script](https://docs.oracle.com/javase/9/jshell/scripts.htm)
76+
named `startup.jsh`, it will append to the JShell session
77+
the argument `--startup startup.jsh`, executing
78+
at the beginning all the instruction in the script,
79+
so you can add there all the imports, object definitions
80+
or any Java instruction that you want to execute
81+
at the begging of the session. You can override
82+
the startup script path with the project property
83+
`jshell.startup` in the `gradle.properties` file,
84+
or set the same property in the command line
85+
arguments, like:
86+
87+
$ gradle --no-daemon --console plain jshell -Pjshell.startup=/path/to/run.jsh
88+
89+
90+
Troubleshooting
91+
---------------
92+
4793
If you see this warning and the jshell console does not detect your classes:
4894

4995
> :jshell task :classes not found, be sure to compile the project first
5096
5197
Means the `classes` task needed to compile your project before launch `jshell`
52-
does not exist, just append the task needed to compile the project,
53-
some times is the same `classes` task but is not detected in multi-modules
54-
projects, so you need to add it explicitly in the Gradle command:
98+
does not exist, just append in the command line the task needed to compile
99+
the project, some times is the same `classes` task but is not detected
100+
in multi-modules projects, so you need to add it explicitly in the
101+
Gradle command:
55102

56103
$ gradle --no-daemon --console plain classes jshell
57104

@@ -63,8 +110,8 @@ System Requirements
63110
* Gradle
64111

65112

66-
Build & Publish
67-
---------------
113+
Build and Publish
114+
-----------------
68115

69116
Compile and build the .jar locally with:
70117

@@ -74,6 +121,7 @@ Publish to your local Maven repo:
74121

75122
$ ./gradlew publishToMavenLocal
76123

124+
77125
About
78126
-----
79127

@@ -84,14 +132,17 @@ and this version solve some issues and adds the following features:
84132
- It works with **multi-module projects**
85133
- There is no need to set the env variable `JAVA_OPTS` with a bunch of
86134
of arguments _"--add-exports jdk.jshell/jdk.intern..."_
87-
- _Coming soon_: add special support to the **Spring Framework**
135+
- It allows to run at the beginning of the session a _.jsh_ startup script
136+
- _Coming soon_: special support to the **Spring Framework**
88137

89138
Project: https://github.com/mrsarm/jshell-plugin
90139

91-
Authors:
140+
### Authors
92141

93142
- Mariano Ruiz <mrsarm@gmail.com>
94143
- https://github.com/bitterfox
95144
(original [project](https://github.com/bitterfox/jshell-gradle-plugin))
96145

97-
License: (2020) [Apache Software License 2.0](https://www.apache.org/licenses/LICENSE-2.0).
146+
### License
147+
148+
- (2020) [Apache Software License 2.0](https://www.apache.org/licenses/LICENSE-2.0).

src/main/groovy/com/github/mrsarm/jshell/plugin/JShellPlugin.groovy

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,30 @@ class JShellPlugin implements Plugin<Project> {
4444
}
4545
Thread.currentThread().setContextClassLoader(ClassLoader.getSystemClassLoader()) // promote class loader
4646
def path = pathSet.join(System.properties['os.name'].toLowerCase().contains('windows') ? ';' : ':')
47-
jshellTask.logger.debug(":jshell executing with --class-path \"{}\"", path)
48-
JavaShellToolBuilder.builder().run((String[])["--class-path", path].toArray())
47+
jshellTask.logger.info(":jshell executing with --class-path \"{}\"", path)
48+
String[] args = [
49+
"--class-path", path,
50+
"--startup", "DEFAULT",
51+
"--startup", "PRINTING"
52+
]
53+
if (project.findProperty("jshell.startup")) {
54+
def jshellStartup = project.findProperty("jshell.startup")
55+
jshellTask.logger.info(":jshell executing with --startup DEFAULT --startup PRINTING " +
56+
"--startup \"{}\"", jshellStartup)
57+
args = args + (String[]) ["--startup", jshellStartup]
58+
} else {
59+
def startupJsh = new File("${project.projectDir}/startup.jsh")
60+
if (startupJsh.exists()) {
61+
def startupJshPath = startupJsh.absolutePath
62+
jshellTask.logger.info(":jshell executing with --startup DEFAULT --startup PRINTING" +
63+
" --startup \"{}\"", startupJshPath)
64+
args = args + (String[]) ["--startup", startupJshPath]
65+
} else {
66+
jshellTask.logger.info(":jshell did not find a startup.jsh script at the project dir " +
67+
"nor a `jshell.startup` configuration")
68+
}
69+
}
70+
JavaShellToolBuilder.builder().run(args)
4971
}
5072
}
5173
}

0 commit comments

Comments
 (0)