从代码的Maven pom.xml的版本检索

问题描述 投票:210回答:8

什么是从行家的pom.xml检索版本号的代码,即,编程的最简单的方法?

java maven-2 version
8个回答
237
投票

您使用的是Java假设,你可以

  1. 创建(最常见)你.properties目录src/main/resources文件(但在步骤4,你可以告诉它到别处)。
  2. 使用项目版本的标准的Maven属性设置一些属性的值在.properties文件:foo.bar=${project.version}
  3. 在Java代码中,从属性文件从classpath中的资源加载值(谷歌如何做到这一点很多例子,但here's an example for starters)。
  4. 在Maven中,使资源筛选 - 这将导致Maven来该副本过程中的文件复制到你的输出类和翻译资源,解释财产。你可以找到一些信息here但大多只是做在你的POM:
    <build>
      <resources>
        <resource>
          <directory>src/main/resources</directory>
          <filtering>true</filtering>
        </resource>
      </resources>   
    </build>

您还可以得到其他标准的属性,如project.nameproject.description,甚至你把你的POM <properties>任意属性等资源筛选,使用Maven型材相结合,可以让你在编译的时候变的生成行为。当你指定在与-PmyProfile运行的配置文件,可以使性质则可以在构建露面。


78
投票

接受的答案可能得到一个版本号为静态的应用程序的最佳和最稳定的方法,但实际上并没有回答原来的问题:如何从pom.xml中获取神器的版本号?因此,我想提供展示如何在运行时动态地做一个选择:

你可以使用Maven本身。更确切的说,你可以使用一个Maven库。

<dependency>
  <groupId>org.apache.maven</groupId>
  <artifactId>maven-model</artifactId>
  <version>3.3.9</version>
</dependency>

然后做在Java中是这样的:

package de.scrum_master.app;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.FileReader;
import java.io.IOException;

public class Application {
    public static void main(String[] args) throws IOException, XmlPullParserException {
        MavenXpp3Reader reader = new MavenXpp3Reader();
        Model model = reader.read(new FileReader("pom.xml"));
        System.out.println(model.getId());
        System.out.println(model.getGroupId());
        System.out.println(model.getArtifactId());
        System.out.println(model.getVersion());
    }
}

控制台日志如下:

de.scrum-master.stackoverflow:my-artifact:jar:1.0-SNAPSHOT
de.scrum-master.stackoverflow
my-artifact
1.0-SNAPSHOT

更新2017年10月31日:为了回答西蒙Sobisch的后续问题,我修改了这样的例子:

package de.scrum_master.app;

import org.apache.maven.model.Model;
import org.apache.maven.model.io.xpp3.MavenXpp3Reader;
import org.codehaus.plexus.util.xml.pull.XmlPullParserException;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Application {
  public static void main(String[] args) throws IOException, XmlPullParserException {
    MavenXpp3Reader reader = new MavenXpp3Reader();
    Model model;
    if ((new File("pom.xml")).exists())
      model = reader.read(new FileReader("pom.xml"));
    else
      model = reader.read(
        new InputStreamReader(
          Application.class.getResourceAsStream(
            "/META-INF/maven/de.scrum-master.stackoverflow/aspectj-introduce-method/pom.xml"
          )
        )
      );
    System.out.println(model.getId());
    System.out.println(model.getGroupId());
    System.out.println(model.getArtifactId());
    System.out.println(model.getVersion());
  }
}

71
投票

包装文物包含META-INF/maven/${groupId}/${artifactId}/pom.properties文件,该文件的内容是这样的:

#Generated by Maven
#Sun Feb 21 23:38:24 GMT 2010
version=2.5
groupId=commons-lang
artifactId=commons-lang

许多应用程序使用此文件在运行时读取应用程序/罐的版本,有需要的零设置。

与上述方法的唯一问题是,这文件是(目前)期间package相位产生并在测试期间将因此不存在等(有一个JIRA问题来改变这一点,请参阅MJAR-76)。如果这是你的一个问题,然后由Alex描述的方法是要走的路。


41
投票

也有在Easy way to display your apps version number using Maven中描述的方法:

加入这pom.xml的

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-jar-plugin</artifactId>
      <configuration>
        <archive>
          <manifest>
            <mainClass>test.App</mainClass>
            <addDefaultImplementationEntries>
              true
            </addDefaultImplementationEntries>
          </manifest>
        </archive>
      </configuration>
    </plugin>
  </plugins>
</build>

然后,使用这样的:

App.class.getPackage().getImplementationVersion()

我发现这种方法更简单。


15
投票

如果您使用MVN包装,如罐子或战争,使用方法:

getClass().getPackage().getImplementationVersion()

它在存档读取产生的META-INF / MANIFEST.MF的财产“的实现版本”(即设置为pom.xml中的版本)。


15
投票

为了补充什么@kieste已经发布,我认为这是有Maven构建有用信息在你的代码,如果你使用Spring启动的最佳方法:在http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info的文档是非常有用的。

你只需要激活执行机构,并添加您在application.propertiesapplication.yml需要的属性

Automatic property expansion using Maven

You can automatically expand info properties from the Maven project using resource filtering. If you use the spring-boot-starter-parent you can then refer to your Maven ‘project properties’ via @..@ placeholders, e.g.

project.artifactId=myproject
project.name=Demo
project.version=X.X.X.X
project.description=Demo project for info endpoint
[email protected]@
[email protected]@
[email protected]@
[email protected]@

6
投票

使用这个库为了便于简单的解决方案。添加到任何你的字符串需要再查询清单。

 System.out.println("JAR was created by " + Manifests.read("Created-By"));

http://manifests.jcabi.com/index.html


0
投票
    <build>
            <finalName>${project.artifactId}-${project.version}</finalName>
            <pluginManagement>
                <plugins>
                    <plugin>
                        <groupId>org.apache.maven.plugins</groupId>
                        <artifactId>maven-war-plugin</artifactId>
                        <version>3.2.2</version>
                        <configuration>
                            <failOnMissingWebXml>false</failOnMissingWebXml>
                            <archive>
                                <manifest>
                                    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
                                    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
                                </manifest>
                            </archive>
                        </configuration>
                    </plugin>
                 </plugins>
            </pluginManagement>
</build>

获取使用this.getClass().getPackage().getImplementationVersion()版本

PS不要忘了补充:

<manifest>
    <addDefaultImplementationEntries>true</addDefaultImplementationEntries>
    <addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>
</manifest>
© www.soinside.com 2019 - 2024. All rights reserved.