Publishing MuleSoft common assets or libraries to Anypoint Exchange

  • February 23, 2024

Shared libraries, or common assets, in Mule 4 provide a powerful mechanism for promoting code reuse, enhancing maintainability and increasing development efficiency.

Step 1: Add Anypoint Exchange credentials into the settings.xml file of your local .m2 repository

To configure your local Maven repository, you must add your Anypoint Platform username and password to the settings.xml file. Insert the following code into your file, replacing ‘your_username’ and ‘your_password’ with your actual Anypoint username and password, respectively.

<servers>
  <server>
    <id>MuleRepository</id>
    <username>Anypoint_Username</username>
    <password>Anypoint_Password</password>
  </server>
</servers>

In this scenario, the serverId defined in the settings.xml file is crucial, as it’s used for both distribution management and the repository in the pom.xml file. Please note: If the settings.xml file isn’t present in the .m2 folder, you should create the file first, and then paste in the provided code.

Step 2: Add classifier in mule-maven-plugin

When you create any Mule project, the pom.xml file generates a mule-maven-plugin. Please add the following command within that plugin:

<configuration>
<classifier>mule-plugin</classifier>
</configuration>

publish-muleSoft-1

Step 3: Add Anypoint Exchange distribution management in the pom.xml file of your common library

<distributionManagement>
<repository>
<id>muleRepository</id>
<name>Corporate Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v1/organizations/{orgId}/maven</url>
<layout>default</layout>
</repository>
</distributionManagement>

publish-muleSoft-2

Refer to the image above.

The ID value must match the serverId defined in the settings.xml file of the local maven repository. In this case, the ID is muleRepository.

You must replace the orgId in the URL with your Anypoint Platform organization ID. You can get it from Anypoint Platform => Access Management => Business Groups => Click on your organization.

publish-muleSoft-3

Step 4: Modify the GroupId in your pom.xml file to reflect the Anypoint Platform OrganizationId

Make sure the GroupId in your pom.xml file matches the Anypoint Platform OrganizationId. (The process to obtain the organization ID from Anypoint Platform was described above.)

Step 5: Deploy the common library or asset in Anypoint Exchange

Run mvn deploy in cmd under workspace.

publish-muleSoft-4

After you execute this step, the asset or shared library will be visible in Anypoint Exchange.

publish-muleSoft-5

How to use these assets in other projects

Step 1: Copy the dependency from the Dependency Snippets and paste it into the project so all functions will be able to inherit.

publish-muleSoft-6

Step 2: Add the repository into your application’s pom.xml file:


<repository>
  <id>MuleRepository</id>
  <name>Anypoint Exchange Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v1/organizations/{orgId}/maven</url>
  <layout>default</layout>
</repository>

— By Sonu Mishra