Many customers create shared libraries which contain flows, sub-flows, etc. to be re-used across the Mule projects. For example, common sub-flows can be used for logging, error-handling, shared business logic, etc. This article aims to provide information and guidance on how to create and import common libraries in Mule 4.
Below topics are covered in this article.
- Creating a common Library
- Publish common library as custom connector to anypoint exchange
- Import exchange asset to anypoint studio and use it another project
1. Creating a common Library:
Create a sample mule project (ex: commonlibrary) with two flows. One flow is error-flow.xml and another is log-flow.xml as shown in below pic.
We need to do below necessary changes in pom.xml to make this project ready for publishing.
- Modify the groupId of the Maven project to be your Organization ID in your project’s POM file
- Ensure that below plugin is added to pom.xml
- Add the below Maven facade as a repository in the distribution management section of your project’s POM file.
- Update the settings.xml file in your Maven .m2 directory. After you install Maven, the mvn clean command creates the .m2 directory. In Windows, the directory resides at <default-drive>\Users\YOUR_USER_NAME\.m2 that contains your Anypoint Platform credentials. The Maven client reads the settings file when Maven runs. (Assumption is that you have maven already installed)
Please note that <id> value in settings.xml must match with <id> value in distributionManagement section of pom.xml
<?xml version=”1.0″ encoding=”UTF-8″?>
2. Publish common library as custom connector to anypoint exchange
- Go to the folder where pom.xml is present. Execute the command mvn deploy.
- Once the command is executed is successfully, go to anypoint exchange. There you will see commonlibrary asset is reflected. Now this asset is ready to be imported to anypoint studio.
3. Import exchange asset to anypoint studio and use it another project
- Create a new mule project. (ex: muleproject). In Mule palette section click on Search in Exchange (as shown below).
- Another pop up will open. Enter the asset name (commonlibrary in our case) you want to import and add and click on finish. After that you will see that corresponding POM dependency gets added to POM.xml
- With Maven Facade you can consume connectors, Mule applications, and REST APIs published in Exchange. To consume an Exchange asset, add the Maven facade as a repository in the repositories section of pom.xml
- Now, to be able to import, it is necessary to create an “Import” configuration, which contains the name of the Mule configuration file that contains the sub-flow we want to use, in this case error-flow.xml, log-flow.xml are the ones we created.
- This will make imported sub-flows “Logger”, “Error” visible from the library in our Mule application as flow-reference, as depicted below