Introduction

Productivity comes from being effective, not repeating yourself. Laziness is also one of a three great virtues of a programmer. Once you understand how to deploy Mule app to multiple environments, there is no point in having to repeat yourself every time you create a new project. But, you also won’t want to miss it out just for the sake of convenience as creating an environment aware project is always a best practice.

Then, how could you keep being lazy without compromizing quality?

The answer is the template. MuleSoft promotes reusability with it’s containerizable app architecture and AnyPoint Exchange. I’ve created a environment aware mule project template and you can download and reuse from here.

Batteries included

This project template includes minimum batteries for you to solid start straight away.

Seperation of Concerns

There are 3 apps under src/main/app folder. businessLogic.xml, config.xml and errorHandling.xml. It is a good idea to seperate concerns from the start. This helps you to focus on particular one at a time and improves manageability.

businessLogic.xml : Business logic goes here. It already includes sampleFlow using environment variable and custom global exception strategy to be a reference.

config.xml : Global mule configuration elements goes here.

errorHandling.xml : Global custom exception strategy goes here.

Project configuration for multiple environments

mule-project configuration

mule-project.xml is located in root folder of the project.

development is configured to be a default target environment.

and it auto generates mule-app.properties file as follows. mule-app.properties file is located under src/main/app folder.

resource files

There are 4 environment properties files I’ve included. development, test, qa and production. You can find this under src/main/resources folder.

Within the resource files, I have pre-configured sample variables with comments including naming convention.

# You can comment like this in property file. # Naming convention : {Connector}.{PropertyName}={YourOwnValue} # e.g. smtp.username=JSmith# e.g. smtp.password=P@ssword# Naming convention when value needs to be different per flow : {flowName}.{Connector}.{PropertyName}={YourOwnValue}# e.g. sampleFlow.http.port=8888# e.g. sampleFlow.http.port=8081# 1. Uncomment following and update as per your needs#{Connector}.{PropertyName}={YourOwnValue}#{flowName}.{Connector}.{PropertyName}={YourOwnValue}sampleFlow.http.port=8888# 2. Update businessLogic.xml from src/main/app to use above when necessary# For example,# username="${smtp.username}"# 3. Update value of mule.env from mule-project.xml to target specific environment. # By default within template, you have development, test, qa and production. Development is the initial value.

Property Placeholder

As covered in Seperation of concerns, global mule configuration elements are to be specified in config.xml. I have configured Property Placeholder as follows. This configuration will pick up ${mule.env} value from mule-project configuration and load selected environment properties file accordingly.

Global Exception Strategy

It is a good idea to create one or more global exception strategies and reuse them from flows. So, I have included a practice from here. With this template, you will start with one global exception strategy and reference to it.

How to use the template

Option 1 : Create a project by importing deployable archive

Deployable archive can be downloaded from here. You can import this into AnyPoint studio and change project name as you like.

Option 2 : Create a project using a template from Exchange

You can also upload this template into Exchange and create a project from there. As of now, I can’t demonstrate this as Exchange 2.0 BETA currently doesn’t support uploading a template yet. But, pretty straight forward once available.

Test

Without doing anything, businessLogic.xml already includes sampleFlow listening to 8888 port. Thus, run project and test GET request to 127.0.0.1:8888

Summary

This template is intended to help creating environment aware project without having to write any code. I will keep add/improve templates which I think it might help productivity of mule developers.