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 ...
随机推荐
- Add Binary <leetcode>
Given two binary strings, return their sum (also a binary string). For example,a = "11"b = ...
- javascript中document.appendChild和document.body.appendChild的问题
在IE7中 var conentDiv = document.createElement("div"); document .body .appendChild(conentDiv ...
- SQL时间戳的使用
SQL时间戳的使用 一直对时间戳这个概念比较模糊,相信有很多朋友也都会误认为:时间戳是一个时间字段,每次增加数据时,填入当前的时间值.其实这误导了很多朋友. 1.基本概念 时间戳:数据库中自动生成的唯 ...
- 解除win7系统静音
#ifdef SPEAKERDEVMUTECONTROL# define SPEAKERDEVMUTECONTROL_EXPORT __declspec(dllexport)#else# define ...
- 小甲鱼python视频第八讲(课后习题)
1.for循环的使用,注意下面代码的区别 for i in range(0,10,2): print("i love you") for i in range(0,10): pri ...
- hdu 5748(LIS) Bellovin
hdu 5748 Peter有一个序列a1,a2,...,ana_1,a_2,...,a_na1,a2,...,an. 定义F(a1,a2,...,an)=(f1,f2,...,fn ...
- ios php RSA 非对称加密解密 der 和pem生成
ios 使用public_key.der加密 php 使用 private_key.pem解密 openssl req -x509 -out public_key.der -outform der - ...
- 7.5 [bx+idata] 书中错误
这节中的问题 7.1 有错误 题目和我自己的注释为: 用 Debug 查看内存,结果如下: 2000:1000 BE 00 06 00 00 00 ... 写出下面程序执行后,ax,bx,cx中的内容 ...
- Java 第十章 类和对象
类和对象 类与对象的关系是什么? 答 :类是具有相同属性和方法的一组对象的集合. 类是抽象的,对象是具体的:类是对象的模版,对象是类的实例. 定义一个类的语法是什么? public class 类名{ ...
- Jade之Includes
Includes jade允许利用include将其他文件(支持filters所支持的类型)中的代码嵌入当前代码中. jade: //- index.jade doctype html html in ...