Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display application version in title using thymeleaf and springboot

I want to display in my htm page the version of my webapp, using something like this (thymeleaf inside) :

<h4 th:text="${version}">Version</h4>

The data is well set in the pom.xml :

<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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>fr.test.navig</groupId>
    <artifactId>navigo</artifactId>
    <version>2.0.3-SNAPSHOT</version>
...
<!-- Package as an executable jar -->
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <mainClass>Application</mainClass>
                        <addDefaultImplementationEntries>
                            true
                        </addDefaultImplementationEntries>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    </plugins>
</build>

and I can see it in the MANIFEST.MF (which is in the generated jar under META-INF) :

Implementation-Version: 2.0.3-SNAPSHOT

I've tried to get the appplication version in the controller and set it in a ModuleAttribute :

@ModelAttribute("version")
public String getVersion() {
    logger.info("ModelAttribute to get application version");
    return getClass().getPackage().getImplementationVersion();
}

But getClass().getPackage().getImplementationVersion() value is null. Indeed the package implementationVersion is not the implementation Version of the application by default.

like image 247
jayjaypg22 Avatar asked Sep 01 '16 15:09

jayjaypg22


1 Answers

I know I'm late but Patrick's answer and Spring docs greatly helps in this matter.

1. If your pom.xml use spring-boot-starter-parent as parent, you can use @project.version@ to get version (and any other Maven properties) in your application.properties file. According to Spring docs:

You can automatically expand properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via @..@ placeholders

Maven pom.xml:

<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Foo</name>
<description>Bar</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

Spring application.properties:

[email protected]@

2. Then a class annotated with @ControllerAdvice can be used to inject version as model attribute.

@ControllerAdvice
public class ControllerAdvice {

    @Value("${foo.app.version}")
    private String applicationVersion;

    @ModelAttribute("applicationVersion")
    public String getApplicationVersion() {
        return applicationVersion;
    }

}

3. Finally this model attribute can be accessed by Thymeleaf as any other.

<th:block th:text="${applicationVersion}"></th:block>

Hope this helps!

like image 101
emrekgn Avatar answered Oct 03 '22 19:10

emrekgn