【问题标题】:display application version in title using thymeleaf and springboot使用 thymeleaf 和 spring boot 在标题中显示应用程序版本
【发布时间】:2017-01-09 13:58:35
【问题描述】:

我想在我的 htm 页面中显示我的 webapp 的版本,使用类似这样的东西(里面的百里香):

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

数据在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>

我可以在 MANIFEST.MF 中看到它(在 META-INF 下生成的 jar 中):

实施版本:2.0.3-SNAPSHOT

我尝试在控制器中获取应用程序版本并将其设置在 ModuleAttribute 中:

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

但是getClass().getPackage().getImplementationVersion() 的值是null。确实,包 implementationVersion 默认不是应用的 implementation Version。

【问题讨论】:

标签: maven spring-boot


【解决方案1】:

我知道我迟到了,但Patrick's answer 和 Spring 文档在这件事上大有帮助。

1.如果你的pom.xml使用spring-boot-starter-parent作为父级,你可以使用@project.version@获取application.properties 文件中的版本(和任何其他 Maven 属性)。根据 Spring 文档:

您可以使用 Maven 项目自动扩展属性 资源过滤。如果你使用 spring-boot-starter-parent 你 能够 然后通过 @..@ 占位符引用您的 Maven “项目属性”

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>

希望这会有所帮助!

【讨论】:

  • 这会起作用,但不是必须的。只需在您的 pom 中启用资源过滤并将 @project.version@ ​​放入 Thymeleaf 模板中。
  • 简单一点,省略 2. 并在 3. 中使用 ${@environment.getProperty("foo.app.version")} 代替。如果您不使用 spring-boot-starter-parent,请看这里:maven.apache.org/plugins/maven-resources-plugin/examples/…
【解决方案2】:

这是我发现的最简单的方法: 在我的控制器中:

@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;
}

在我的 htm 页面中:

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

【讨论】:

    【解决方案3】:

    您需要配置资源插件以激活对需要使用来自您的 POM 文件的属性进行丰富的文件的过滤。

    在生成的战争中,版本(实际上是${project.version})将被硬编码为您的 POM 版本。

    【讨论】:

    • 我已经添加了 src/main/resourcestrue ,但它没有没有更好的工作,是你建议的还是它更具体到 htm 文件?
    • 我猜你的 HTML 文件不在src/main/resources 中。如果你想对其应用过滤器,你应该将src/main/webapp/hmtl(或类似的东西)添加到资源配置中
    • 我的 htm 在src/main/resources/templates。我已经更改了 pom 中的路径,但效果并不好。由于在 htm 文件中使用,没有错误,只是没有值:而不是 &lt;h4 &gt;2.0.3-SNAPSHOT&lt;/h4&gt; 或列表 &lt;h4&gt;&lt;/h4&gt;,我在生成的源代码中什么都没有。
    • @project.version@ 是你想要的。
    猜你喜欢
    • 2016-04-03
    • 2020-11-28
    • 2020-01-25
    • 2019-03-12
    • 1970-01-01
    • 2021-06-03
    • 1970-01-01
    • 1970-01-01
    • 2018-07-02
    相关资源
    最近更新 更多