Groovy ShortTypeHandling ClassNotFoundException

ShortTypeHandling was introduced in groovy-all-2.3.0.jar so the quick fix was to replace the older groovy-all-x.x.x.jar with groovy-all-2.3.0.jar. This solved the runtime ShorTypeHandling ClassNotFoundException but also created new problems by introducing a new groovy-all.jar dependency in the application. The real issue was how the groovy compiler was being invoked via maven. Because I introduced spock which … Read more

Gradle String Replacement – no placeholders

Read the text in: String contents = new File( ‘whatever.txt’ ).getText( ‘UTF-8’ ) Replace the text contents = contents.replaceAll( ‘xxx’, ‘yyy’ ) Write the text out again new File( ‘replaced.txt’ ).write( contents, ‘UTF-8’ ) You should be able to wrap them into a task and call the task as normal

What’s the operator

The << is a left-shift operator. In this scenario, task “task$counter” is a Task object declaration, and << is overloaded as an alias to the doLast method, which appends the closure to the list of actions to perform when executing the task. If you don’t specify the <<, the closure is treated as a configuration … Read more

Running Groovy scripts from Gradle using a different version of Groovy

You can create directory src/main/groovy, put your script called myscript.groovy in there: println “hello world from groovy version ${GroovySystem.version}” Then, have a build.gradle file in your project root directory: apply plugin: ‘groovy’ repositories { mavenCentral() } dependencies { compile ‘org.codehaus.groovy:groovy-all:2.0.5’ } task runScript (dependsOn: ‘classes’, type: JavaExec) { main = ‘myscript’ classpath = sourceSets.main.runtimeClasspath } … Read more

How do I parse a YAML file in groovy?

I am adding here the same example, but implemented with the native YAMLSlurper (Groovy 3.x+): import groovy.yaml.YamlSlurper def exampleYaml=””‘\ — – subject: “maths” – subject: “chemistry” ”’ List example = new YamlSlurper().parseText(exampleYaml) // If your source is a File // List example = new YamlSlurper().parse(“example.yaml” as File) example.each{println it.subject} For previous versions (Original answer): snakeyaml … Read more

How does one return from a groovy closure and stop its execution?

I think you want to use find instead of each (at least for the specified example). Closures don’t directly support break. Under the covers, groovy doesn’t actually use a closure either for find, it uses a for loop. Alternatively, you could write your own enhanced version of find/each iterator that takes a conditional test closure, … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)