文章来自: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 xx = new java.util.Date()
println xx = -3.1499392
println xx = false
println xx = "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 = 3if(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
}/*ClosuresA Groovy Closure is like a "code block" or a method pointer. It is a piece ofcode 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 processinga + 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)

 完

转载于:https://www.cnblogs.com/itech/p/5627968.html

jenkins2 groovy语法相关推荐

  1. 【Groovy】Groovy 方法调用 ( 字符串切割 | 使用 Java 语法切割字符串 | 使用 Groovy 语法切割字符串直接为变量赋值 | 数组赋值给变量 变量个数小于等于数组长度 )

    文章目录 一.字符串切割 1.使用 Java 语法切割字符串 2.使用 Groovy 语法切割字符串直接为变量赋值 3.数组赋值给变量 变量个数小于等于数组长度 二.完整代码示例 一.字符串切割 在 ...

  2. 【Groovy】Groovy 代码创建 ( 使用 Java 语法实现 Groovy 类和主函数并运行 | 按照 Groovy 语法改造上述 Java 语法规则代码 )

    文章目录 一.创建 Groovy 代码文件 二.使用 Java 语法实现 Groovy 类和主函数并运行 三.按照 Groovy 语法改造上述 Java 语法规则代码 一.创建 Groovy 代码文件 ...

  3. groovy语法基础

    转载自:http://www.ibm.com/developerworks/cn/education/java/j-groovy/j-groovy.html 使用 Groovy 的简单语法开发 Jav ...

  4. Gradle入门之Groovy语法

    前言 gradle是一个基于JVM的构建工具,它结合了Python.Ruby和Smalltalk的许多强大的特性,Groovy 代码能够与Java代码很好地结合,也能用于扩展现有代码.由于其运行在 J ...

  5. gradle的groovy语法案例详解

    groovy的语法与python的语法有些类似 1.groovy的三个特性 1)不需要分号 2)不需要写get,set方法,对象里面默认会有get与set方法 3)方法的最后一个值默认为返回值,不需要 ...

  6. 一、Groovy语法(一):基础语法

    Groovy基础语法 来源:https://www.jianshu.com/p/8127742e0569 1.Groovy中的变量 1.1.变量的类型:基本类型(java中的int,float,dou ...

  7. Groovy语法大全

    文章目录 一.Win10安装groovy环境 `1.打开groovy自带的编辑器` 二.数值和表达式 `1.整数运算符` `2.混合运算` `3.赋值` 第一次使用变量,需要用def去声明变量 变量被 ...

  8. Gradle Groovy 基础语法 MD

    Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...

  9. Grade for Android 之二:Groovy 与Java的语法区别

    Groovy对Java开发者来说是尽可能的自然过渡.Groovy设计者设计Groovy时,遵循最小改变原则,尤其是有Java开发背景的开发者学习Groovy时. 如下列出了Java和Groovy的主要 ...

最新文章

  1. 刻意练习:Python基础 -- Task11. 魔法方法
  2. 皮一皮:叛徒可耻!!!
  3. 第三章:3.6 典型信号傅里叶变换
  4. 北斗导航 | ION GNSS+ 2021、 ION GNSS+ 2020会议论文下载:ION 美国导航学会
  5. java mysql开发_Java数据库开发
  6. List 在迭代时可能抛出的异常
  7. 详解:设计模式之-代理设计
  8. 人工智障学习笔记——梯度下降(1)基础变种
  9. 进程间通信(IPC)之内存映射mmap和共享内存shm
  10. fstream的使用(一)
  11. mac 查看环境变量_Mac开工利器Homebrew介绍
  12. logistic回归详解(三):梯度下降训练方法
  13. 行政区划分与省直辖县级市
  14. 蒙特卡罗方法与马尔科夫链
  15. 后缀为 axd 与 ashx 的文件有什么区别
  16. arm有啥不同 intel_Intel处理器真的胜过ARM了?安兔兔什么的才不可靠呢
  17. python的循环控制语句while和for的使用详解
  18. python中if elif else是什么意思_python中的elif是什么意思
  19. 个人实名认证的几种方案
  20. hdoj 4417 Super Mario 【树状数组 + 思维】

热门文章

  1. dockerq启动报错(iptables failed: iptables --wait -t nat -A DOCKER -p tcp -d 0/0 --dport 9876 -j DNAT --t
  2. centos下防火墙设置
  3. Linus系统下查看系统版本
  4. 手机知识:手机充电必备的几个小技巧,赶快看一下吧!
  5. 程序语言基础:解释程序基本原理笔记
  6. word2010添加b5纸张大小_纸张幅面规格尺寸你了解吗?
  7. java ibm 2035,C# java 连接 IBM MQ时出现 2035 或 2013认证错误的解决方法
  8. 新乡台达服务器驱动器维修,台达DELTA伺服驱动器维修
  9. python爬虫面试题
  10. 多边形的时针方向与法线方向