从pom获取应用程序版本

时间:2014-01-10 12:59:30

标签: java spring maven versioning

我有一个rest端点用于返回有关应用程序的信息(到目前为止只有app版本) 但到目前为止,这些信息都是硬编码的,忘记更改它很容易。 我最好从pom或manifest文件中检索app版本。是否有任何项目带来了这样的功能?

4 个答案:

答案 0 :(得分:10)

您最好使用内置清单。

new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))

对于具体的impl-version:

new Manifest(Application.class.getResourceAsStream("/META-INF/manifest.mf"))
            .getMainAttributes()
            .get(Attributes.Name.IMPLEMENTATION_VERSION)

使用maven不要忘记使用以下方法创建清单:

<?xml version="1.0" encoding="UTF-8"?>
<project>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

答案 1 :(得分:7)

Spring Boot可以引用pom.xml中定义的项目版本,并使用Actuator通过REST公开它:

# application.properties
endpoints.info.enabled=true
info.app.version=@project.version@

然后访问/ info网址(例如http://localhost:8080/info)将返回:

{"app": {"version": "<major.minor.incremental>"}}

另请参阅:spring boot/spring web app embedded version number

答案 2 :(得分:4)

有一个名为Appinfo的惊人项目,请使用它并享受! (它有一个丑陋的页面,我知道 - 但它有效:)

  
    

AppInfo允许使用当前版本号,构建日期或内部版本号自动提供应用程序。

  

同样出色的Spring Boot Actuator提供了名为Info Endpoint的功能,可以将版本信息发布到Web或REST。

  
    

默认情况下,执行器将/ info端点添加到主服务器。它包含来自git.properties的提交和时间戳信息(如果该文件存在)以及它在前缀为“info”的环境中找到的任何属性。

  

答案 3 :(得分:2)

您可以使用resource filtering maven或类似maven-substitute-plugin

的内容
相关问题