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
-
Read CSV file in Groovy @Grab, import, File, getText, parseCSV, separators, readFirstLine
-
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 map (dictionary, hash, associative array.md) map, containsKey, key, value, keySet, sort (keys, values)
-
Groovy: Random numbers, random selection from list of values
-
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)
-
Groovy variable scope (def)
-
Command line parameters
-
Groovy file and directory attributes File, canExecute, isFile, isDirectory
-
Exception handling (try, catch, Exception)
-
Casting Integer, String, ...
-
STDOUT/STDERR
-
Read/Write YAML files
-
Read/Write INI files
-
Read/Write XML files
-
HTTP request
Other resources
enum planets {
Mercury,
Venus,
Earth,
Mars,
Jupiter,
Saturn,
Uranus,
Neptune
}
println("hi")
println(planets)
println(planets.Earth)
Elapsed time
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
- Interpolation can be used
- matcher.matches vs being null ?
- \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