Over a million developers have joined DZone.

Creating Reusable Groovy Scripts in MuleSoft (3.9)

DZone's Guide to

Creating Reusable Groovy Scripts in MuleSoft (3.9)

Read this article in order to view a tutorial on how to create reusable groovy scripts in MuleSoft 3.9.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.


MuleSoft developers around the world would be able to identify with me their challenges and issues when they are infusing Groovy scripts into their Mule applications. The challenge here is not so much about creating snippets of Groovy scripts in the mule application, but more about creating reusable groovy scripts in MuleSoft. As of this writing, there is no online content that will teach you how to create reusable Groovy scripts. The search results that appear would talk about SOAP UI groovy scripts, but you will find nothing that is relevant to what you need.

This article will attempt to teach the reader how to write reusable Groovy scripts and the best practices in infusing Groovy scripts into their Mule applications.

Installing the Groovy IDE Plug-In

Launch the Anypoint IDE, and at the top menu on the upper left-hand corner, go to Help>Install New Software, which will launch the “Install” pop up dialog. Once there, click on the add button on the right-hand corner of the first text box (as depicted in Figure 2.0a).

Figure 2.0a

Once the button is clicked, you will see another pop-up dialog asking for Name and Location (Figure 2.0b).

Figure 2.0b

Enter the following name and location into the popup at Figure 2.0b.

  • Name :groovy plugin
  • Localtion :http://dist.springsource.org/snapshot/GRECLIPSE/e4.5/

Once this is done, you will see that the install dialog displays the following selection for installation. Proceed to select all of them (Figure 2.0c), and click next to proceed with the installation.

Figure 2.0c

Once the installation is completed on your Anypoint IDE, proceed to the following section (3.0).

Creating the Groovy Package

You need to create a new Mule project in the Anypoint IDE and once that is done, you need to create a subfolder under src/main/resource. The new folder that you are creating would be the package root folder from which your Groovy file will reside.

Now create a new Groovy file called “HelloWorld.groovy.” The entire structure of your file would be as follows (Figure 3.0a):

Figure 3.0a

Enter the following snippet of code into the file:

package groovy

staticString greet(){
println"Hello how are you !!!"
return"Hello how are you!!!"

So what you have essentially done by naming the file “HelloWorld.groovy” is implicitly created a java class called “HelloWorld.” When you create methods/functions in the file, you are essentially creating functions/methods in the “HelloWorld” class. The first statement in the class is the package name, which should be the same as your folder name.

Incorporating the Groovy Script

Right after creating the groovy script file in section 3.0, we are now ready to incorporate it into our Mule application. We will create a simple Mule application that looks like the following (Figure 4.0a):

Figure 4.0a

The second message processor is the “groovy scripting message processor.” The Groovy scripting message processor configuration is displayed below (Figure 4.0b):

Figure 4.0b

It is that easy to reuse Groovy scripts, and if you follow my way of creating reusable scripts, you will no longer need to use the “Script File” configuration (displayed in Figure 4.0b).

Full Mule XML code is listed below:

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:scripting="http://www.mulesoft.org/schema/mule/scripting" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/scripting http://www.mulesoft.org/schema/mule/scripting/current/mule-scripting.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="" port="8081" doc:name="HTTP Listener Configuration"/>
<flow name="testgroovyFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
<scripting:component doc:name="Groovy">
<scripting:script engine="Groovy"><![CDATA[groovy.HelloWorld.greet()]]></scripting:script>

Debugging the Groovy Script

If you follow what I have shown you so far and incorporate the practice in all your Mule applications, you could even debug the Groovy script that you have written (apparently there are people who believe that they can never debug Groovy scripts in Mule applications, and this article will prove them wrong). If you set breakpoints in the following locations (Figure 4.0a and Figure 5.0a), you will be able to stop execution at the following points during runtime.

Figure 5.0a shows the blue dots as breakpoints.

Now run the application in debug mode, and in your internet browser, browse to the following URL http://localhost:8081/test. You will now see that the runtime execution stops at the first breakpoint (Figure 5.0b).

Figure 5.0b

If you move forward from the first breakpoint, the debugger will move into the Groovy script (Figure 5.0c).

Figure 5.0c

This shows that using this way in creating Groovy scripts in your Mule application not only encourages re-use, but also allows for Groovy scripts debugging. This will be really helpful when you have complex Groovy scripts to debug.


When you create Groovy scripts using the method I have shown you, then you will only need to enter minimal Groovy statements at the groovy message processors, and you could leave all the heavy lifting to the scripts declared in the script files. Doing it this way would also allow you to build Groovy scripts independently from your Mule IDE and test the groovy script independently to ensure it works before actually using them in the Mule application (this will speed up development time tremendously).

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

mulesoft ,groovy ,reusability ,debugging ,eclipse groovy plug-in ,integration ,mule

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}