[英]Load Maven POM version number into Java Project
I want to add a version numbering to my Java application (in this case a Vaadin Portlet) which features the Version number in the help view. 我想在我的Java应用程序(在本例中为Vaadin Portlet)中添加版本编号,其中包含帮助视图中的版本号。
The thing is that that version number is the one defined in the my Maven's POM file (for example 1.1.5-SNAPSHOT) and that is the one we are going to change (at release for example). 问题是该版本号是我的Maven的POM文件中定义的版本号(例如1.1.5-SNAPSHOT),这是我们要更改的版本号(例如发布时)。
Any idea on how to get it out of the POM and into (for example, a String) in Java? 关于如何从POM中获取它并使用Java(例如,String)的任何想法? (If this is at all possible)
(如果可能的话)
Thanks 谢谢
Use a filtered properties file containing the line 使用包含该行的已过滤属性文件
version=${project.version}
and load this properties file from the classpath in your Java program. 并从Java程序中的类路径加载此属性文件。
There are many solutions to this problem 这个问题有很多解决方案
But I would defintely prefer the first, as accessing MANIFEST.MF is quite easy in Java 但我绝对不喜欢第一个,因为在Java中访问MANIFEST.MF非常容易
The version in the POM is available as a property "project.version". POM中的版本可作为属性“project.version”使用。 Expressed as ${project.version}
表示为$ {project.version}
So you should be able to use it with filtering resources: 所以你应该能够将它与过滤资源一起使用:
http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html http://maven.apache.org/plugins/maven-resources-plugin/examples/filter.html
The nicest approach would probably be to filter (and replace) a properties file or a deployment descriptor. 最好的方法可能是过滤(和替换)属性文件或部署描述符。
Or you could read the maven pom.properties from classpath. 或者您可以从类路径中读取maven pom.properties。 The pom.properties are located at:
pom.properties位于:
META-INF/maven/groupid/artifactId/pom.properties META-INF /行家/ GROUPID / artifactId的/ pom.properties
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.