在Eclipse中运行时从pom获取maven项目版本和工件ID [英] Getting maven project version and artifact ID from pom while running in Eclipse

查看:183
本文介绍了在Eclipse中运行时从pom获取maven项目版本和工件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我遇到这个问题时,我正在查找如何从maven pom或manifest获取应用程序名称(工件ID)和版本在运行时获取Maven工件版本

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}

现在打开 maven资源过滤

  <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆