Though I have worked on Java for more than a decade, I have not had a chance to work on Groovy. While working for API Integration into Jenkins CI/CD pipeline, I extensively used Groovy to invoke REST API, validate the user input parameters, and business logic for that. After that, I found that Groovy is a fascinating program language for Java developers.

Why Is Groovy Easy for Java Developers?

It allows to use the Java syntax liberally and tries to be as natural as possible for Java developers. It is an object-oriented dynamic programming language for Java virtual machine (JVM) and can be integrated smoothly with any Java Program. The groovy syntax is lucid, familiar, and direct that makes to develop projects faster and easier. It demands a shorter learning curve for Java Developer to develop, test, and integrate to make production-ready code in a short span.

What Is Groovy?

Apache Groovy is a powerful, optionally typed, and dynamic language, with static-typing and static compilation capabilities, and immediately delivers an application with powerful features, including scripting capabilities, Domain-Specific Language authoring, runtime and compile-time meta-programming and functional programming.

There are some of the key takeaways which I have encountered during the Jenkins pipeline Integration with API. The key takeaways explained below in the form of questions, answers, program including the output.

1. How To Define the Global Variable in Groovy?

Assign the value to a variable without def to define Global variable.

def comments = ‘Emergency build’ // It is not accessible inside the display method.

planId = ‘IMP2345667’ // Since the planId declared without def, hence is considered as a global and accessible inside the display method.

Java

def display() {

println "The Global varible is ${planId}"

}

//call the display method as below

display()

Output:

The Global variable is IMP2345667

2. How To Interpolate String in Groovy?

You can use either single quotes or double quotes to declare String.

For Example, You can declare planId as follows.

def planId=‘IMP2356790’

def planId=“IMP2356790”

When you print the String by single quote, the String interpolation would not happen. The ${} placeholders printed as it is as shown below.

println 'The Implementation planId is ${planId}’

Output:

The Implementation planId is ${planId}

Use double quotes for String Interpolation. Now, the ${} placeholders transformed with planId value as shown below.

println “The Implementation planId is ${planId}”

Output:

The Implementation planId is IMP2356790

3. How to check Not Null and Not Empty in Groovy?

The minimal code to check not null and not empty in groovy is to apply “?.trim()” on String as follows.

Java

def validateString(String inputField) {

if (!inputField ?.trim()) {

println "The value you entered is -${inputField}. Enter Valid Input of not null and not empty."

return

}

}

//Assign Null value to check the validateString

def planId = null 

 //Call the validateString by passing the pladId to validate the String

validateString(planId)

The output for validateString is as follows.

The value you entered is -null. Enter Valid Input of not null and not empty.

Note:

To check the Not Null for an object, do not use ?.trim() instead use obj !=null. Use ?.trim() only for Strings not for an object.

#integration #jenkins #jenkins pipeline #groovy 2.0

Lesson Learned on Groovy
1.15 GEEK