文章来自:
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基本语法,方便大家查阅。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202#!/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)
完
转载于:https://www.cnblogs.com/itech/p/5627968.html
最后
以上就是香蕉汉堡最近收集整理的关于jenkins2 groovy语法的全部内容,更多相关jenkins2内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复