Groovy intro

1. basic example

println "hello, this line is outside of any task"

task stringsAndTypes << {
    // TODO: Run `$ gradle sAT`
    println "Nice work abbreviating the task name"

    // TODO: Create a variable named foo and assign it the value 4.2
    def foo = 4.2

    // TODO: Print the value and class of foo
    println "foo is of type: ${foo.class} and has value: $foo"

    // TODO: Use string interpolation to print the square root of 127
    // HINT: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html
    println "The square root of 127 is ${Math.sqrt(127)}"

    // TODO: Assign the current date to foo and print it, along with its type
    // HINT:
    // http://docs.oracle.com/javase/7/docs/api/java/util/package-summary.html
    foo = new java.util.Date()
    println "foo is now of type: ${foo.class} and has value: $foo"


    // TODO: Use the `substring` method to extract my name from this string
    def hello = "Hello, Jeremy."
    def name = hello.substring(7, 13)

    // TODO: Use `toUpperCase` to capitalize and print my name
    println name.toUpperCase()
}


task closures << {
    // TODO: Declare a closure that takes two arguments and adds them together
    // HINT: http://groovy.codehaus.org/Closures
    def addTwo = {x, y -> x + y}

    // TODO: Call your closure with arguments 17 and 25, and print the result
    // Beware that something like `println addTwo 17, 25` is ambiguous
    println addTwo(17,25)
}

task lists << {
    // TODO: Declare a list containing 5 of your favorite candies
    // HINT: Try searching for "groovy list literal"
    def candyList = ["Tomatoes","Carrots","Spinach","Radishes","Beef"]

    // TODO: Create a new list of your candies in all caps
    // HINT: http://groovy.codehaus.org/Tutorial+2+-+Code+as+data,+or+closures
    def capitalCandies = candyList.collect{it.toUpperCase()}

    // TODO: Print each capital candy
    capitalCandies.each{println it}
}

task foobar << {
    def foo = "bar"
    println "$foo + foo equals ${foo + 'foo'}"
}

 2. dependency

(1) dependsOn

task putOnSocks {
    doLast {
        println "Putting on Socks."
    }
}

task putOnShoes {
    dependsOn "putOnSocks"
    doLast {
        println "Putting on Shoes."
    }
}

(2)finalizedBy

task eatBreakfast {
    finalizedBy "brushYourTeeth"
    doLast{
        println "Om nom nom breakfast!"
    }
}

task brushYourTeeth {
    doLast {
        println "Brushie Brushie Brushie."
    }
}
原文地址:https://www.cnblogs.com/phoenix13suns/p/4555998.html