Author: Rishabh Garg
This blog will help you to understand how to extract pom.xml values/properties into your MuleSoft code.
What is a POM.xml file ?
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.
Demo:
Steps:
- Open your Anypoint Studio & create a new Mule project.
- Create a dummy flow with some logger in it.
- Create one config.yaml file under src/main/resources & define some properties in it.
Now this is the main step of this demo, where we will learn how to get the values/properties from your pom.xml.
- Open your pom.xml file from your package explorer.
- Added resources tag after plugins tag under build tag.
Tags under resources:
- Directory: Maven will look for your project’s resources under src/main/resources
- Include: To include a resource, we only need to add an <includes> element.
- Exclude: To exclude a resource, we only need to add an <excludes> element.
- filtering: Maven will scan resources for property references surrounded by ${…}.
Extra Pointers:
- When specifying a resource directory, every file within that directory may not be used. Thus, we may have to specify only the files that we want to include or specify the files that we want to exclude.
- To include/exclude all properties and yaml file under src/main/resources, we can go with **/<fileName.ext>
- If you don’t want to exclude any file from the directory, in that case you don’t need to define a second <resource> tag under <resources>.
- Save the above changes & run your MuleSoft project.
After running the project in the console you will be able to see that these values are directly coming from your pom.xml file.
Use case:
- We will use this concept when we are pushing our logs to external logging applications like: Splunk, ELK etc and there we don’t want to hardcode the application name & version. So in that case every time we deploy our application to a newer version it will automatically change the application in the external logging system with a newer version.
For exp: In my current project I am sending MuleSoft logs to Splunk & below is log4j2 file.
The above screen is of splunk where you can see that for the same application we are having different versions & these versions are coming from pom.xml automatically & which makes it easy to find the logs.
Reference Links:
https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
https://maven.apache.org/plugins/maven-resources-plugin/examples/include-exclude.html#