Tech

How to create parent-child projects in MuleSoft Mule4

Photo of Salumiah Syed, MuleSoft Senior Consultant Written by Salumiah Syed, MuleSoft Senior Consultant,   Apr 22, 2020

Today, I want to explain how to use pom.xml to resolve APIs' common dependencies that might lead to duplication and mismatch in MuleSoft Mule4.

The pom.xml file contains the core information about a project and its configuration details, including its dependencies, build directory, source directory, test source directory, plugin, goals etc. Maven reads the pom.xml file and then executes the goal.

In Mule4, all projects are 'mavenised' by default.

As per API-led connectivity design, a project may contain 'n' number of APIs (experience, process, system), each API with its dependencies. A situation when APIs share common dependencies could lead to duplication and mismatch. The best practice is to use a parent pom.xml file.

A parent pom.xml file (or super POM) in Maven is used to structure the project in order to avoid redundancies and duplicate configurations by using inheritance between different pom.xml files. If any dependency or properties are configured in both - parent and child - pom.xml files but have different values, then the child POM value takes priority.

Let me explain in detail using one of my projects as an example. In this project, there are three APIs, two of which are mapped to one version, while the third one is mapped to a different version. This configuration mismatch could lead to multiple issues. We can easily solve the problem by developing a parent POM.

By using the super POM, we force all dependencies of the child POMs to be derived from their parent POM.

How to create mule-maven-parent in a Mule4 project

Update the pom.xml file:

  1. By default, the <packaging> will be set to 'mule-application'. Update it to 'pom' <packaging>pom</packaging>

  2. Add the <sharedLibraries> if required, in my example, I use 'mysql'

  3. Add the child projects related <dependencies>

  4. Create an artifact using the maven command: mvn clean install -U

How to create a mule-maven-child

Update the pom.xml file:

  1. Add <parent> pom artifact into your config details

    <parent>
    <groupId>com.mycompany</groupId>
    <artifactId>mule-maven-parent</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    </parent>

  2. Add the <sharedLibraries> if required. In this example, I use 'mysql'

  3. Remove the <dependencies>,<repositories>, <pluginRepositories> from child projects. They will be inherited from parent pom

  4. Create an artifact using the maven command: mvn clean install -U

Finally, create Mule flows in mule-maven-child projects and run the application.

Note: Add the required <dependencies> to the parent pom.xml, generate the latest artifact and rebuild the child project before publishing.

You can find the source code here: mule-maven-parent.rar and mule-maven-child.rar

 

Did you find it useful? Let me know if you have any questions or if there are other issues you want me to cover in my next blogs.

Topics: MuleSoft Tech API

We’d love to hear your opinion on this post