Any senior developer is going to want to create a library of commonly used features that will be shared among MuleSoft applications. In Mule, we create a common, shared project that has reusable components and flows. Domain projects don’t allow reusable flows, so this is another great option.
Updated April 12, 2024
This can include many things, the most popular being:
- Global Error Handling
- Global Logging
- Common Business Logic Flows
This activity was completed using the following versions:
- Mule 4.2.2
- AnyPoint Studio 7.4.2
- Mule Mavin Plugin 3.3.5
I will show as much of the setup in AnyPoint studio UI as possible. We’ll only edit the files manually when necessary.
First, create a Mule project that you want to have your shared code in. I called my project “mule-common-flows”. If you commonly use a domain application, DO NOT use it for this project. This project will not run on it’s own, it will only be referenced by other projects that, in turn, can be domain based projects.
I added two Mule configuration files. One that will have Error Handlers, and another that will have a re-usable flow.
Once you’re happy with your re-usable components, we need to edit the POM.XML file. We need to add a “classifier” tag in the “org.mule.tools.mavin” plugin section like so:
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
<configuration>
<classifier>mule-plugin</classifier>
</configuration>
</plugin>
Then go to a command prompt and navigate to the source folder for this project and mvn clean command like so:
C:\Source\mule-common-flows>mvn clean package install
If you receive the message: “The packaging for this project did not assign a file to the build artifact”, then you may be using the mule maven plugin version 3.8.x or later, you’ll need to remove the package option, like so:
C:\Source\mule-common-flows>mvn clean install
You should receive a “BUILD SUCCESS” message. If you don’t, you’ll have to resolve whatever issues you have before continuing. This will build a JAR file and place it in the /target folder in the project.
Leave this project open in your Package Explorer, although you can close all the XML file tabs.
In the project in which you wish to reference the shared items, we have to manually edit the POM.XML file again. Add a section in the dependencies section like below:
<dependency>
<groupId>com.mycompany</groupId>
<artifactId>mule-common-flows</artifactId>
<version>1.0.0-SNAPSHOT</version>
<classifier>mule-plugin</classifier>
</dependency>
The dependency property values need to match the values in the POM.XML of the mule-common-flows project. The “classifier” property is additional.
Next, open one of the Mule Configuration Files in your project that is going to consume the mule-common-flows.
Click on the “Global Elements” tab.
Click on the “Create” button.
Navigate to Global Configurations – Import and click “OK”
DO NOT click on the ellipses to select a file. The file is already referenced in the JAR file of the mule-common-flows project. So you need to just manually enter the name of the configuration file. I’m going to add two, one for my global error handler and the other for the shared flows. Better to copy and paste the names to be sure. I put the names of the files in the Notes section too, so that they show up in the summary screen.
Your Global Configuration Elements should look something like the below. You can see how entering the names of the files in the notes is helpful.
Save all your changes.
Now to test them out. I’m going to setup my project to reference my new and shiny Global Error Handler. Click on “Create” again. Expand “Global Configuration” and click on “Configuration” and click “OK”.
Your Global Error Handler should now be in the drop down. If it’s not, then make sure you’ve saved everything. You can try closing and re-opening the project as we often need to do.
Click “OK” to save those changes.
Now, let’s go to the “Message Flow” tab. Drag a “Flow Reference” to the canvas if one doesn’t already exist.
Click on your “Flow name” drop down and you should now see our flow from the mule-common-flows project like highlighted below:
There you did it! Now go show off to all your colleagues.
References:
https://help.mulesoft.com/s/article/How-to-add-a-call-to-an-external-flow-in-Mule-4
How do we use connectors(Database,MQ) and properties of the shared/common flow in the main flow ?
Common flows are the same as any other flow that you create in MuleSoft. I’ve only used the standard Request and such in my common flows so that I don’t wind up with lots of dependencies that I don’t need in a particular microservice. If you create a common flow for DB or MQ access, then once you reference it, you should be able to use a flow or sub-flow in your common flows just as you would any other flow in your project.