文章来自: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. 在C#中创建和读取XML文件

    1.创建简单的XML文件 为了便于测试,我们首先创建控制台应用程序,项目命名为CreateXml,Program.cs代码如下: 这样会在C盘根目录下创建data2.xml文件,文件内容为 using ...

  2. 【C#】 一些不常用,很容易混淆的知识点

    [C#] 一些不常用但很容易混淆的知识点 1. 访问修饰符 internal ,译为内部的, 在同一个程序集中可访问,它的内部是相对与程序集的,可不能想当然了 2. String.Compare 这个 ...

  3. Light OJ 1019 - Brush (V)(图论-dijkstra)

    题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1019 题目大意:Tanvir想从节点1的位置走到节点n的位置, 输出最短距离, ...

  4. IIS mime类型 任意类型

    HTTP头   任意mime类型   .*    application/octet-stream

  5. 时间转换为yyyymmdd

    Convert.ToDateTime(tbinpici.Text).ToString("yyyyMMdd")

  6. flash builder4.7bug

    flash builder4.7项目,swc中的button实例出来有bug,解决办法: 1,把button都改成movieclip. 2,改用swf做资源.

  7. .Net中几种常见的页面跳转传值方法

    1.ASP Server对象Execute方法 ASP Server对象的Execute方法可以在执行当前页面的过程中将另一个页面执行结果的内容插入到当前页面的输出中.Execute方法带一个参数,是 ...

  8. vim 中乱码问题

    在Linux下开发,经常遇到乱码问题:shell或者vim中显示不了中文,或者能够显示,但不能输入中文.每次都是上网去搜,或者同事告诉我一些命令来解决的.一直没有理解为什么会出乱码,本文就是想认真分析 ...

  9. SQLSERVER - Mysql 调试 笔记

    //性能SET STATISTICS IO on;  SET STATISTICS TIME on; //Mysql 切分字符串 CREATE  PROCEDURE proc_split(    in ...

  10. android 的touch event分析

    android中的事件类型分为按键事件和屏幕触摸事件,Touch事件是屏幕触摸事件的基础事件,有必要对它进行深入的了解.   一个最简单的屏幕触摸动作触发了一系列Touch事件:ACTION_DOWN ...