Mule Common Processes

MULE COMMON PROCESSES

Purpose:

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. Picture1 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 filePicture2
  • Ensure that below plugin is added to pom.xml
<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>
  • Add the below Maven facade as a repository in the distribution management section of your project’s POM file.

<distributionManagement>

<repository>

<id>Repository</id>

<name>Corporate Repository</name>

<url>https://maven.anypoint.mulesoft.com/api/v1/organizations/${project.groupId}/maven</url>

<layout>default</layout>

</repository>

</distributionManagement>

  • 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″?>

<settings xmlns=”http://maven.apache.org/SETTINGS/1.0.0″

          xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance”

          xsi:schemaLocation=”http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd”>

  <servers>

    <server>

      <id>Repository</id>

      <username>myusername</username>

      <password>mypassword</password>

       </server>

  </servers>

</settings>

2. Publish common library as custom connector to anypoint exchange

  •  Go to the folder where pom.xml is present. Execute the command mvn deploy.
Picture3
  • 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.
Picture4

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).
Picture5
  • 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
Picture6
  • 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

<repository>

<id>Repository</id>

<name>Corporate Repository</name>

<url>https://maven.anypoint.mulesoft.com/api/v1/organizations/ORNIZATION_ID/maven</url>

<layout>default</layout>

</repository>

  • 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.
Picture7   Picture8
  • This will make imported sub-flows “Logger”, “Error” visible from the library in our Mule application as flow-reference, as depicted below
Picture9