Java 在 Eclipse 中运行时从 pom 获取 maven 项目版本和工件 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26551439/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 02:47:10  来源:igfitidea点击:

Getting maven project version and artifact ID from pom while running in Eclipse

javamaven-3pom.xml

提问by swhite

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.

当我遇到这个问题Get Maven artifact version at runtime 时,我正在查找如何从 maven pom 或清单中获取应用程序名称(工件 ID)和版本。

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.

我的最后一次尝试如下。这在打包时使用清单(有效)并在 eclipse 中运行时尝试获取 .properties 文件。

String appVersion = getClass().getPackage().getImplementationVersion();
    if(appVersion == null || "".equals(appVersion)) {
        appVersion = Glob.getString(appVersion);
        if(appVersion == null || "".equals(appVersion)) {
            System.exit(0);
        }
    }

采纳答案by coderplus

Create a property file

创建属性文件

src/main/resources/project.properties

with the below content

以下内容

version=${project.version}
artifactId=${project.artifactId}

Now turn on maven resource filtering

现在打开Maven 资源过滤

  <resource>
    <directory>src/main/resources</directory>
    <filtering>true</filtering>
  </resource>

so that this file is processed into

以便将此文件处理为

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"));

回答by J. Chomel

An easy solution with maven 4 is now to add a VersionUtilstatic method in your package:

使用 maven 4 的一个简单解决方案是VersionUtil在您的包中添加一个静态方法:

package my.domain.package;
public class VersionUtil {
  public static String getApplicationVersion(){
    String version = VersionUtil.class.getPackage().getImplementationVersion();
    return (version == null)? "unable to reach": version;
  }
}

The thing is you need this ′mave-war-plugin′ in the project's pom, saying you want to add addDefaultImplementationEntries:

问题是你在项目的 pom 中需要这个“mave-war-plugin”,说你想添加addDefaultImplementationEntries

<build>
    <finalName>${project.artifactId}</finalName>
    <plugins>
     ...
      <plugin>
            <artifactId>maven-war-plugin</artifactId>
            <version>3.2.2</version>
            <configuration>
                <failOnMissingWebXml>false</failOnMissingWebXml>
                <archive>
                    <manifest>
                        <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                    </manifest>
                </archive>
            </configuration>
        </plugin>
    ...

Then call the VersionUtil.getApplicationVersion()from some place in your code.

然后VersionUtil.getApplicationVersion()从代码中的某个地方调用。