jenkins2 groovy语法
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语法的更多相关文章
- Groovy 语法学习
一.配置 Groovy 环境: 下载 Groovy(Groovy 依赖 Java,所以需要 JDK 环境):http://www.groovy-lang.org/download.html 配置环境变 ...
- atitit.groovy 语法特性
atitit.groovy 语法特性 1. Groovy 1.6概览1 1.1. 多路赋值2 2. 新发布的Groovy2.0为这门语言带来了关键的静态特性:静态类型检查和静态编译:2 3. 参考3 ...
- Gradle系列之一 Groovy语法精讲
Gradle技术之一 Groovy语法精讲 gradle脚本是基于groovy语言开发的,想要学好gradle必须先要对groovy有一个基本的认识 1. Groovy特点 groovy是一种DSL语 ...
- Groovy语法基础
Groovy 简介 Groovy 是一种基于 JVM 的动态语言,他的语法和 Java 相似,最终也是要编译 .class 在JVM上运行. Groovy 完全兼容 Java 并且在此基础上添加了很多 ...
- jenkins2 pipeline 语法快速参考
jenkins2 pipeline中常用的语法快速参考. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.com/ciand ...
- groovy语法
1.注释1.1. 单行注释1.2. 多行注释1.3. GroovyDoc注释1.4. Shebang线2.关键词3.标识符3.1. 普通标识符3.2. 带引号的标识符4.字符串4.1. 单引号字符串4 ...
- 看懂Gradle脚本(4)- Groovy语法之运算符重载
继续讨论Task定义 回想一下前一篇文章的样例: task myTask { doLast { println 'hello world!' } } 这段脚本定义了一个名为myTask的任务.而且通过 ...
- jenkins2 groovy脚本参考
使用plugin生成groovy脚本,或者参考已有的groovy脚本. 文章来自:http://www.ciandcd.com文中的代码来自可以从github下载: https://github.co ...
- hibernate查询之Criteria实现分页方法(GROOVY语法)
public int searchTest(String name, Integer pageIndex, List<Test> resultList){ def criteria = T ...
随机推荐
- C语言提供了几个标准库函数 itoa() atoi()
C语言提供了几个标准库函数C语言提供了几个标准库函数,可以将任意类型(整型.长整型.浮点型等)的数字转换为字符串.以下是用itoa()函数将整数转换为字符串的一个例子: # include <s ...
- 使用 archetype插件创建maven目录结构
步骤一: 步骤二: 等待下载插件
- JQuery this 和 $(this) 详解
this this 在面向对象语言中,指代调用上下文对象,在js中,也是一模一样的概念,所以不管这个this出现在什么地方,只要追踪到当前调用对象是什么,那么this是什么也就一目了然了. 先看一个 ...
- nginx限制ip连接数和带宽
今天有个人问我,nginx怎么限制ip连接数,突然想不起来了,年龄大了,脑子不怎么好使了.还要看一下配置才想起了.那个人又问我,你测试过的吗?一下子把我问蒙了,我真没测试过了,也不知道启作用了没有.下 ...
- Exception异常规范
把异常的栈轨迹以String形式返回 /** * 把异常的栈轨迹以String形式返回,而不是直接打印到console * @author King * @time 2015-04-29 * @ret ...
- linux中压缩与解压缩命令
.tar 解包:tar xvf FileName.tar 打包:tar cvf FileName.tar DirName (注:tar是打包,不是压缩!) ——————————————— .gz 解压 ...
- 揭开HTTP网络协议神秘面纱系列(三)
HTTP首部字段有四种类型:通用首部字段,请求首部字段,响应首部字段,实体首部字段. 通用首部字段: 首部字段 说明 Cache-Control 控制缓存的行为 Connection 逐跳首部.连接的 ...
- EF 6 for mysql
1. NuGet安装 MySql.Data.MySql.Data.Entity 2.安装Entity Framework 6 Tools for Visual Studio 2012 & 20 ...
- 使用12c的DBCA创建数据库的时候报错TNS-04404
情况:之前使用的默认的pdb数据库pdborcl,连接的时候使用SQLdeveloper,配置了tnsname.ora,里面添加了pdborcl的service:这里报错是感觉tnsname配置错误了 ...
- hdoj 2680 choose the best route
Problem Description One day , Kiki wants to visit one of her friends. As she is liable to carsicknes ...