10

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.

jayjaypg22
  • 1,641
  • 5
  • 22
  • 41
  • Don't have an exact answer but here are a couple links that may help http://stackoverflow.com/questions/3697449/retrieve-version-from-maven-pom-xml-in-code and http://stackoverflow.com/questions/21045007/getting-application-version-from-pom – Rob Baily Sep 01 '16 at 19:36
  • @RobBaily I've read it before asking. The solution I tried is my interpretation of http://stackoverflow.com/questions/3697449/retrieve-version-from-maven-pom-xml-in-code answer – jayjaypg22 Sep 02 '16 at 09:16

3 Answers3

12

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:

foo.app.version=@project.version@

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!

emrekgn
  • 624
  • 9
  • 25
  • 1
    This will work but it is not necessary. Just enable resource filtering in your pom and put @project.version@ in the Thymeleaf template. – jzonthemtn Oct 19 '17 at 19:49
  • 2
    A bit simpler, omit 2. and in 3. use `${@environment.getProperty("foo.app.version")}` instead. And if your not using spring-boot-starter-parent, take a look here: https://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html – RiZKiT Feb 26 '18 at 15:15
2

Here is the simplest way I've 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;
}

In my htm page :

<h4 th:text="${version}">Version</h4>
jayjaypg22
  • 1,641
  • 5
  • 22
  • 41
0

You need to configure resource plugin to activate filtering on the file that need to be enriched with properties coming from your POM file.

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

YMomb
  • 2,366
  • 1
  • 27
  • 36
  • I've added src/main/resources true , but it doesn't work better, is it what you suggested or was it more specific to the htm file by instance? – jayjaypg22 Sep 02 '16 at 09:13
  • I guess that your HTML file is not inside `src/main/resources`. If you want to apply filter on it, you should add the `src/main/webapp/hmtl`(or something like that) to the resources configuration – YMomb Sep 02 '16 at 09:38
  • My htm is in `src/main/resources/templates`. I've changed the path in the pom, but it doesn't work better. And as the use is in the htm file, there is no error, just no value : instead of `

    2.0.3-SNAPSHOT

    ` or at list `

    `, I have nothing at all in the source generated.
    – jayjaypg22 Sep 02 '16 at 12:47
  • `@project.version@` is what you want. – jzonthemtn Oct 19 '17 at 19:50