Running groovy shell from gradle task


(pschorf2) #1

I’m trying to run a groovy shell (org.codehaus.groovy.tools.shell.Main) with my project’s classpath. I created the following task:

task(shell, dependsOn: ':core:compileJava', type: JavaExec) {
                                                                                                                                              main = 'org.codehaus.groovy.tools.shell.Main'
                                                                                                                                                            classpath configurations.shell + project(':core').sourceSets.main.runtimeClasspath
                                                                                                                       standardInput = System.in
                                                                                                                                                                              }

which works, but due to some sort of buffering issue, if I hit backspace in the console the cursor doesn’t move back and the text still appears, but it is apparently removed. Is there a workaround for this? I couldn’t find much aside from http://gradle.1045684.n5.nabble.com/Reading-keyboard-input-td3073108.html


(Giovanni Botta) #2

I almost got it to run with the following:

apply plugin:‘application’

mainClassName = ‘org.codehaus.groovy.tools.shell.Main’

run.standardInput = System.in

dependencies {

compile ‘org.codehaus.groovy:groovy-all:2.2.2’

compile ‘org.fusesource.jansi:jansi:1.11’

compile ‘commons-cli:commons-cli:1.2’

compile ‘jline:jline:2.11’

compile project(’:my-module’)

}

And then from the command line

gradle run

However it gets stuck on:

Groovy Shell (2.2.2, JVM: 1.6.0_45)

Type ‘help’ or ‘\h’ for help.


groovy:000>

Building 87% > :console:run

and if I type nothing shows up. I feel like Gradle is sucking all the input from ‘System.in’ not allowing me to type anything. I also have an SO for this.

Ideas?


(dylon.edwards) #3

This seems to work:

configurations {
 shell
}
  dependencies {
 shell 'commons-cli:commons-cli:1.2'
 shell('jline:jline:2.11') {
  exclude(group: 'junit', module: 'junit')
 }
 shell 'org.codehaus.groovy:groovy-groovysh:2.2.+'
}
  task(shell, dependsOn: 'classes') << {
 def classpath = sourceSets.main.runtimeClasspath + configurations.shell
   def command = [
  'java',
  '-cp', classpath.collect().join(':'),
  'org.codehaus.groovy.tools.shell.Main',
  '--color',
  '--terminal', 'unix']
   def proc = new ProcessBuilder(command)
  .redirectOutput(ProcessBuilder.Redirect.INHERIT)
  .redirectInput(ProcessBuilder.Redirect.INHERIT)
  .redirectError(ProcessBuilder.Redirect.INHERIT)
  .start()
   proc.waitFor()
}

Then, execute it via

gradle shell

(Giovanni Botta) #4

Uhm, getting:

  • What went wrong:

Execution failed for task ‘:console:shell’.

No such property: Redirect for class: java.lang.ProcessBuilder

Do you have any includes defined?


(Giovanni Botta) #5

Oh wait, I am using Java 6, ‘Redirect’ is not there.