文章来自: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. WSGI

    [WSGI] WSGI:Web Server Gateway Interface. WSGI接口定义非常简单,它只要求Web开发者实现一个函数,就可以响应HTTP请求.我们来看一个最简单的Web版本的 ...

  2. [z]vs中无法加入断点进行调试的解决方案

    http://blog.chinaunix.net/uid-15464162-id-3799069.html [ 1] 以前也遇到过同样的问题,但没有问个为什么,也没有探个毕竟.昨天调试一个DLL,添 ...

  3. 关于hql一些不常见但好用的技巧(个人总结)

    最近一直在用spring-data-jpa这个东西,感觉方法上注解hql语句已经是很常用的方法了, 有一些关于hql的经验分享一下: 一.hql的join hql的优势就是直接的关联关系嘛,但是通过h ...

  4. html meta中的viewport指令

    viewport含义: ViewPort <meta>标记用于指定用户是否可以缩放Web页面,如果可以,那么缩放到的最大和最小缩放比例是什么.使用 ViewPort <meta> ...

  5. $ajax引用DOM

  6. #import、#include、@class、@protocol、@interface

    #include 它用于对系统自带的头文件的引用,编译器会在系统文件目录下去查找该文件.(注意重复引用) #import 是Objective-C针对#include的改进版本,确保引用的文件只会被引 ...

  7. php array转json、xml

    class Encode{ public static function jsonEncode($code,$message,$data){ $result = array( 'code' => ...

  8. ORACLE_簽核PROC帶游標

    CREATE OR REPLACE PROCEDURE SP_C_TXYYMM(VAPPLY_NO IN VARCHAR2,VUSER_NM IN VARCHAR2,VFAC_NO IN VARCHA ...

  9. Android中EditText样式修改 聚焦光标、背景

    在Android开发中,根据项目的需求,需要定制一些特殊的样式,例如:使用EditText时,聚焦时的背景及光标图片使用自定义而非android系统默认的.这两天,在项目中涉及此需求,现记录如下: 首 ...

  10. cygwin下清屏的三种方法

    1. 做一个clear脚本,放到/bin下去 $vim /bin/clear #!/bin/bash cmd /c cls 2. ctrl + L 3. 在cygwind中install ncurse ...