Custom Gradle Task

I have started using Gradle lately for building Java and Groovy projects. So far I really like it.

I do find it a bit strange that there Gradle does not run Grails projects out of the box.

Scouring the web, I found a task that will run the Groovy console from Gradle, and include the jars in the classpath:

// you can type "gradle console" and get a console with all your classes
task(console, dependsOn: 'classes', type: JavaExec) {
  main = 'groovy.ui.Console'
  classpath = sourceSets.main.runtimeClasspath
}

Personally, I do not like the Groovy console. I prefer groovysh, the Groovy shell. I have tried and tried to get it to work with Gradle, but so far I have not been able to.

I did make a small task that will print the jars in the project’s class path to the screen. Then you can put them in a text file and come up with a groovysh command with the jars in the classpath. It is not perfect, but it works:

task printClassPath() {
     println( "-- Groovy version: " + groovy.lang.GroovySystem.getVersion() )
     dependsOn classes
     description = 'Print jars in classpath to command line'
     
     println("Printed the path")
     runtimeClasspath.getAsPath().tokenize(":").each { theFile ->
        println theFile
     }
     println "\n"
}

If anyone has a task that runs a Groovy shell with the jars in it, let me know.

There are 221 words in this article.