在代码中从maven的pom.xml中检索版本号的最简单方法是什么,即以编程方式?
当前回答
使用这个库可以获得简单的解决方案。向清单中添加您需要的任何内容,然后按字符串查询。
System.out.println("JAR was created by " + Manifests.read("Created-By"));
http://manifests.jcabi.com/index.html
其他回答
引言:因为我记得在几年前回答了这个问题后,我经常提到这个问题,展示了一个动态访问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.
假设你正在使用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指定配置文件时,可以启用属性,然后可以在构建中显示这些属性。
为了补充@kieste所发布的内容,我认为如果使用Spring-boot,这是在代码中提供Maven构建信息的最佳方式:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info上的文档非常有用。
您只需要激活执行器,并在应用程序中添加所需的属性。属性或application.yml
Automatic property expansion using Maven
You can automatically expand info properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via @..@ placeholders, e.g.
project.artifactId=myproject
project.name=Demo
project.version=X.X.X.X
project.description=Demo project for info endpoint
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@
打包的工件包含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描述的方法就是正确的方法。
<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>
推荐文章
- Java泛型什么时候需要<?扩展T>而不是<T>,切换有什么缺点吗?
- 如果性能很重要,我应该使用Java的String.format()吗?
- getResourceAsStream返回null
- 如何使用Java中的Scanner类从控制台读取输入?
- 如何添加JTable在JPanel与空布局?
- Statement和PreparedStatement的区别
- 为什么不能在Java中扩展注释?
- 在Java中使用UUID的最重要位的碰撞可能性
- 转换列表的最佳方法:map还是foreach?
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?