Groovy
Groovy is a general purpose language that runs on the top of the JVM, the Java Virtual Machine that seems to allow developers to be a lot more productive than using Java.
My primary reason for looking into it is that it is the language used for the Jenkins Pipelines.
Oh and you can also grab my Groovy book.
Articles
- Hello World in Groovy print, println
- Reading from STDIN in Groovy Console, System, readLine, System.in.newReader().readLine() as Integer
- Groovy types boolean, int, byte, Integer
- Groovy: Undeclared variable - runtime exception - groovy.lang.MissingPropertyException def, MissingPropertyException
- Groovy: Number guessing game Random, nextInt, System.in.newReader().readLine
- Determine type of an object in Groovy instanceof, getClass
- Lists in Groovy List, ArrayList, LinkedList, size, for, each, eachWithIndex, count
- Sum of numbers File, readLines, LineNumberReader, newReader, readLine
- Solution to the "Color selector" exercise
- Read CSV file in Groovy @Grab, import, File, getText, parseCSV, separators, readFirstLine
- Solution to the count digits exercies
- reading and writing files - appending content in Groovy File, getText, readLines, newReader, readLine, write, append
- listing the content of a directory, traversing a directory tree eachFile, eachFileRecurse
- Groovy - Regular Expressions - regexes
- Groovy map (dictionary, hash, associative array) map, containsKey, key, value, keySet, sort (keys, values)
- Groovy Read/Write JSON files
- Groovy: Date, Time, Timezone
- Groovy: import and use functions from another file
- Groovy: Random numbers, random selection from list of values
- Groovy: Closures
- Groovy: remove spaces from a string (replaceAll)
- Groovy: temporary file with auto delete (File, createTempFile, deleteOnExit, absolutePath)
- Groovy: relative path (SourceURI, Path, Paths, get, getParent, resolveSibling)
- Return multiple values from a function
- String length
- Substring
- for loop - break - continue
- Groovy code reuse
- Groovy functions
- Groovy: evaluate code in another file
- Groovy classes
- Groovy function overloading
- Groovy variable scope (def)
- Recursive functions
- Groovy command line arguments (args)
- Command line parameters
- Groovy exit - System.exit - early exit from Groovy script
- Groovy file and directory attributes File, canExecute, isFile, isDirectory
- Groovy join elements of array (join)
- Groovy pop push
- Groovy: Formatted printing with printf and sprintf
- Groovy system properties
- Groovy path to current executable script
- Exception handling (try, catch, Exception)
- Throw (raise) exception
- Casting Integer, String, ...
- Printing Unicode characters from Groovy
- Import standard libraries
- Iterate over map keys
- Get the list of keys of a map as an ArrayList
- STDOUT/STDERR
- Read/Write YAML files
- Read/Write INI files
- Read/Write XML files
- HTTP request
Other resources
examples/groovy/enum_planets.groovy
enum planets { Mercury, Venus, Earth, Mars, Jupiter, Saturn, Uranus, Neptune } println("hi") println(planets) println(planets.Earth)
Elapsed time
examples/groovy/elapsed_time.groovy
import groovy.time.TimeCategory import groovy.time.TimeDuration def start = new Date() println(start) print("Press any key ...") def name = System.in.newReader().readLine() def stop = new Date() println(stop) diff = stop - start println(diff) TimeDuration td = TimeCategory.minus( stop, start ) println td
Groovy regex
1) Interpolation can be used 2) matcher.matches vs being null ? 3) \b to mark end of word
def exp = 'line' def matcher = (text =~ /\b$exp\b/) println("hi") if (matcher) { println("match") }
groovy time duration - elapsed time
import groovy.time.TimeDuration tdx = new TimeDuration(0, 0 , 0, 2000) println(tdx)
http://docs.groovy-lang.org/2.4.9/html/gapi/groovy/time/TimeDuration.html
duration:
println(currentBuild.getClass()) // class org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html println("duration: ${currentBuild.durationString}") // duration: 0.35 sec and counting println(currentBuild.getDuration()) // 397 (miliseconds) println(currentBuild.getDurationString()) // 0.4 sec and counting https://stackoverflow.com/questions/51788139/how-to-get-jenkins-build-job-duration
Published on 2018-05-24