在代码中从maven的pom.xml中检索版本号的最简单方法是什么,即以编程方式?
当前回答
打包的工件包含META-INF/maven/${groupId}/${artifactId}/pom。属性文件,内容如下:
#Generated by Maven
#Sun Feb 21 23:38:24 GMT 2010
version=2.5
groupId=commons-lang
artifactId=commons-lang
许多应用程序使用这个文件在运行时读取应用程序/jar版本,不需要任何设置。
上述方法的唯一问题是,该文件(目前)是在包阶段生成的,因此在测试期间不会出现(有一个Jira问题可以改变这一点,请参阅MJAR-76)。如果这对您来说是一个问题,那么Alex描述的方法就是正确的方法。
其他回答
如果使用Spring和Maven,这非常简单,不需要配置。 根据“使用Maven自动扩展属性”官方文档,您可以使用资源过滤自动扩展Maven项目中的属性。如果你使用spring-boot-starter-parent,你就可以用@.引用Maven的“项目属性”。@占位符,示例如下:
project.version=@project.version@
project.artifactId=@project.artifactId@
你可以在任何类中使用@Value注释来检索它:
@Value("${project.artifactId}@${project.version}")
private String RELEASE;
我希望这能有所帮助!
引言:因为我记得在几年前回答了这个问题后,我经常提到这个问题,展示了一个动态访问Maven POM信息的动态版本(例如,也在测试期间),今天我发现了一个类似的问题,涉及从另一个模块B访问模块a的Maven信息。
I thought about it for a moment and spontaneously had the idea to use a special annotation, applying it to a package declaration in package-info.java. I also created a multi-module example project on GitHub. I do not want to repeat the whole answer, so please see solution B in this answer. The Maven setup involves Templating Maven Plugin, but could also be solved in a more verbose way using a combination of resource filtering and adding generated sources directory to the build via Build Helper Maven. I wanted to avoid that, so I simply used Templating Maven.
在使用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;
使用这个库可以获得简单的解决方案。向清单中添加您需要的任何内容,然后按字符串查询。
System.out.println("JAR was created by " + Manifests.read("Created-By"));
http://manifests.jcabi.com/index.html
第一步:如果你使用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中重新连接分离对象的正确方法是什么?
- 应该……接住环内还是环外?