Article from: http://www.ciandcd.com the code from can be downloaded from github: HTTPS://GITHUB.COM/CIANDCD installation:
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
Reference:
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 basic syntax for easy access.
#!/usr/bin/env groovy//Hello worldprintln "Hello world!" Variables:you can assign values to Variables for later usedef x = 1println xx = new java.util.Date () println xx =-3.14 99392println xx = falseprintln xx = "groovy!" println x//creating an empty listdef technologies = []/*** Adding A elements to the list ***///as with JAVATECHNOLOGIES.A DD ("Grails")//left shift adds, and returns the Listtechnologies << "Groovy"//ADD multiple Elementstechnologies.add All (["Gradle", "Griffon"])/*** removing elements from the list ***///as with Javatechnologies.remove ("Griffon")// Subtraction Works alsotechnologies = technologies-' Grails '/*** iterating Lists ***///iterate over elements of a Listtec Hnologies.each {println "technology: $it"}technologies.eachwithindex {it, I-println "$i: $it"}/*** Checking List C Ontents ***///evaluate If a list contains element (s) (Boolean) contained = Technologies.contains (' Groovy ')//orcontained = ' Groovy ' in technologies//Check for multiple contEntstechnologies.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)/*** Ma Nipulating Lists ***///replace All elements in the Listcollections.replaceall (technologies, ' Gradle ', ' Gradle ')// Shuffle a listcollections.shuffle (technologies, New Random ())//clear a listtechnologies.clear ()//creating an empty Mapdef DevMap = [:]//add valuesdevmap = [' name ': ' Roberto ', ' framework ': ' Grails ', ' language ': ' Groovy ']devmap.put (' LastName ', ' Perez ')//iterate over elements of a Mapdevmap.each {println "$it. Key: $it. Value"}devmap.eachwithindex {it, I -println "$i: $it"}//evaluate If a map contains a keyassert devmap.containskey (' name ')//evaluate if a map contains a Valueassert devmap.containsvalue (' Roberto ')//get the keys of a mapprintln devmap.keyset ()//get the values of a mapprintln Devmap.values ()//groovy supports the usual if-else SyntaxdEF x1 = 3if (x1==1) {println "one"} else if (x1==2) {println "one"} else {println "X greater than"}//groovy also supports the ternary operator:def y = 10def 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 rangedef x3 = 0for (i in 0.) {x3 + = I}//iterate over a listx4 = 0for (i-in [5,3,2,1]) {x4 + = I}//iterate over an Arrayarra y = (0..20). ToArray () x5 = 0for (i in array) {x5 + = I}//iterate over a mapdef map = [' name ': ' Roberto ', ' framework ': ' Gra Ils ', ' language ': ' Groovy ']x6 = 0for (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, 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 closuredef sum = {A, B--println a+b}sum (2,4)//closures may refer t o variables not listed in their parameter list.def X7 = 5def multiplyby = {num--num * x7}println multiplyby (Ten)//I f you had a Closure that takes a single argument and you may omit the//parameter definition of the closuredef clos2 = {pri Ntln it}clos2 ("HI")/* Groovy can memoize closure results [1][2][3]*/def cl = {A, B, sleep (+)//simulate so Me 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)
Finish
Jenkins2 Groovy Getting Started