October 1, 2021

Git info in Mule-application

Hello everyone!

Let’s get acquainted, my name is Aleksei!

I have been creating server applications in Java for over 10 years using various technologies. Having developed enterprise solutions using SpringBoot for a long time, I am used to using convenient practices such as health check or version info, that I can receive a large amount of useful information through Actuator. Unfortunately, the actuator does not come with Mule. But we are programmers and we will always find a way out of every difficult situation.

Today we are going to talk about how to add git commit information for the Mule application.

Quite often it happens that you need to get information about the version of a running application online. In order to get online information about the application version in the Mule application, the git-commit-id-plugin plugin will help us. The plugin will receive the necessary information from your GitHub repository upon build and will carefully put it into a file in your project. For Mule – applications, the plugin is quite easy to configure.

The first thing we need to add is the plugin dependency in the pom.xml of your project

<plugin>  
    <groupId>pl.project13.maven</groupId>  
    <artifactId>git-commit-id-plugin</artifactId>  
    <version>4.0.0</version>  
    <executions>  
        <execution>  
            <id>get-the-git-infos</id>  
            <goals>  
                <goal>revision</goal>  
            </goals>  
        </execution>  
        <execution>  
            <id>validate-the-git-infos</id>  
            <goals>  
                <goal>validateRevision</goal>  
            </goals>  
        </execution>  
    </executions>  
    <configuration>  
        <generateGitPropertiesFile>true</generateGitPropertiesFile>  
        <generateGitPropertiesFilename>  
            ${project.basedir}/src/main/resources/git.json  
        </generateGitPropertiesFilename>  
        <format>json</format>  
    </configuration>

</plugin>

Let’s take a closer look at the configuration.

For starters, the version of the plugin itself was selected 4.0.0 as compatible with the java 8 version, but you can choose a more recent version.

In the <executions> section, we can see that the plugin is launched at several stages of the maven build.

<executions>  
    <execution>  
        <id>get-the-git-infos</id>  
        <goals>  
            <goal>revision</goal>  
        </goals>  
    </execution>  
    <execution>  
        <id>validate-the-git-infos</id>  
        <goals>  
            <goal>validateRevision</goal>  
        </goals>  
    </execution>  
</executions>

The default revision stage is executed in the maven stage of the build initialize will generate a file with git info. The default <goal>validateRevision</goal> is executed in the maven phase of the package build will verify that the git info file has been successfully generated

In the <configuration> section:

The <generateGitPropertiesFile> tag is responsible for the simple creation of a file without the need for specific adjustments, i.e. all possible git information will be included in the file.

The <generateGitPropertiesFilename> tag is responsible for the location and name of the file. We deliberately place the files in the “/src/main /resources” directory, because in the future it will make it easier to display the file because the file will be located on the classpath and easily readable from the Mule application.

<format> json </format> – the format of the output file, selected json, which will make it possible to reflect it as it is, without additional formatting.

We will also add the file “/src/main/resources/git.json” to .gitignore because we do not need to store the file in git at all because it will be generated every assembly.

We should now be able to request a file via the API. To do this, add a new flow to our Mule application. We want our information about git in our case to be available at “/ version

    <flow name="versionFlow" doc:id="6a358c69-be3e-477a-b5f7-0bf4c1d688a8" > 
		<http:listener doc:name="Listener" doc:id="a758b654-2475-4d4a-8e8c-6215d75607d2" config-ref="HTTP_Listener_config" path="/version"/> 
		<ee:transform doc:name="Transform Message" doc:id="391e0096-ee69-42dc-b56c-3f03b6572174"> 
			<ee:message> 
				<ee:set-payload><![CDATA[%dw 2.0 
output application/json 
--- 
readUrl("classpath://git.json", "application/json")]]></ee:set-payload> 
			</ee:message> 
		</ee:transform> 
	</flow>

Since we have prepared a json file, we can use a simple message transform that will simply read the file content from the classpath.

<ee:transform doc:name="Transform Message" doc:id="391e0096-ee69-42dc-b56c-3f03b6572174"> 
	<ee:message> 
		<ee:set-payload><![CDATA[%dw 2.0 
output application/json 
--- 
readUrl("classpath://git.json", "application/json")]]></ee:set-payload> 
	</ee:message> 
</ee:transform>

Let’s run our application and check how it works!

Project with examples: https://github.com/leadex/git-extension

Aleksei Platonov

Head of development

Get in touch
  • Business Center 1, M Floor, Meydan, Nad Al Sheba, Dubai, United Arab Emirates
    +971 800 0320341