How to use Maven Archetypes to create MuleSoft API project templates



  • Maven is a dependency management utility that Mule implements to enhance project development.
  • All new projects created in Anypoint StudioX are configured Maven by default and one of its plug-ins is Maven Archetypes.
POM- (project object model):
  • This is a fundamental unit of work in Maven.
  • It is an XML file that contains information about the project and configuration details used by Maven to build the project.
  • We can declare the project dependencies in pom.xml
  • The artifact ID is also used as part of the name of the JAR file produced when building the project.
  • The output of the build process, the build result that is, is called an artifact in Maven.
ArtifactId GroupId
A unique name of the project A unique base name of the company or group that created the project.
  Implementation: Pre-Requisites: 1) Check whether maven is installed in our system/not. To check, Open cmd –>Run –> mvn –v –> (Displays the version of maven) 2) Settings.xml file should configure in C:\Users\username\.m2 Add settings.xml file with below config where it allows mule to configure other plug-ins. <settings> <pluginGroups> <pluginGroup></pluginGroup> </pluginGroups> </settings> Now, Create a template following the best practices include the following 1) Define the common error handler as part of template project, either using pom dependency or mule config file. 2) Define the env specific properties and secure properties file as per the requirement. 3) Define global-error.xml for global error handling. 4) Define the config file for connector configuration like Http, Salesforce, File, FTP etc. 5) Create separate folders to create DWL, Properties, Secure properties, Datatypes, Examples, certificates etc. 6) Add the dependency and configure the pom.xml as per the business requirement. 7) Configure the mule-artifact.json as per the business need.
  • Locate the project folder
Right click on project file –> properties –> Location
  • Run the following command to create maven archeType
mvn archetype:create-from-project “BUILD SUCCESS” represents we successfully generated “archetype” shown in project –> target –> generated sources –> archetype folder. Also check archetype created in com/mycompany folder. The project lies inside archetype folder. Do necessary modifications to the project depends upon requirement. Check whether the META.INF file contains all the folders included in the projects (ex: datatypes, examples). Provide any folder/file name with  __artifactId__  (artifact id can be assigned dynamically). Cross check finally if all the files included in the project or not. Goto:  C:\{project folder}\target\generated-sources\archetype Execute following command to create archetype-catalog, it will generate the groupId, artifactId, version and description of project. mvn clean install- archetype catalog (or) mvn install   archetype-catalog will be created in –> C:/users/username/.m2/repository Example: <archetypes> <archetype> <groupId>com.mycompany</groupId> <artifactId>template-archetype</artifactId> <version>1.0.0-SNAPSHOT</version> <description>template-archetype</description> </archetype> </archetypes> Execute a final command where we can actually generate our project using maven archetype. mvn archetype:generate  -DarchetypeGroupId=com.mycompany -DarchetypeArtifactId=demo-template-archetype -DarchetypeVersion=1.0.0-SNAPSHOT -DgroupId=com.mycompany -DartifactId=custom-template -Dversion=1.0.0 -DgroupId, -DartifactId –Dversion can be chosen based on our requirements. After successful execution –> “BUILD SUCCESS” project got successfully created using maven archetype. Import the project to check everything is okay.
  • After successfully importing the project to studio, we have to publish the project as a template to Anypoint Exchange.
  • Next -> Define ArtifactId, Name of the project, version and type ass “Template”
  • After successfully published ->We are able to see our template in AnyPoint Exchange (Master)Location.
Advantages of using maven Archetype:
  • Consistent structure among multiple projects.
  • Dependencies and reusable components are pre-configured, developer can focus only on logic.
  • Standardize project development.
  • Speed up your development time.
  • Enable developers to easily follow best practices.
Project structure is essential for a healthy development cycle. It is a crucial part of software integration.