@@ -18,15 +18,19 @@ package com.github.mrsarm.jshell.plugin
1818import jdk.jshell.tool.JavaShellToolBuilder
1919import org.gradle.api.Plugin
2020import org.gradle.api.Project
21+ import org.gradle.api.Task
2122import org.gradle.api.tasks.JavaExec
2223
2324class JShellPlugin implements Plugin<Project > {
2425
25- @Override
26- void apply (Project project ) {
27- def jshellTask = project. tasks. create(' jshell' )
28- jshellTask. group = ' application'
29- jshellTask. description = ' Runs a JShell session with all the code and dependencies.'
26+ private String [] shellArgs
27+
28+ // This task could be merged with the main task in just one,
29+ // the problem is that the main task is a JavaExec one,
30+ // and at least for now I couldn't find a way
31+ // to get the dependencies list with this type
32+ private Task createJshellSetupTask (Project project ) {
33+ Task jshellTask = project. tasks. create(' jshellSetup' )
3034 def classesTask = project. tasks. find { it. name == " classes" }
3135 if (classesTask) {
3236 jshellTask. dependsOn classesTask
@@ -45,10 +49,9 @@ class JShellPlugin implements Plugin<Project> {
4549 pathSet. addAll(classpath. findAll { it. exists() })
4650 }
4751 }
48- Thread . currentThread(). setContextClassLoader(ClassLoader . getSystemClassLoader()) // promote class loader
4952 def path = pathSet. join(System . properties[' os.name' ]. toLowerCase(). contains(' windows' ) ? ' ;' : ' :' )
5053 jshellTask. logger. info(" :jshell executing with --class-path \" {}\" " , path)
51- String [] args = [
54+ shellArgs = [
5255 " --class-path" , path,
5356 " --startup" , " DEFAULT" ,
5457 " --startup" , " PRINTING"
@@ -60,21 +63,34 @@ class JShellPlugin implements Plugin<Project> {
6063 def jshellStartup = project. findProperty(" jshell.startup" )
6164 jshellTask. logger. info(" :jshell executing with --startup DEFAULT --startup PRINTING " +
6265 " --startup \" {}\" " , jshellStartup)
63- args = args + (String []) [" --startup" , jshellStartup]
66+ shellArgs = shellArgs + (String []) [" --startup" , jshellStartup]
6467 }
6568 else {
6669 def startupJsh = new File (" ${ project.projectDir} /startup.jsh" )
6770 if (startupJsh. exists()) {
6871 def startupJshPath = startupJsh. absolutePath
6972 jshellTask. logger. info(" :jshell executing with --startup DEFAULT --startup PRINTING" +
7073 " --startup \" {}\" " , startupJshPath)
71- args = args + (String []) [" --startup" , startupJshPath]
74+ shellArgs = shellArgs + (String []) [" --startup" , startupJshPath]
7275 } else {
7376 jshellTask. logger. info(" :jshell did not find a startup.jsh script at the project dir " +
7477 " nor a `jshell.startup` configuration" )
7578 }
7679 }
77- JavaShellToolBuilder . builder(). run(args)
80+ }
81+ return jshellTask
82+ }
83+
84+ @Override
85+ void apply (Project project ) {
86+ Task jshellSetupTask = createJshellSetupTask(project)
87+ Task jshellTask = project. tasks. create(' jshell' )
88+ jshellTask. group = ' application'
89+ jshellTask. description = ' Runs a JShell session with all the code and dependencies.'
90+ jshellTask. dependsOn jshellSetupTask
91+ jshellTask. doLast {
92+ Thread . currentThread(). setContextClassLoader(ClassLoader . getSystemClassLoader()) // promote class loader
93+ JavaShellToolBuilder . builder(). run(shellArgs)
7894 }
7995 }
8096}
0 commit comments