Professional Documents
Culture Documents
Groovy
Groovy
Groovy
Features of Groovy:
Why Groovy?
It can be used to orchestrate your pipeline in Jenkins and it can glue different languages
together meaning that teams in your project can be contributing in different languages.
It also offers many productivity features like DSL support, closures, and dynamic typing. Unlike
some other languages, it functions as a companion, not a replacement, for Java. Groovy source
code gets compiled to JVM Byte code so it can run on any platform.
Functions in Groovy
The def keyword allows use to define a function that we can use in the code.
hi()
def hi() {
println("Hello World!")
After the def keyword we provide the name of the function and then in parentheses the list of expected
parameters. In our first example there are no paramaters. Then within curly braces we put the body of
the function.
We can put the definition of the function anywhere in the file. A good practice would be to put all the
functions in the end of the file and put the main body of the code at the top. That will make it easier to
read.
Better yet, you might want to put all your code in functions and leave only a single call to a single
function in the main body of your code. That will probably help keeping the code clean.
In this example we created a function that was designed to add two numbers and return the result. We
can call it with exactly two numbers and it will return the sum.
Example:
return x+y
Example:
def parseArgs(String[] args) {
String usage = 'build.groovy [options] buildfile'