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.pswp.cn/news/287172.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/287172.shtml
英文地址,請注明出處:http://en.pswp.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(…