我想在我的htm页面中显示我的webapp版本,使用类似的东西(百里香内部):
<h4 th:text="${version}">Version</h4>
Run Code Online (Sandbox Code Playgroud)
数据在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>
Run Code Online (Sandbox Code Playgroud)
我可以在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();
}
Run Code Online (Sandbox Code Playgroud)
但getClass().getPackage().getImplementationVersion()价值是null.实际上,package implementationVersion默认情况下不是应用程序的实现版本.
我知道我迟到了,但Patrick的回答和Spring文档在这件事上有很大的帮助.
1.如果您的pom.xml使用spring-boot-starter-parent作为父级,则可以使用它@project.version@来获取application.properties文件中的版本(以及任何其他Maven属性).根据Spring文档:
您可以使用资源过滤从Maven项目自动扩展属性.如果你使用spring-boot-starter-parent,你可以通过@ .. @ placeholders引用你的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>
Run Code Online (Sandbox Code Playgroud)
Spring application.properties:
foo.app.version=@project.version@
Run Code Online (Sandbox Code Playgroud)
2.然后用注释一类@ControllerAdvice可用于注入版本模型属性.
@ControllerAdvice
public class ControllerAdvice {
@Value("${foo.app.version}")
private String applicationVersion;
@ModelAttribute("applicationVersion")
public String getApplicationVersion() {
return applicationVersion;
}
}
Run Code Online (Sandbox Code Playgroud)
3.最后,这个模型属性可以通过Thymeleaf任何其他访问.
<th:block th:text="${applicationVersion}"></th:block>
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助!
这是我发现的最简单的方法:在我的控制器中:
@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;
}
Run Code Online (Sandbox Code Playgroud)
在我的 htm 页面中:
<h4 th:text="${version}">Version</h4>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6656 次 |
| 最近记录: |