Groovy

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

What is Groovy?

 Groovy is a Java syntax compatible object-oriented programming language for the Java platform.


 It is both a static and dynamic language with features similar to those of Python, Ruby,
and Smalltalk.
 It can be used as both a programming language ad scripting language.
 Groovy uses a curly-bracket syntax .
 It was first released in 2003.
 Current and stable version of groovy is 4.0.0

Features of Groovy:

 Support for static and dynamic typing:


statically typed languages are those in which type checking is done at the compiling stage and
not during execution.
Dynamic typing syntaxes such as Groovy are those in which the check is performed during
execution.
 Concise, brief, direct syntax:
this allows developers that use Groovy to develop projects faster and easier.
 Relatively short learning curve:
this is a relatively simple language like Python or Ruby. It is easy for developers to learn.

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.

Passing parameters and returning a result

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:

def add(x, y) { -function is defined

return x+y

 z = add(2, 3) –passing the values

println(z) // 5 –result displaying

  Variables used in Groovy

 byte − This is used to represent a byte value. An example is 2.


 short − This is used to represent a short number. An example is 10.
 int − This is used to represent whole numbers. An example is 1234.
 long − This is used to represent a long number. An example is 10000090.
 float − This is used to represent 32-bit floating point numbers. An example is 12.34.
 double − This is used to represent 64-bit floating point numbers which are longer decimal
number representations which may be required at times. An example is 12.3456565.
 char − This defines a single character literal. An example is ‘a’.
 Boolean − This represents a Boolean value which can either be true or false.
 String − These are text literals which are represented in the form of chain of characters. For
example “Hello World”
Build Groovy Script

 We use import to use packages in script.

 def initializeBuildProcess(String[] args) -> this is how we declare a methods.

 buildUtils.assertDbbBuildToolkitVersion(dbbToolkitVersion ) -> Like if, we have assert to


chek whether the statement is true or not.
 We can use Parse method to pass the arguments.

 Example:
 def parseArgs(String[] args) {
 String usage = 'build.groovy [options] buildfile'

 CLIBuilder Provides a builder to assist the processing of command line arguments.


 Example :
 def cli = new CliBuilder(usage:usage,header:header).
 These options are used with cli commands
 -a display all files
 -l use a long listing format
 -t sort by modification time

 To define properties syntax is


 @Field BuildProperties props = BuildProperties.getInstance()-
 Here Buiild Properties is a property and props is an object we have created. We can access
this object for further usage.

 We can use list in groovy .


 List<String> buildList = createBuildList()

You might also like