在代码中从maven的pom.xml中检索版本号的最简单方法是什么,即以编程方式?
当前回答
第一步:如果你使用Spring Boot,你的pom.xml应该已经包含了Spring - Boot -maven-plugin。您只需要添加以下配置。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
它指示插件执行build-info目标,默认情况下不会运行。这会生成关于应用程序的构建元数据,包括工件版本、构建时间等。
步骤2:使用buildProperties bean访问构建属性。在我们的例子中,我们创建了一个restResource来访问我们的webapp中的构建信息
@RestController
@RequestMapping("/api")
public class BuildInfoResource {
@Autowired
private BuildProperties buildProperties;
@GetMapping("/build-info")
public ResponseEntity<Map<String, Object>> getBuildInfo() {
Map<String, String> buildInfo = new HashMap();
buildInfo.put("appName", buildProperties.getName());
buildInfo.put("appArtifactId", buildProperties.getArtifact());
buildInfo.put("appVersion", buildProperties.getVersion());
buildInfo.put("appBuildDateTime", buildProperties.getTime());
return ResponseEntity.ok().body(buldInfo);
}
}
我希望这能有所帮助
其他回答
关于ketankk的回答:
不幸的是,添加这个打乱了我的应用程序处理资源的方式:
<build>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
但是在maven-assemble-plugin的< manifest >标签中使用这个可以达到目的:
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
所以我能得到版本使用
String version = getClass().getPackage().getImplementationVersion();
在使用spring引导时,这个链接可能有用:https://docs.spring.io/spring-boot/docs/2.3.x/reference/html/howto.html#howto-properties-and-configuration
使用spring-boot-starter-parent,你只需要在你的应用配置文件中添加以下内容:
# get values from pom.xml
pom.version=@project.version@
之后的值是这样的:
@Value("${pom.version}")
private String pomVersion;
假设你正在使用Java,你可以:
Create a .properties file in (most commonly) your src/main/resources directory (but in step 4 you could tell it to look elsewhere). Set the value of some property in your .properties file using the standard Maven property for project version: foo.bar=${project.version} In your Java code, load the value from the properties file as a resource from the classpath (google for copious examples of how to do this, but here's an example for starters). In Maven, enable resource filtering. This will cause Maven to copy that file into your output classes and translate the resource during that copy, interpreting the property. You can find some info here but you mostly just do this in your pom: <build> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> </resource> </resources> </build>
你还可以获取其他标准属性,如project.name, project.description,甚至是你放在pom <properties>中的任意属性,等等。资源过滤与Maven概要文件相结合,可以在构建时为您提供可变的构建行为。当您在运行时使用-PmyProfile指定配置文件时,可以启用属性,然后可以在构建中显示这些属性。
<build>
<finalName>${project.artifactId}-${project.version}</finalName>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>3.2.2</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
<archive>
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
使用this.getClass().getPackage().getImplementationVersion()获取版本
PS别忘了补充:
<manifest>
<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
第一步:如果你使用Spring Boot,你的pom.xml应该已经包含了Spring - Boot -maven-plugin。您只需要添加以下配置。
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>build-info</id>
<goals>
<goal>build-info</goal>
</goals>
</execution>
</executions>
</plugin>
它指示插件执行build-info目标,默认情况下不会运行。这会生成关于应用程序的构建元数据,包括工件版本、构建时间等。
步骤2:使用buildProperties bean访问构建属性。在我们的例子中,我们创建了一个restResource来访问我们的webapp中的构建信息
@RestController
@RequestMapping("/api")
public class BuildInfoResource {
@Autowired
private BuildProperties buildProperties;
@GetMapping("/build-info")
public ResponseEntity<Map<String, Object>> getBuildInfo() {
Map<String, String> buildInfo = new HashMap();
buildInfo.put("appName", buildProperties.getName());
buildInfo.put("appArtifactId", buildProperties.getArtifact());
buildInfo.put("appVersion", buildProperties.getVersion());
buildInfo.put("appBuildDateTime", buildProperties.getTime());
return ResponseEntity.ok().body(buldInfo);
}
}
我希望这能有所帮助
推荐文章
- Intellij IDEA Java类在保存时不能自动编译
- 何时使用Mockito.verify()?
- 在maven中安装mvn到底做什么
- 不可变与不可修改的集合
- 如何在JSON中使用杰克逊更改字段名
- GSON -日期格式
- 如何从线程捕获异常
- 无法解析主机"<URL here>"没有与主机名关联的地址
- 如何在Java中打印二叉树图?
- String.format()在Java中格式化双重格式
- com.jcraft.jsch.JSchException: UnknownHostKey
- Java中的操作符重载
- 如何加速gwt编译器?
- 在Hibernate中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?