Creating and using Template Project in Mule
Template projects are great way to define standards of designing applications across organizations and can be used as a prototype to build similar applications and to share and reuse work across organization.
I have tried to explain the steps to create a template project, publish template project to exchange and reuse it with the help of below POC where i have defined global error handler, global.xml for Global configuration Elements and properties file in template project.
Steps:
- Create a new Mule Project in Anypoint Studio.
2. Open project’s pom.xml and modify groupId as your org’s Id, add description, add classifier as “mule-application-template” under maven plugin configuration and exchange repository under distribution management in order to publish template to exchange as shown and highlighted below:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>2b4250b3-0421-4fa9-8e2a-126b5ef25a5a</groupId>
<artifactId>mule-template-project</artifactId>
<version>1.0.0</version>
<packaging>mule-application</packaging>
<name>mule-template-project</name>
<description>This is a sample project template project</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<app.runtime>4.5.1</app.runtime>
<mule.maven.plugin.version>4.0.0</mule.maven.plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-clean-plugin</artifactId>
<version>3.2.0</version>
</plugin>
<plugin>
<groupId>org.mule.tools.maven</groupId>
<artifactId>mule-maven-plugin</artifactId>
<version>${mule.maven.plugin.version}</version>
<extensions>true</extensions>
<configuration>
<classifier>mule-application-template</classifier>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.mule.connectors</groupId>
<artifactId>mule-http-connector</artifactId>
<version>1.8.0</version>
<classifier>mule-plugin</classifier>
</dependency>
<dependency>
<groupId>org.mule.connectors</groupId>
<artifactId>mule-sockets-connector</artifactId>
<version>1.2.3</version>
<classifier>mule-plugin</classifier>
</dependency>
</dependencies>
<repositories>
<repository>
<id>anypoint-exchange-v3</id>
<name>Anypoint Exchange</name>
<url>https://maven.anypoint.mulesoft.com/api/v3/maven</url>
<layout>default</layout>
</repository>
<repository>
<id>mulesoft-releases</id>
<name>MuleSoft Releases Repository</name>
<url>https://repository.mulesoft.org/releases/</url>
<layout>default</layout>
</repository>
</repositories>
<distributionManagement>
<repository>
<id>anypoint-exchange-v3</id>
<name>Exchange Private Repository</name>
<url>https://maven.anypoint.mulesoft.com/api/v3/organizations/${project.groupId}/maven</url>
<layout>default</layout>
</repository>
</distributionManagement>
<pluginRepositories>
<pluginRepository>
<id>mulesoft-releases</id>
<name>MuleSoft Releases Repository</name>
<layout>default</layout>
<url>https://repository.mulesoft.org/releases/</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
3. Create global.xml and global-error-handler.xml under src/main/mule and properties folder with properties file under src/main/resources as shown below:
4. under global.xml > Global Elements, add environment specific and common configuration with description so that can be easily understood by template user also add apikit router config as we are creating global error handler for Apikit router in our poc :
5. Now under global-error-handler.xml , define common error handler block as shown:
6. Now save changes made and run maven command to publish template project to exchange using command “mvn deploy”:
Since we have published our template project to Exchange, we will now use this template project to create a new Mule project.
Click New>File> project from Template.
select published template from exchange:
click open and project will appear in your studio.
Now rename project and files as per your requirement.
Right click on project > Anypoint Platform> Download Raml from design center and select Raml and click ok, flow will be added as shown:
Now remove error handling from flow sapi-main and refer global-error-handler:
Remove Http Listener config and apikit config from sapi.xml and paste in global.xml :
Similarly we can use configuration properties file.
Now we will save project, deploy it and test it.