文章来自:http://www.ciandcd.com
文中的代码来自可以从github下载: https://github.com/ciandcd
 
安装:

wget https://dl.bintray.com/groovy/maven/apache-groovy-binary-2.4.7.zip
unzip apache-groovy-binary-2.4.7.zip
sudo ln -s /home/osboxes/Downloads/groovy-2.4.7/bin/groovy /usr/bin/groovy
groovy -v
Groovy Version: 2.4.7 JVM: 1.8.0_91 Vendor: Oracle Corporation OS: Linux

参考:

https://learnxinyminutes.com/docs/groovy/
http://www.groovy-lang.org/index.html
https://github.com/ciandcd/jenkins-awesome/blob/master/utils/groovy_basic.gy

 

groovy基本语法,方便大家查阅。

#!/usr/bin/env groovy

// Hello World
println "Hello world!" // Variables: You can assign values to variables for later use
def x = 1
println x x = new java.util.Date()
println x x = -3.1499392
println x x = false
println x x = "Groovy!"
println x //Creating an empty list
def technologies = [] /*** Adding a elements to the list ***/
// As with Java
technologies.add("Grails") // Left shift adds, and returns the list
technologies << "Groovy" // Add multiple elements
technologies.addAll(["Gradle","Griffon"]) /*** Removing elements from the list ***/
// As with Java
technologies.remove("Griffon") // Subtraction works also
technologies = technologies - 'Grails' /*** Iterating Lists ***/
// Iterate over elements of a list
technologies.each { println "Technology: $it"}
technologies.eachWithIndex { it, i -> println "$i: $it"} /*** Checking List contents ***/
//Evaluate if a list contains element(s) (boolean)
contained = technologies.contains( 'Groovy' ) // Or
contained = 'Groovy' in technologies // Check for multiple contents
technologies.containsAll(['Groovy','Grails']) /*** Sorting Lists ***/ // Sort a list (mutates original list)
technologies.sort() // To sort without mutating original, you can do:
sortedTechnologies = technologies.sort( false ) /*** Manipulating Lists ***/ //Replace all elements in the list
Collections.replaceAll(technologies, 'Gradle', 'gradle') //Shuffle a list
Collections.shuffle(technologies, new Random()) //Clear a list
technologies.clear() //Creating an empty map
def devMap = [:] //Add values
devMap = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
devMap.put('lastName','Perez') //Iterate over elements of a map
devMap.each { println "$it.key: $it.value" }
devMap.eachWithIndex { it, i -> println "$i: $it"} //Evaluate if a map contains a key
assert devMap.containsKey('name') //Evaluate if a map contains a value
assert devMap.containsValue('Roberto') //Get the keys of a map
println devMap.keySet() //Get the values of a map
println devMap.values() //Groovy supports the usual if - else syntax
def x1 = 3 if(x1==1) {
println "One"
} else if(x1==2) {
println "Two"
} else {
println "X greater than Two"
} //Groovy also supports the ternary operator:
def y = 10
def x2 = (y > 1) ? "worked" : "failed"
assert x2 == "worked" //Instead of using the ternary operator:
//displayName = user.name ? user.name : 'Anonymous'
//We can write it:
//displayName = user.name ?: 'Anonymous' //For loop
//Iterate over a range
def x3 = 0
for (i in 0 .. 30) {
x3 += i
} //Iterate over a list
x4 = 0
for( i in [5,3,2,1] ) {
x4 += i
} //Iterate over an array
array = (0..20).toArray()
x5 = 0
for (i in array) {
x5 += i
} //Iterate over a map
def map = ['name':'Roberto', 'framework':'Grails', 'language':'Groovy']
x6 = 0
for ( e in map ) {
x6 += e.value
} /*
Closures
A Groovy Closure is like a "code block" or a method pointer. It is a piece of
code that is defined and then executed at a later point.
More info at: http://www.groovy-lang.org/closures.html
*/ //Example:
def clos = { println "Hello World!" } println "Executing the Closure:"
clos() //Passing parameters to a closure
def sum = { a, b -> println a+b }
sum(2,4) //Closures may refer to variables not listed in their parameter list.
def x7 = 5
def multiplyBy = { num -> num * x7 }
println multiplyBy(10) // If you have a Closure that takes a single argument, you may omit the
// parameter definition of the Closure
def clos2 = { println it }
clos2( "hi" ) /*
Groovy can memoize closure results [1][2][3]
*/
def cl = {a, b ->
sleep(3000) // simulate some time consuming processing
a + b
} mem = cl.memoize() def callClosure(a, b) {
def start = System.currentTimeMillis()
println mem(a, b)
println "Inputs(a = $a, b = $b) - took ${System.currentTimeMillis() - start} msecs."
} callClosure(1, 2)
callClosure(1, 2)
callClosure(2, 3)
callClosure(2, 3)
callClosure(3, 4)
callClosure(3, 4)
callClosure(1, 2)
callClosure(2, 3)
callClosure(3, 4)

 完

jenkins2 groovy语法的更多相关文章

  1. Groovy 语法学习

    一.配置 Groovy 环境: 下载 Groovy(Groovy 依赖 Java,所以需要 JDK 环境):http://www.groovy-lang.org/download.html 配置环境变 ...

  2. atitit.groovy 语法特性

    atitit.groovy 语法特性 1. Groovy 1.6概览1 1.1. 多路赋值2 2. 新发布的Groovy2.0为这门语言带来了关键的静态特性:静态类型检查和静态编译:2 3. 参考3 ...

  3. Gradle系列之一 Groovy语法精讲

    Gradle技术之一 Groovy语法精讲 gradle脚本是基于groovy语言开发的,想要学好gradle必须先要对groovy有一个基本的认识 1. Groovy特点 groovy是一种DSL语 ...

  4. Groovy语法基础

    Groovy 简介 Groovy 是一种基于 JVM 的动态语言,他的语法和 Java 相似,最终也是要编译 .class 在JVM上运行. Groovy 完全兼容 Java 并且在此基础上添加了很多 ...

  5. jenkins2 pipeline 语法快速参考

    jenkins2 pipeline中常用的语法快速参考. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciand ...

  6. groovy语法

    1.注释1.1. 单行注释1.2. 多行注释1.3. GroovyDoc注释1.4. Shebang线2.关键词3.标识符3.1. 普通标识符3.2. 带引号的标识符4.字符串4.1. 单引号字符串4 ...

  7. 看懂Gradle脚本(4)- Groovy语法之运算符重载

    继续讨论Task定义 回想一下前一篇文章的样例: task myTask { doLast { println 'hello world!' } } 这段脚本定义了一个名为myTask的任务.而且通过 ...

  8. jenkins2 groovy脚本参考

    使用plugin生成groovy脚本,或者参考已有的groovy脚本. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.co ...

  9. hibernate查询之Criteria实现分页方法(GROOVY语法)

    public int searchTest(String name, Integer pageIndex, List<Test> resultList){ def criteria = T ...

随机推荐

  1. asp.net下拉列表绑定数据库多个字段

    select ID, CONVERT(varchar(10),TBName) +','+RoomName+ ',最大人数:'+CONVERT(varchar(10),MAXNum) as ClassR ...

  2. TS 流的解码过程(系摘抄)

    TS 流解码过程: 1. 获取TS中的PAT 2. 获取TS中的PMT 3. 根据PMT可以知道当前网络中传输的视频(音频)类型(H264),相应的PID,PCR的PID等信息. 4. 设置demux ...

  3. C#获取操作系统是32位或64位的代码

    注意需添加引用System.Management) public static string Distinguish64or32System() { try { string addressWidth ...

  4. MVC dirname(——FILE——)

    1.MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(controller)的缩写,一种软件设计典范,用一种业务逻辑.数据.界面显示分离的方法组织 ...

  5. Javascript函数、构造函数、原型、类和对象

    函数 函数是JavaScript中特殊的对象,对函数执行typeof运算会返回字符串"function",因为函数也是对象,他们可以拥有属性和方法. 静态方法 函数在JS中定义了类 ...

  6. oracle常用操作指令

    1.cmd   sqlplus /nolog; 2.conn  sys/  as sysdba; 3.create user query identified by query;//创建用户 4.al ...

  7. Python全栈之路-----基础篇

    Python诞生 Python是著名的”龟叔“Guido van Rossum(吉多·范罗苏姆)在1989年圣诞节期间,为了打发无聊的圣诞节而编写的一个编程语言. Python语法很多来自C,但又受到 ...

  8. 小甲鱼python视频第七讲(课后习题)

    1.assert的作用. assert用来判断语句的真假,如果为假的话将触发AssertionError错误. 如果为真则继续执行. 2.变量互换(注意顺序) 3.成员资格运算符(in) 4.分数的划 ...

  9. Android--多线程之Handler(转)

    前言 Android的消息传递机制是另外一种形式的“事件处理”,这种机制主要是为了解决Android应用中多线程的问题,在Android中不 允许Activity新启动的线程访问该Activity里的 ...

  10. MFC下无法为空间添加变量解决

    许久不用MFC,今天在vs2008下用MFC写个小东西,结果在为控件添加变量的时候,居然无法成功——那个界面显示怪怪的,点击完成提示失败.    还好同事遇到过这个问题,给出链接http://hi.b ...