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."
    }
}
时间: 2024-10-10 17:18:54

Groovy intro的相关文章

Android studio:Groovy 与 Gradle 基础【三】

转载:http://www.apkbus.com/forum.php?mod=viewthread&tid=255064&extra=page%3D2%26filter%3Dauthor%26orderby%3Ddateline&_dsign=276e9e2e 相信看过前一篇 <Android Studio 与 Gradle 深入>的同学,有一部分就会遇到我初识 Gradle 时的困惑:代码我也依稀看得懂,但就是不知道还能这样写,为什么这样写. 问题与解决方案 回想我在

[转载] 详述三种现代JVM语言--Groovy,Scala和Clojure

转载自http://www.tuicool.com/articles/jYzuAv和http://www.importnew.com/1537.html 在我与Martin Fowler曾经合作呈现的一次主题演讲中,他作出了一个有洞察性的观点: Java的遗产将是平台,而不是程序设计语言. Java技术的原始工程师们作出了一个明智的决定,就是将编程语言与运行时环境分开,最终这使得超过200种语言能够运行在Java平台上.这种架构对于该平台的长期活力是至关重要的,因为计算机程序设计语言的寿命一般都

TestRunnerInsertMysqlSingle.groovy

s C:\Users\Lindows\workspace\GroovyTest\src\com\iteye\lindows\mysql\TestRunnerInsertMysqlSingle.groovy 1 package com.iteye.lindows.mysql 2 3 import junit.framework.Assert 4 import net.grinder.script.GTest 5 import net.grinder.scriptengine.groovy.juni

Groovy语法

Java的东西Groovy都能用,包括语法和类库 例如,新建一个HelloGroovy.groovy 输入一下Java代码: /** * Created by sunwenming on 2017/2/8. */ public class HelloGroovy {     public static void main(String[] args) {         for (int i = 0; i < 3; i++) {             System.out.println(&quo

Groovy与Gradle在Android中的应用

大家都知道, Android Studio 的编译构建,是基于Gradle的, 而Gradle又是基于Groovy, Groovy又是基于Java的 Android Studio 的gradle 本身就是基于groovy的, 所以环境的搭建就不多说了. Groovy语言基础 1. 注释:与java相同使用//, /** **/ 2. Groovy语句可以不用分号结尾 3. 支持动态类型, 就是说, 声明变量时, 可以不指定类型 4. 函数定义: def getChannelOption() {

Scala vs. Groovy vs. Clojure

http://stackoverflow.com/questions/1314732/scala-vs-groovy-vs-clojure Groovy is a dynamically typed language, whose syntax is very close to Java, with a number of syntax improvements that allow for lighter code and less boilerplate. It can run throug

Groovy简单再入门

以前试过,忘得差不多了,趁这次从JAVA一直撸到SERVLET,SPRING,GROOVY,GRAILS的机会,再弄一次吧... def authors = [ "Peter Ledbrook", "Glen Smith" ] def quoteParts = [ ["Time", "waits", "for no man"], ["The roundhouse kick", "

用Groovy构建java脚本

我是做工作流项目的,工作流中各个模板引擎都需要要执行一个动态业务,这些动态业务有多种实现方式,最常用的就是用户自己写一段脚本文件,然后工作流引擎执行到这里的时候,运行这个脚本文件. 这个运行脚本文件的功能最初项目组是用ScriptEngineManager实现的.要求用户提供一个实现业务的jar,并写js脚本,脚本里动态地调用java内置在js引擎里的几个函数,这几个函数就是动态地调用jar里面的业务函数. 但是这样做太不方便,一是js无法调用java代码,只能预置java函数,或者通过预置的j

groovy : poi 导出 Excel

參考 poi-3.10-FINAL/docs/spreadsheet/quick-guide.html write_xls.groovy 代码例如以下 package xls; import javax.swing.JFileChooser; import javax.swing.filechooser.FileFilter; import javax.swing.filechooser.FileNameExtensionFilter; import java.io.File; import j