Back to Blog Archive

Manage RAML API dependencies in Mule using Maven and Anypoint Exchange

Posted on: September 3, 2018
Author:
Sam

This post shows you how Maven can be used with Anypoint Exchange to consume your published assets.

Introduction

In November 2017, MuleSoft released Crowd which brought major changes to Anypoint Platform’s API Manager and Exchange. An API definition now begins it’s lifecycle in Design Center,  a web-based, syntax-aware environment for designing and documenting APIs. From Design Center, API Definitions are published to Exchange so they can be discovered and shared.

With the Crowd release Platform APIs were introduced for Exchange, including a HTTP Maven Facade which allows a Maven client to publish and consume Exchange assets.

Setting Up Maven to Use Exchange

In order for Maven to publish or consume assets from Exchange you first need to set up your settings.xml file with your Anypoint Platform credentials. You need to know your Anypoint Platform account details and Organisation Id. You can find your account’s Organisation Id by browsing to Exchange and looking in the URL: https://anypoint.mulesoft.com/exchange/?organizationId=[Anypoint Platform Organisation Id].

Replace the placeholders in the following snippet and save your settings.xml into your Maven /conf directory or your user .m2/ folder.

Publishing An API Definition as a Maven Artefact

The API Definition is published to Exchange from the Design Center. At this point you define a unique Asset Id and Asset Version, which along with your Organisation Id, form the Maven GAV. Exchange will not let you publish an asset with an Asset Id and Asset Version that are already in use without first deleting the existing version from Exchange.

Warning: You should avoid deleting and re-publishing an asset with the same version at all costs, as it may have already been downloaded by API consumers. When changing the definition of an API a new version number should always be used, even for very minor changes. 

Publishing an API Definition to Exchange creates four artefacts that are accessible from a Maven client.

Packaging Classifier Description
pom An XML file that contains information about the project
jar mule-plugin A Studio 7 plugin for a Mule 4 Connector based on the RAML definition
zip raml API Definition as defined in Design Center
zip fat-raml API Definition as defined in Design Center, with all Exchange modules included

Downloading an API Definition from Exchange Using Maven

To develop the API you need to download the API Definition from Exchange into the Mule project. When creating a new project, Anypoint Studio can download the RAML definition from an Exchange Asset and include it in src/main/api. If the RAML is then updated in Design Centre and a new version published to Exchange, API Sync can be used to download the latest RAML definition into the project.

To simplify this, Maven can be used to download the RAML definition from Exchange as part of a build. Adding the following plugin to your API project’s pom.xml will download the fat-raml file from Exchange as a zip, and unpack it’s contents into the src/main/api folder. Maven binds the plugin to the generate-sources phase of the Maven build lifecycle. This ensures that the latest API definition is used during tests and is packaged into the final artefact.

Note: As Anypoint Studio uses the files in this folder for generating flows and Datasense metadata it is important that they are updated, and not just the files output in target/classes.

Replace the placeholders in the below snippet with the Asset Id and Asset Version that was set when publishing the API definition from Design Center.

In this post I have visited how to add dependency on assets from your API design lifecycle through to your Mule implementation using Maven. I hope you found this useful.

Author:
Sam

Comments

Contact Us

Ricston Ltd.
Triq G.F. Agius De Soldanis,
Birkirkara, BKR 4850,
Malta
MT: +356 2133 4457
UK: +44 (0)2071935107

Send our experts a message

Need Help?
Ask our Experts!