zoukankan      html  css  js  c++  java
  • 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."
        }
    }
  • 相关阅读:
    Ubuntu 装JDK
    U盘文件夹被病毒隐藏,且不能取消解决办法
    wireshark: there are no interfaces on which a capture can be done
    [转]Ubuntu 常用快捷键10个
    恢复被win7覆盖的Ubuntu Grub
    U盘安装Win7 64位
    荣耀3X畅玩版狙击红米note!
    Secret and Whisper
    360 chrome不能登录Google账户
    周鸿祎仍想做手机
  • 原文地址:https://www.cnblogs.com/phoenix13suns/p/4555998.html
Copyright © 2011-2022 走看看