What this blog series is not:
A deep dive into Groovy language
A deep dive into Groovy programming
Any kind of deep dive

What this blog series is:
A Groovy “Scripting” skill enablement guide
A starting guide for SAP-IS developers to explore scripting on their own.

Pre-Requisites: Basic understanding of how IDEs work. Basics of any programming langauage.

Table of Contents

  1. Table of Contents
  2. Introduction
  3. Why Groovy Script?
  4. Setting up IDE for Groovy Dev
    1. List of Prerequisites
    2. Installing Correct Groovy Version on your machine
    3. Download IntelliJ IDEA or Eclipse (or NWDS)
    4. Installing Groovy Plug-in
    5. Downloading SAP’s Script Libraries
    6. Setting up the path to Script API in IDE
      1. In Eclipse
      2. In IntelliJ IDEA
  5. Your First Script
    1. Vanilla “Hello World” Script
    2. “Hello World” Script with CI format, executed in CI
  6. What is next?

Introduction

Groovy Script is a key component in the SAP Integration Suite, especially in the context of SAP Cloud Platform Integration (CPI). It is a dynamic scripting language for the Java platform that enhances the productivity of integration developers.

  1. Purpose: In SAP Integration Suite, Groovy Script is used to implement custom logic during the integration process. It provides flexibility to manipulate messages, transform data formats, and handle complex integration scenarios.
  2. Integration: It seamlessly integrates with Java, allowing developers to use existing Java libraries and frameworks in their scripts.
  3. Features: Groovy offers features like easy-to-read syntax, robust error handling, and native support for XML and JSON, making it ideal for integration tasks.
  4. Use Cases: Common use cases in SAP CPI include message mapping, data transformation, content enrichment, and dynamic routing.

In a nutshell, Groovy Script is a powerful tool for developers in the SAP Integration Suite, enabling efficient and flexible integration solutions.

Why Groovy Script?

In SAP Integration Suite, you have the option to develop your custom scripts in two formats: GroovyScript and JavaScript. And you are free to choose whichever mode you prefer. Essentially, a significant factor is if you already know JavaScript. If you do, go with JavaScript as it does have its advantages and can be utilized beyond just Cloud Integration.

But then the question arises, why would you learn Groovy Script in that case? A most practical answer is that most Cloud Integration Interviews would require you to know Groovy Script rather than JavaScript.

However, preferring Groovy script over JavaScript in SAP Cloud Integration Development is often due to:

  1. Java Integration: Groovy is built on the Java platform, allowing seamless integration with Java libraries and functionalities, crucial in Java-heavy SAP environments.
  2. XML and JSON Handling: Groovy has excellent built-in support for XML and JSON, simplifying parsing and manipulation of these data formats in integration processes.
  3. Dynamic Scripting and Productivity: As a dynamic language, Groovy offers flexibility, concise syntax, and features like closures, enhancing developer productivity and code maintainability. (Note: JavaScript also has closures and other similar features)
  4. Robust Error Handling: Groovy provides superior error handling, especially useful in managing integration-specific exceptions.
  5. SAP Ecosystem Compatibility: Groovy’s widespread use in the SAP ecosystem ensures strong community support and a wealth of resources for SAP-specific integration challenges.

As you delve deeper into Scripting, all these points will start making sense.

Bonus for Developers with SAP PI/PO background, a lot of stuff in context of writing UDFs and Java Mapping transalates very well over to Groovy. So, your learning curve will be simpler. You will just need to acquaint yourself with Groovy Syntax and testing methods.

Setting up IDE for Groovy Dev

I have choosen to use IntelliJ IDEA just because it so happened. But if you have NWDS or Eclipse installed it will work seemslessly there too. However, please note that most of my demos will include screenshots from IntelliJ IDEA. I will include Eclipse screenshot wherever I can and if it makes sense.

List of Prerequisites

  1. Installing Correct Groovy Version on your machine
  2. Download IntelliJ IDEA or Eclipse (or NWDS)
  3. Installing Groovy Plug-in
  4. Downloading SAP’s Script Libraries
  5. Setting up path to Script API in IDE (adding dependency)

Installing Correct Groovy Version on your machine

Use below Groovy Script in your iFlow to get correct Groovy and Java version which your Cloud Integration runtime is using:

import com.sap.gateway.ip.core.customdev.util.Message

def Message processData(Message message) {
    StringBuilder sb = new StringBuilder()
    sb << "Groovy: ${GroovySystem.getVersion()}\\r\\n"
    sb << "Java: ${System.getProperty('java.version')}\\r\\n"
    message.setBody(sb.toString())
    return message
}

If you use this in your iFlow as shown below and use basic CI Simulator you can see the effective version of Groovy and Java:

Now, you need to have the Groovy installed as per the version mentioned above from https://groovy.apache.org/download.html#distro

I am assuming you already have JDK installed but if not please have JDK installed as per shown version.

Download IntelliJ IDEA or Eclipse (or NWDS)

All the tools required for Groovy Scripting are open source and one google away.

If you already have NWDS you can just add Groovy plug-in (as shown in next step) to it and its ready for basic dev.

Installing Groovy Plug-in

In IntelliJ IDEA, Groovy comes bundled in default distribution or installation file as can be seen here:

However, In Eclipse, you need to add it from Marketplace:

Downloading SAP’s Script Libraries

To be able to use the full capability of Camel and Groovy, you need to download the .jar library provided by SAP, so that you can write and test programs in the IDE of your choice.

To get the latest library head over to the https://tools.hana.ondemand.com/#cloudintegration

Scroll to the “Using Script API” section and download the scipt API jar file.

Setting up the path to Script API in IDE

Now, we need to add previously downloaded library file to existing projects or new projects so that we can work with Groovy Scripts.

In Eclipse

Go to Build path of your project:

Add the external jar file.

Navigate to the directory where you saved the file in previous step and add it. After that click “Apply and Close”

You are now set with Eclipse.

In IntelliJ IDEA

You go to your “Project Structure -> Global Libraries -> + -> Java”

You navigate to your locally saved Script API jar file and add it. A pop will confirm that this library will be added to your currently opened project.

You can control at module level what libraries your project uses as shown below:

Click “Apply” and “Ok”

Now you know that how to add the Scipt API in Eclipse/IDEA, you are set for your “First Groovy Script”. We will repeat these steps in our projects whenever we want to Build or Execute Groovy Scripts for CI.

Notes: We might need to add few more dependencies (libraries) as we move to new learnings but we will cross that bridge when we get there. Moreover, we will follow same the process to add new dependencies.

Your First Script

There are two things we will do:

  1. Vanilla “Hello World” script. To test if IDE is set up for Groovy Language.
  2. “Hello World” Groovy Script. Using SAP’s Script API to generate a “Hello World” message. This script will follow same template as seen in your Cloud Integration when you open a fresh Groovy Script.

Vanilla “Hello World” Script

Create a Groovy project:

Create a new Groovy Script under “src” package

Use basic code to print hello world and execute it.

“Hello World” Script with CI format, executed in CI

This is where our external Script API will come into picture. However, note that at this point the script API is used for only syntactical and build validation and not for the message simulation, that we will do in upcoming blogs.

We will follow the same process and create a new “Hello World” project and script (Or you can modify the Vanilla one).

Replace the vanilla code with this one:

import com.sap.gateway.ip.core.customdev.util.Message

def Message processData(Message message) {
    //code here
    return message
}

Immediately, you will see errors popping up on Message and processData object, this is due to missing dependency on Script API file we downloaded earlier.

We need to add the Script API jar file to project path. As mentioned in prerequisites section add the file to project path for dependency. “Apply and Close”

You will notice the errors going away. In IntelliJ IDEA you will need to “rebuild the project”.

Now we will modify the code a little bit to populate the message body with the text “Hello World”.

import com.sap.gateway.ip.core.customdev.util.Message

def Message processData(Message message) {
    //code here
    message.setBody('Hello World')
    return message
}

Now if you copy and use this code in your iFlow from earlier, you can again simulate the script and see whats being populated in the message body.

Now replace the default code there with below code (you can copy from ealier steps as well):

So, we now know how to use the script you developed in your IDE, inside your iFlow without worrying about invalid syntax and method issues.

Note: Though we tested the basics directly inside Cloud Integration iFlow, we will learn in Blog 4 of this series, how to mock a Camel Message and do these tests in IDE first, before deploying them on the Cloud Integration.

What is next?

Now that we have IDE part set, adding dependency process sorted. We will now first understand the basics of Groovy Scripting with a brief “Introduction to Groovy Language”.

Have doubts or question? Shoot me a note in comments.

See you in next blog.