简体   繁体   English

将Maven POM版本号加载到Java Project中

[英]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.

相关问题 如何像Pom一样从Java方法获取Maven项目版本 - How to Get Maven Project Version From Java Method as Like at Pom 如何在普通pom中保留版本号并在其他pom文件中使用(spring maven项目) - How to keep version number in common pom and use it in other pom files (spring maven project) 如何在POM中获取maven版本号 - How to get maven version number inside POM Maven的pom文件Java版本规范 - pom file java version spec for Maven 如何将语义版本控制应用于 java maven 项目以自动增加 pom.xml 中的版本 - how to apply semantic versioning to the java maven project to automatically increment the version inside pom.xml 从 pom.xml 从 Java Maven 项目在 Z3B06ADACCEC858ZEDEFBBC66 管道中获取版本 - Get the version from pom.xml from a Java Maven project in Bitbucket pipeline 通过编辑pom在Maven项目中编码Java文件 - Java file encoding in a Maven project by editing pom 对 TestNG version# 和 Selenium-JAVA version# 感到困惑 - 如何获取 Maven POM.xml 文件的版本号 - Confused about TestNG version# and Selenium-JAVA version# - How to get version number for Maven POM.xml file Maven在POM中增加项目版本,但过滤后的资源变为旧版本 - Maven increments project version in POM, but filtered resource gets old version 项目属性与pom中的Java版本 - Java version in project properties vs pom
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM