Display version of application in title using thimeleaf and spring boot

I want to show on my htm page a 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 file:

<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 see it in MANIFEST.MF (which is in the generated bank in META-INF):

Implementation-Version: 2.0.3-SNAPSHOT

I tried to get the version of the application in the controller and set it in the ModuleAttribute attribute:

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

But the getClass().getPackage().getImplementationVersion()value null. Indeed, the packageVersion implementation is not the default version of the application implementation.

+4
source share
3 answers

Here is the easiest way I found: In my controller:

@ModelAttribute("version")
public String getVersion() throws IOException {
    logger.info("ModelAttribute to get application version");
    Manifest manif = new Manifest(
            Application.class.getResourceAsStream("/META-INF/MANIFEST.MF"));
    String version = (String) manif.getMainAttributes().get(
            Attributes.Name.IMPLEMENTATION_VERSION);
    return version;
}

On my htm page:

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

, , Spring docs .

1. pom.xml spring -boot-starter-parent , @project.version@, ( Maven) . . Spring docs:

Maven, . spring -boot-starter-parent,    Maven @.. @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:

foo.app.version=@project.version@

2. , @ControllerAdvice, .

@ControllerAdvice
public class ControllerAdvice {

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

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

}

3. , Thymeleaf .

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

, !

+3

You need to configure the resource plugin to activate filtering in a file, which should be enriched with properties coming from your POM file.

In the generated war, the version (in fact ${project.version}) will be hard-coded to your version of POM.

0
source

Source: https://habr.com/ru/post/1653338/


All Articles