To expand on the discussion in the comments (should really be a new question...) I'll lay out the fastest way to create a dataweave module. This isn't necessarily the best (there isn't much in the way of documentation around doing this), but it will work just fine.
Create a new project in anypoint studio. Pick something relevant to the library name. For example, if this is going to be a library around XML functionality, you would probably name it something like xml-dw-library
.
We'll need to change our pom.xml
with a specific build profile, dependencies, and repos to support a dataweave module. I've create a gist you can use a template here: https://gist.github.com/mikeacjones/52ab8e4e01eec4c49116ecaca5f2a9e3 You want to take the time now to update your groupId
as this dictates how you will use the module later. In my case, I'm going to use io.syntaxsugar
as I use this as my namespace for personal projects. Set an appropriate artifactId
as well.
In src/main/mule
go ahead and delete the default flow created there. It is still useful to create MUnit flows for testing, but we won't actually have a flow here.
In src/main/
, create a folder called dw
. Now, from here, the folder name and file names dictate your imports. In my case, I want my import to look like this: import * from io::syntaxsugar::XML
, so I'll create a file call XML.dwl
with this path: src/main/dw/io/syntaxsugar/XML.dwl
.
Next, I'm going to set my content (this is just an example so I'm using something I created earlier) with all of my functions. In this case, I'm going to use this one:
%dw 2.0
fun appendNamespace(data, nsSelector: (k: Key) -> Namespace | Null) =
data match {
case is Array -> data map appendNamespace($, nsSelector)
case is Object -> data mapObject do {
var ns0 = nsSelector($$)
---
if (ns0 != null) ns0#"$($$)": appendNamespace($, nsSelector)
else ($$): appendNamespace($, nsSelector)
}
else -> data
}
Your project should look something like this at this point:
- Now that we've created our module, we can install it into our local maven repo (or if you have a central repo, you can setup your distribution management).
- And finally, to use it, we can add this module as a dependency to projects now:
<dependency>
<groupId>io.syntaxsugar</groupId>
<artifactId>xml-dw-library</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>dw-library</classifier>
</dependency>
And then use it like so in a dataweave script:
This lets you keep your dataweave modules separate and reusable (and tracked under separate source control), and you can also apply CI/CD to the dataweave modules themselves and unit test separately from your mule applications.
You'll hopefully have noticed in the POM.xml we defined test resource folders; these actually allow you to create dataweave unit tests using dataweave. For each test, create a folder for the test (example: appendNsTest), and then in the folder you can create a folder called inputs that you place a file for each input (example: payload.json
or vars.json
), out.json/xml/csv/etc
, and transform.dwl
. The transform.dwl
is executed and the results compared against out.*
.
This is a lot, so I've taken the working example I used for screenshots and published it to this public repo: https://github.com/mikeacjones/xml-dw-library