{{announcement.body}}
{{announcement.title}}

Custom Maven Plugin in Mule 4

DZone 's Guide to

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.

· Integration Zone ·
Free Resource

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 

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

Anypoint access token


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(onenew 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

 

Topics:
anypoint platform, build automation, maven, mule 4, mule maven, mulesoft, mulesoft api, mulesoft open source, plugin development, raml 1.0

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}