Author: Sanket Kangle
DataWeave is the MuleSoft expression language for accessing and transforming data that travels through a Mule app. Transform message component is the dedicated processor which converts input format to required output format using DataWeave language. DataWeave is tightly integrated with the Mule runtime engine, which runs the scripts and expressions in your Mule app. By default, the Transform message component has the DataWeave script inline. We can externalize or move this transformation script to a separate dwl file and have XML reference it.
Following are the reasons for externalizing DataWeave script:
- Reusability: By externalizing DataWeave script, the same script can be used at multiple places without code duplication.
- Maintainability: By externalizing DataWeave script, its maintainability increases as a uniform structure is followed.
- Best practice: It is the best practice recommended by MuleSoft.
How to externalize?
- Open your Transform message properties.
- Click on the edit icon as shown in the image below.
- Select “File” instead by default “inline”.
- Give an appropriate name with .dwl extension and click on “OK”.
- The file should be created under src/main/resources and can be seen in Package Explorer.
- To do that, give a path while creating the file. An example is shown in the image below.
- It is good practice to externalize all dataweave scripts under one package in src/main/resources as shown in the exhibit below. Make it a habit to do this while creating any project.
- Following is how XML refers to these files