We all face the dilemma when it comes to maintaining different environment configurations inside the WSO2 ESB code base. There are multiple options, one among them is to mount a registry and keep all the properties based on the environment in the registry resource. But, what if I tell you that there is an easier way of doing this, completely using maven.
Yes, we can build our ESB code, just like how JAVA code is built. All we need are property files for each environment and a little knowledge about maven,
Let us assume that we have an endpoint, something like the one mentioned below.
http://localhost:8280/ServiceA
which changes its URI for each environment, i.e., the hostname changes from localhost to qa-host in the QA environment, uat-host in the UAT environment, and prod-host in the Production environment. Our goal here is to build the source code with the endpoint of the specific environment, to which we are going to deploy the code.
So, we are going to change the endpoint artifact as mentioned below.
http://HOSTNAME:8280/ServiceA
Here, at the build time, we are expecting the HOSTNAME to be replaced with the actual hostname of the specific environment. We will be working on the POM.XML of the ESBConfig project, as that's where all our artifacts reside.
To achieve this, we are going to use property files for each environment, and a plugin for maven, called com.google.code.maven-replacer-plugin, which replaces the properties in the code with values for those properties. The way we achieve the properties to be replaced with actual values, based on the environment would be by using Profiles in maven. So when we mention the profile at the time of build, maven will pick the profile and the property file defined for the profile, and replace the properties with their values at the time of build, for the profile we selected.
Steps
Create a folder Properties, and create property files for each environment (DEV.properties, QA.properties, UAT.properties, and PRODUCTION.properties) inside the ESB Config project.
Add a property called HOSTNAME in each property file, and provide the value for the same, according to your need (like HOSTNAME=localhost for DEV, HOSTNAME=QAHOST for QA, and so on), and save the files.
Go to POM.XML of the ESB Config project, and add the following lines, before build tag.
The quoted example has only dev profile, but add as many profiles as you need and remove the activeByDefault tag if it is not your default environment.
In the tag tokenValueMap, you can see that we have given the location of the property file of the specific environment, for this particular profile, repeat the same for other profiles as well.
In the tag include, we have included only the files with *.xml extension, to replace the properties and build the WSO2 XML artifacts alone.
Once done with the pom.xml, save it and build the code.
Build for Environment:
To build code for DEV environment, execute mvn clean install, or mvn clean install -Pdev, the hostname of the endpoint will be replaced with the value of HOSTNAME property at the time of build, and your car file will have the actual endpoint value localhost, instead of just HOSTNAME.
For other environments, execute mvn clean install -P{profileId}, and the hostname of the endpoint will be replaced with the value of HOSTNAME property at the time of build, and your car file will have the actual endpoint value, instead of just HOSTNAME.