Custom Modules in Mule 4 - DataWeave 2.0

Updated: Sep 4

Github repository with the Mule Project can be found at the end of the post.

Some days ago, I started using this great functionality in DW 2.0, and I faced some challenges while creating the code, so I figured I’d write a blog post about my experience.

MuleSoft’s documentation offers a great explanation of how to create these custom modules. One problem that I couldn’t find how to solve in the documentation was how to correctly load a property from the properties file into the custom module.

If you’re not familiar with custom modules, don’t worry, I got your back ;)

For this post, I’ll explain what a custom module is, why you would want to use it, some examples on how you can use it, and finally, how to load properties into this module.

What are Custom Modules in DataWeave 2.0?

First of all, let me tell you what a regular Module is in DW 2.0: from MuleSoft’s documentation, “DataWeave 2.0 functions are packaged in modules. Functions in the Core (dw::Core) module are imported automatically into your DataWeave scripts.”. Yes, all those functions that you use in your DW scripts, come from the Core module. Functions like ++, flatten, map, joinBy, that you may use on your day-to-day, come from Modules, you just don’t need to explicitly import them into your script. Other developers may call them libraries however in DW 2.0 they are called modules.

Mule offers a wide choice of modules that you can use to transform and manipulate your data, like the Arrays module, the Strings module, the Objects module, even Binaries or Tree. You don’t need to know how all of these work, just understand the basic meaning of a module, which is: functions created by MuleSoft that can be reused into your DW scripts.

Custom Modules are pieces of code that can be stored into an external file, in order to be reused in other parts of the Mule application. These are not created by MuleSoft, but by yourself, or your company.

Why are they useful?

Here is an example to illustrate why Custom Modules are useful: you just created a new function that will join an array of words into a complete sentence. Like this:

As you can see, we are using the joinBy function in the code to select which character we want our words to be separated by, in our complete sentence. If we were to change our code from joinBy “ “ to joinBy “-”, our final output would be "Hello-ProstDev-:)" instead.

If this piece of code, to create a complete sentence from an array of words, was used in several parts of your application, it would be a good idea to create a function inside a Custom Module to store this code. Then just reference that function from anywhere else in the rest of the app; instead of having to copy and paste this code into each part where you need to use it.

Let’s see how we can create this module for our Mule Application:

  • Create a “modules” folder inside your “src/main/resources” folder, and then create a new “.dwl” file inside the new modules folder, you can name it something like “Custom.dwl” (you can name the folder and the file however you prefer).

  • Inside this file, create a new function and paste the code that we had just created to generate the output sentence from our words array