我将我的版本信息保存在我的POM中:
<version>2.0.0</version>
我希望这个号码暴露在:
是否有一种简单(自动)方式可以做到这一点,或者可以以编程方式完成?
答案 0 :(得分:2)
spring-boot-maven-plugin
允许生成您可能希望执行器提供的POM坐标和其他属性。
https://docs.spring.io/spring-boot/docs/current/maven-plugin/examples/build-info.html
Maven目标信息,
https://docs.spring.io/spring-boot/docs/current/maven-plugin/build-info-mojo.html
spring-boot:build-info
答案 1 :(得分:1)
Maven 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>wendelsilverio</groupId>
<artifactId>hello-maven</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Hello</name>
</project>
您可以以编程方式在内部创建jar:
public String getVersion() {
String version = null;
// try to load from maven properties first
try {
Properties p = new Properties();
InputStream is = PomVersionMain.class
.getResourceAsStream("/META-INF/maven/wendelsilverio/hello-maven/pom.properties");
if (is != null) {
p.load(is);
version = p.getProperty("version", "");
}
} catch (Exception e) {
// ignore
}
// fallback to using Java API
if (version == null) {
Package aPackage = PomVersionMain.class.getPackage();
if (aPackage != null) {
version = aPackage.getImplementationVersion();
if (version == null) {
version = aPackage.getSpecificationVersion();
}
}
}
if (version == null) {
// we could not compute the version so use a blank
version = "Version could not compute";
}
return version;
}