DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workkloads.

Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • How to Build a New API Quickly Using Spring Boot and Maven
  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • Instant App Backends With API and Logic Automation
  • Unleashing the Power of GPT: A Comprehensive Guide To Implementing OpenAI’s GPT in ReactJS

Trending

  • How to Format Articles for DZone
  • Beyond Microservices: The Emerging Post-Monolith Architecture for 2025
  • How Clojure Shapes Teams and Products
  • APIs for Logistics Orchestration: Designing for Compliance, Exceptions, and Edge Cases
  1. DZone
  2. Data Engineering
  3. Databases
  4. Custom Maven Plugin in Mule 4

Custom Maven Plugin in Mule 4

Custom Maven plugin raml-sync-checker is used to check if the RAML in mule 4 app - API implementation is in sync with the RAML published in Anypoint Exchange.

By 
Arpit Singh user avatar
Arpit Singh
·
Aug. 31, 20 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
10.3K Views

Join the DZone community and get the full member experience.

Join For Free

Problem Faced

Last week, I received a minor Change Requirement from the Client to add a field in the API Specs (RAML)

The requirement was critical, and so I quickly made the RAML change in code, pushed it to Github Repo, and got it merged to Dev. 

What I missed was: updating the same change in the corresponding API in exchange.

Though I pushed those changes to exchange the next day (I got another Change Request, and while publishing the RAML change to Exchange, I remembered what I missed. 

By the way, we do have a checklist to be followed for every new CR that is raised, but the change was so minor and critical, that I neglected the Checklist!

What is RAML Sync Verifier Plugin?

The custom plugin:

  • Checks if the Raml in API implementation project is in Sync with the Raml in Exchange

  • Gives you options to pass various configurations

  • Fails the build if the RAMLs are not in sync with each other (This configuration can be turned off).

Why Is It Required

The RAML published to Exchange and the Raml in your API implementation should be in SYNC for a particular environment. 

There are plenty of good reasons for that: 

  • Maintaining consistency of code

  • Version control

  • Reusing Fragments and canonicals 

Note: Adding the API ID (From API Manager) and Autodiscovery will not make the RAML in code in sync with exchange.  

I hate making the same mistake twice (At least when it comes to Tech Stuff!), so I created a very simple custom Maven plugin, which, during the build process, checks if the RAMLS are in sync.

Source Code Link

I will be describing briefly about the Plugin code, and the complete code with example mule app can be found at GitHub Repo

Pre-Requisites:

  • Knowledge about developing Maven Custom Plugin

  • Knowledge about Mule 4 apps and Anypoint Platform

  • Anypoint Studio Installed 

  • Maven installed 

  • Eclipse installed

  • Valid AnyPoint Credentials

  • Have developed a simple Mule app with a basic API spec in design center using above credentials

About the Code

Project Structure

Project structure

The Plugin has just one Mojo. A Java mojo consists simply of a single class representing one plugin's goal.

Exchange API Used 

  • Access Management API: For obtaining Access Token

  • Cloudhub Graph API: To Get the URL to download Raml (Zip) from Exchange

About The Mojo

  • Goal Name: verify-raml

  • Default Phase: TEST

  • Parameters Matrix

 

Property Name

Required/
Optional
Default Value Remarks

scope

Optional

Test

Scope of the goal

userName

Required

NA

Anypoint userName

password

Required

NA

Anypoint password

downloadDirectory

Optional

${project.rootDir}

Raml Sync Report Path

project

Required [Read-Only]

${project}

Object of org.apache.maven.project.MavenProject

failOnError

Optional

true

Throw Build Error if the raml sync check fails

exchangeVersion

Optional

Latest version of the raml in exchange

Only use if required

projectRamlPath

Optional

src/main/resources/api

Path to the raml root folder. Relative to ${project.rootDir}

Behind the Scene Logic

As mentioned in the Properties Matrix, you are required to pass the appropriate values of the parameters. The GitHub repo also has a sample mule app using the plugin, so you can see the usage of the plugin there as well.

 

Flow Diagram

Flow diagram


The flow diagram can also be found at draw.io.

Steps

  • Obtain Anypoint Platform Access token using https://anypoint.mulesoft.com/accounts/login

Anypoint access token


  • Using Cloudhub GraphQL API, get the URL to download the RAML from Exchange

URL for RAML download


  • Sample Response

Sample response


  • Download and unzip the zip file in a temp directory

  • Compare the unzipped folder with the folder present in the project (default src/main/resources/api/)

  • Using 

    Java
     




    x


     
    1
    Files.walkFileTree(one, new SimpleFileVisitor<Path>()


  • Comparison is done byte by byte in size, but can be expanded as per your requirements

  • If difference in size is detected, then detailed logs are displayed in the build console

getLog().error("=== comparing: " + file + " to " + fileInOther + " === Difference in Bytes : "   +(otherBytes.length - theseBytes.length)); getLog().error(file + " is not equal to " + fileInOther);

 

Note:

You might face an issue on importing the GitHub code in Eclipse. Please mark the goal descriptor as ignored.

 Error in Eclipse IDE


Testing the Plugin

Installing the Custom Maven Plugin in Your Local System

1.       Clone the custom plugin code from GitHub Repo.

2.       Open command prompt and browse to the cloned project Root Dir

3.       Run mvn clean install

Testing the Maven Plugin in A Sample Mule App

1.       Clone the sample Mule app from GitHub repo

2.       Open the command prompt and browse to the cloned project Root Dir

3.       Run mvn clean test.

 The plugin will be executed during the test phase based on the defaultPhase Configuration in the Mojo.

Java
 




xxxxxxxxxx
1


 
1
@Mojo(name = "verify-raml", defaultPhase = LifecyclePhase.TEST)


Notes on The Sample Mule App

  • You should include the plugin with its required configurations in order to use the plugin. 

  • You can check out the usage in the pom.xml of the sample app

Java
 




xxxxxxxxxx
1
41


 
1
<plugin>
2
    <groupId>com.apisero.plugin.maven</groupId>
3
    <artifactId>raml-sync-checker</artifactId>
4
    <version>0.0.1</version>
5
    <executions>
6
        <execution>
7
            <goals>
8
                <goal>verify-raml</goal>
9
            </goals>
10
        </execution>
11
    </executions>
12
    <configuration>
13
        <scope>test</scope>
14
        <anypoint.password>${anypoint.password}</anypoint.password>
15
        <anypoint.username>${anypoint.username}</anypoint.username>
16
        <download.directory>${download.directory}</download.directory>
17
        <raml.path>src/main/resources/api</raml.path>
18
        <exchange.version>latestVersionsOnly</exchange.version>
19
        <failOnError>true</failOnError>
20
   </configuration>
21
</plugin>




  • I have excluded the credentials from my pom.xml, and I would recommend creating a simple API in the design center using your any point credentials and test out the plugin. 

Test Runs

First Run

Raml in exchange is in sync with the raml in sample-mule-app

The plugin will be executed during the test phase based on the defaultPhase Configuration in the Mojo.


First run

@Mojo(name = "verify-raml", defaultPhase = LifecyclePhase.TEST)


Second Run

Raml in exchange is not sync with the raml in sample-mule-app. 

I manually added another endpoint in raml of sample-mule-app

The output on running mvn clean test:mvn clean test

 

Apache Maven app API Sync (Unix)

Opinions expressed by DZone contributors are their own.

Related

  • How to Build a New API Quickly Using Spring Boot and Maven
  • Unlock AI Power: Generate JSON With GPT-4 and Node.js for Ultimate App Integration
  • Instant App Backends With API and Logic Automation
  • Unleashing the Power of GPT: A Comprehensive Guide To Implementing OpenAI’s GPT in ReactJS

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!