问题描述
当我遇到这个问题时,我正在查找如何从maven pom或manifest获取应用程序名称(工件ID)和版本。
I was looking up how to get the application name(artifact id) and version from maven pom or manifest when I came across this question Get Maven artifact version at runtime.
以上对我有用我打包项目但是当我尝试使用eclipse运行程序时,我似乎无法工作。我在构建时尝试使用.properties方法,因为我认为这不依赖于包,但我仍然没有得到结果。如果有人对这个问题有任何想法或解决方案,我们将不胜感激。
The above works for me when I package the project but I can't seem to get anything to work when I try to run the program using eclipse. I tried using the .properties method when building since I assumed that is not package dependent but I am still not getting a result. If anyone has an idea or solution to this problem it would be greatly appreciated.
我的最后一次尝试如下。这在打包(使用)时使用清单,并在eclipse中运行时尝试获取.properties文件。
My last attempt is below. This uses the manifest when packaged(which works) and trying to get the .properties file when running in eclipse.
String appVersion = getClass().getPackage().getImplementationVersion();
if(appVersion == null || "".equals(appVersion)) {
appVersion = Glob.getString(appVersion);
if(appVersion == null || "".equals(appVersion)) {
System.exit(0);
}
}
推荐答案
创建属性文件
src/main/resources/project.properties
以下内容
version=${project.version}
artifactId=${project.artifactId}
现在打开
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
以便将此文件处理成
target/classes/project.properties
包含一些相似的内容到这个
with some content similar to this
version=1.5
artifactId=my-artifact
现在你可以阅读这个属性文件来获得你想要的东西,这应该每次都有效。
Now you can read this property file to get what you want and this should work every time.
final Properties properties = new Properties();
properties.load(this.getClassLoader().getResourceAsStream("project.properties"));
System.out.println(properties.getProperty("version"));
System.out.println(properties.getProperty("artifactId"));
这篇关于在Eclipse中运行时从pom获取maven项目版本和工件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!