One of my favorite things to do in Groovy is read files line by line. I'm going to show you these examples using a Gradle Build Script.

Imagine you had this build.gradle

apply plugin: 'java'
apply plugin: 'maven'
def version = '1.5'
repositories {
dependencies {
    compile 'org.codehaus.groovy:groovy-all:2.1.6'

What if you wanted to write a script to extract the version from the build.gradle to use it in a Shell Script or for instance a CI Build Task? How would you do this efficiently?

// Defines the build.gradle file
def gradleScript = new File('build.gradle')
gradleScript.eachLine {
    // Continue if it is not the version line
    if (!it.startsWith('def version')) {return}
    println evaluate(it) // Evaluates the line and prints the version

This allows you to extract versions fast and simply using nothing but a Groovy Script! (smile)