jenkins2 groovy语法

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/287172.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Android之glide加载图片圆角效果

1 问题 Android加载图片需要圆角化,有什么简单粗暴的方法吗?当然有,用我们的神器glide 2 解决办法 1)简单办法 ImageView imageView = (ImageView)helper.getView(R.id.keepHomeAppImageview);Glide.with(mContext).asBitmap().load(iconUrl) // .ov…

一维条形码***技术(Badbarcode)

【转】http://future-sec.com/badbarcode.html 前言在日常生活中&#xff0c;条形码随处可见&#xff0c;特别在超市&#xff0c;便利店&#xff0c;物流业&#xff0c;但你们扫的条形码真的安全吗&#xff1f;之前TK教主在PacSec介绍的条形码攻击和twitter上的demo视频太炫酷&…

ArcGIS 10.7 模型构建器Model Builder空间建模流程化作业案例----影像拼接与掩膜裁剪

Model Builder(模型构建器)是一个用来创建、编辑和管理空间分析模型的应用程序,是一种可视化的编程环境,通过对现有工具的组合完成新模型或软件的制作,为设计和实现空间处理模型(包括工具、脚本和数据)提供了一个图形化的模型框架。 本文以影像数据的拼接和掩膜裁剪为例…

《看聊天记录都学不会C语言?太菜了吧》(22)(必懂!题解 1-100 内素数)素数原来是质数!为什么你不早说!

若是大一学子或者是真心想学习刚入门的小伙伴可以私聊我&#xff0c;若你是真心学习可以送你书籍&#xff0c;指导你学习&#xff0c;给予你目标方向的学习路线&#xff0c;无套路&#xff0c;博客为证。 本系列文章将会以通俗易懂的对话方式进行教学&#xff0c;对话中将涵盖…

Hello Playwright:(4)自动化测试

利用 Playwright 提供的 API&#xff0c;我们在浏览器上做的很多事情都可以自动化。例如&#xff0c;搜索数据、填写表单和下载文件等等。但最适合的工作&#xff0c;就是自动化测试 Web 应用程序。自动化测试测试是软件开发中的一项基本任务&#xff0c;至少&#xff0c;你需要…

通才和专家:如何选择

原文&#xff1a;Generalists and specialists: thoughts on hiring作者&#xff1a;Nicholas C. Zakas 我的职业生涯经历过各种规模的公司&#xff0c;从非常小的五人创业团队到 13000 人的大公司雅虎&#xff0c;再到约 1000 人规模的 Box&#xff08;我目前所在&#xff09;…

Android之解决NestedScrollView嵌套ViewPager导致出现左右页面滑动冲突

1 问题 NestedScrollView里面嵌勒ViewPagerTabLayout&#xff0c;导致在这个页面监听不到左右页面滑动&#xff0c;需要解决这个监听滑动问题。 2 解决办法 val nestedScrollView: NestedScrollView mainView!!.findViewById(R.id.nestedScrollView)nestedScrollView.isFillV…

linux和裸机的区别,操作系统与裸机的区别

我们在学习stm32到一定阶段可能会了解操作系统&#xff0c;然后便有这种问题产生&#xff0c;下面我就来粗略说说“操作系统与裸机的区别&#xff0c;以及stm32能运行什么操作系统&#xff0c;能运行linux系统吗”等问题。操作系统与裸机的区别裸机运行的程序代码&#xff0c;一…

ArcGIS 10.7拆分多部件要素(Multipart Features)至单部件要素的两种方法

GIS中经常会出现多部件要素的现象,为了便于检查拓扑等关系,需要将其拆分为单个的部件。例如,在用同一个图层的多个图斑去裁剪(Clip)时,或者将多个不相邻的图斑进行合并(merge)时,可能会产生多部件要素,本文演示ArcGIS10.7版本中常见的两种拆分多部件要素至单部件要素…

spring-session + redis 实现集群 session 共享

2019独角兽企业重金招聘Python工程师标准>>> 目前市面上实现session共享的方案有很多&#xff0c;其中比较常用的是使用Tomcat、Jetty等web服务器提供的session共享功能&#xff0c;以此将session内容统一存放在数据库&#xff08;如mysql&#xff09;或者缓存&…

第三方的使用

1. MMDrawerController 抽屉效果 2.SVProgressHUD 透明指示层 3.SDCycleScrollView 无限轮播器 4.SDWebImage 异步图片加载 5.RESideMenu 抽屉效果 6.AFNetworking 网络请求 7.MJRefresh tableView上下拉刷新 8.MJExtension json转模型 9.Masonry 布局适配框架 10.UMengSocia…

《看聊天记录都学不会Python到游戏实战?太菜了吧》(10)无底洞的循环

本系列文章将会以通俗易懂的对话方式进行教学&#xff0c;对话中将涵盖了新手在学习中的一般问题。此系列将会持续更新&#xff0c;包括别的语言以及实战都将使用对话的方式进行教学&#xff0c;基础编程语言教学适用于零基础小白&#xff0c;之后实战课程也将会逐步更新。 若…

业务流水号规则生成组件

对于很多业务系统都需要生成业务流水号&#xff0c;如果订单号、购采单号等等&#xff1b;而这些业务流水号并不是简单的一个增长数值&#xff0c;它们很多时候都有一些不同的规则来定义&#xff0c;如不同类型的字母或地区拼音简写等。为了更灵活生成这些有规则的业务流水号Be…

Android之奔溃提示com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap

1 问题 Android端获取服务端的数据然后我直接把数据转hashMap提示错误如下&#xff0c; com.google.gson.internal.LinkedTreeMap cannot be cast to java.util.HashMap 2 解决办法 直接转Map集合即可 (t.data as Map<String, String>).forEach({if (KEEP_NAME.equals…

ArcGIS中国工具(ArcGISCTools)3.2 安装教程(附安装包下载)

ArcGIS中国工具,简称CTools,集成在ArcGIS 10.x系列版本中。本文在ArcGIS10.7的基础之上,演示3.2版本安装过程,并提供下载地址共大家学习和交流。 一、安装过程

函数式编程工具:filter和reduce

# -*- coding: utf-8 -*- #python 27 #xiaodeng #函数式编程工具&#xff1a;filter和reduce#python内置函数中&#xff0c;map函数是用来进行函数式编程这类工具最简单的内置函数代数#函数式编程含义&#xff1a; #一种编程范式&#xff0c;也就是如何编写程序的方法论&#x…

阿里云ECS,搭建MySQL5.7数据库环境

为什么80%的码农都做不了架构师&#xff1f;>>> 配置mysql yum源 [rootiZbp1j6oiamq7t2otpryarZ ~]# cd /data/ [rootiZbp1j6oiamq7t2otpryarZ data]# ll total 0###################################下载mysql源安装包# [rootiZbp1j6oiamq7t2otpryarZ data]# wge…

Python——通过斐波那契数列来理解生成器

一、生成器&#xff08;generator&#xff09; 先来看看一个简单的菲波那切数列&#xff0c;出第一个和第二个外&#xff0c;任意一个数都是由前两个数相加得到的。如&#xff1a;0,1,1,2,3,5,8,13...... 输入斐波那契数列前N个数&#xff1a; def fab(max): n, a, b 0, 0, 1 …

《看聊天记录都学不会Python到游戏实战?太菜了吧》(9)集万家之长不死 python

本系列文章将会以通俗易懂的对话方式进行教学&#xff0c;对话中将涵盖了新手在学习中的一般问题。此系列将会持续更新&#xff0c;包括别的语言以及实战都将使用对话的方式进行教学&#xff0c;基础编程语言教学适用于零基础小白&#xff0c;之后实战课程也将会逐步更新。 若…

公司c语言面试题目,c语言面试最必考的十道试题,求职必看!!!

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼6、free()函数问&#xff1a;下面的程序会在用户输入’freeze’的时候出问题&#xff0c;而’zebra’则不会&#xff0c;为什么?#include int main(int argc, char *argv[]) {char *ptr (char*)malloc(10);if(NULL ptr){printf(…