在代码中从maven的pom.xml中检索版本号的最简单方法是什么,即以编程方式?


当前回答

为了补充@kieste所发布的内容,我认为如果使用Spring-boot,这是在代码中提供Maven构建信息的最佳方式:http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-application-info上的文档非常有用。

您只需要激活执行器,并在应用程序中添加所需的属性。属性或application.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
info.build.artifact=@project.artifactId@
info.build.name=@project.name@
info.build.description=@project.description@
info.build.version=@project.version@

其他回答

还有一种方法描述了使用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()

我发现这个方法更简单。

打包的工件包含META-INF/maven/${groupId}/${artifactId}/pom。属性文件,内容如下:

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

许多应用程序使用这个文件在运行时读取应用程序/jar版本,不需要任何设置。

上述方法的唯一问题是,该文件(目前)是在包阶段生成的,因此在测试期间不会出现(有一个Jira问题可以改变这一点,请参阅MJAR-76)。如果这对您来说是一个问题,那么Alex描述的方法就是正确的方法。

第一步:如果你使用Spring Boot,你的pom.xml应该已经包含了Spring - Boot -maven-plugin。您只需要添加以下配置。

<plugin>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-maven-plugin</artifactId>
    <executions>
        <execution>
            <id>build-info</id>
            <goals>
                <goal>build-info</goal>
            </goals>
        </execution>
    </executions>
</plugin>

它指示插件执行build-info目标,默认情况下不会运行。这会生成关于应用程序的构建元数据,包括工件版本、构建时间等。

步骤2:使用buildProperties bean访问构建属性。在我们的例子中,我们创建了一个restResource来访问我们的webapp中的构建信息

@RestController
@RequestMapping("/api")
public class BuildInfoResource {
    @Autowired
    private BuildProperties buildProperties;

    
    @GetMapping("/build-info")
    public ResponseEntity<Map<String, Object>> getBuildInfo() {
        Map<String, String> buildInfo = new HashMap();
        buildInfo.put("appName", buildProperties.getName());
        buildInfo.put("appArtifactId", buildProperties.getArtifact());
        buildInfo.put("appVersion", buildProperties.getVersion());
        buildInfo.put("appBuildDateTime", buildProperties.getTime());
        return ResponseEntity.ok().body(buldInfo);
    }
}

我希望这能有所帮助

如果使用Spring和Maven,这非常简单,不需要配置。 根据“使用Maven自动扩展属性”官方文档,您可以使用资源过滤自动扩展Maven项目中的属性。如果你使用spring-boot-starter-parent,你就可以用@.引用Maven的“项目属性”。@占位符,示例如下:

project.version=@project.version@
project.artifactId=@project.artifactId@

你可以在任何类中使用@Value注释来检索它:

@Value("${project.artifactId}@${project.version}")
private String RELEASE;

我希望这能有所帮助!

关于ketankk的回答:

不幸的是,添加这个打乱了我的应用程序处理资源的方式:

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

但是在maven-assemble-plugin的< manifest >标签中使用这个可以达到目的:

<addDefaultImplementationEntries>true</addDefaultImplementationEntries>
<addDefaultSpecificationEntries>true</addDefaultSpecificationEntries>

所以我能得到版本使用

String version = getClass().getPackage().getImplementationVersion();