This plugin provides gradle tasks that start an interactive groovy shells, based on the 'groovysh' command that ships with any Groovy version.
The main feature is an Application shell, meaning a shell where your application classes (Java or other JVM language) can be imported, instantiated and run. This allows you to interact directly with your database-layer, service layer, running application, etc. without having to change a line of your code.
There is also build shell, which allows to connect to a gradle project using the gradle tooling API to run builds.
Another particular feature is a build development shell that has a variable project
which represents
your gradle project, allowing you to introspect your project after it has been instantiated. This can be useful
for developing custom gradle plugins, or debugging large setups.
This plugin is Work In Progress, expect some rough edges, but please do report troubles you face.
- NO GROOVY INSTALLATION REQUIRED (gradle will fetch it like any other dependency)
- NO CHANGE TO YOUR JAVA CODE REQUIRED
Include the plugin in your build.gradle file like this:
apply plugin: 'com.github.tkruse.groovysh'
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.tkruse.gradle:gradle-groovysh-plugin:1.0.1'
}
}
Note: With version 0.x.x of this plugin, the plugin id was just 'groovysh', so you would have
to write apply plugin: 'groovysh'
. THis was changed in line with the gradle portal.
The plugin id has changed with version 1.0.0 due to the requirements of gradle-portal.
Currently your project needs to also have the java plugin applied for the shell
task.
No configuration is required. To change the defaults, see Configuration
If you encounter groovy version mismatches, explicitly set your system version (until I find a clean way to fix this).
groovysh {
// groovyVersion determines the features of the shell and buildShell tasks.
groovyVersion = '2.3.6'
}
Invoke either shell task with option -q
(this means "quiet", else the common gradle output will get in the way).
If you have the gradle daemon configured, also add --no-daemon
gradle -q shell
gradle -q buildShell
gradle -q buildDevShell
When using the gradle wrapper, that would be ./gradlew
instead.
You can find samples in the samples subfolder.
- Instantiate a Spring Container (provided you add Spring dependencies to your project)
- Instantiate a Database connection, write business entities etc.
- Run your algorithms interactively
groovy:000> connector.forProjectDirectory(new File("."))
groovy:000> connection = connector.connect()
===> org.gradle.tooling.internal.consumer.DefaultProjectConnection@598b4d64
groovy:000> project = connection.getModel(GradleProject.class)
===> GradleProject{path=':'}
groovy:000> launcher = connection.newBuild()
===> org.gradle.tooling.internal.consumer.DefaultBuildLauncher@3a370a0
groovy:000> launcher.run()
groovy:000> project.tasks
===> [task ':assemble', task ':build', ...
groovy:000> project.configurations
===> [configuration ':appShellConf', configuration ':archives', ...
groovy:000> project.ext.properties
===> {org.gradle.parallel=false, org.gradle.daemon=false, SLF4J_VERSION=1.7.7}
groovy:000> project.repositories.each {println it.name}
BintrayJCenter
groovy:000> project.apply(plugin: 'java')
groovy:000> project.sourceSets.main.getCompileTaskName()
===> compileMain
groovy:000> project.tasks.compileJava.execute() // only executes once
groovy:000> project.tasks.clean.execute() // only executes once
groovy:000> project.gradle.startParameter
===> StartParameter{taskNames=[buildDevShell], ...}
Notice that the Groovy version for the build shell is the same as for gradle (1.8.6), whereas for the application shell a much more recent version of Groovy (2.3.x) can be used. As a consequence the application shell is much prettier, and the allowed syntax is different.
Also if you are new to groovysh, the number one quirk to know is that you must not declare variables, e.g.:
groovy:000> def x = 3
===> 3
groovy:000> x
Unknown property: x
groovy:000> x = 3
===> 3
groovy:000> x
===> 3
While a shell is running that was started from a project, gradle may lock the project, and other tools may not work well during that time (e.g. IntelliJ gradle update). Stopping the shell frees the lock again.
see TODO
See - Dev setup
See the docs at:
- Manual Build Shell README
- Manual App Shell README
Or just install Groovy and run
groovysh
with a suitable classpath.
-
Java Versions (6?, 7, 8)
-
Operating Systems (Ubuntu Precise)
-
Gradle versions (1.8?, 1.9, 1.10, 1.11, 1.12, 2.0)
-
Groovy Versions (2.x.x)