怎么获取Maven项目的版本号

发布时间:2021-05-24 09:18:19 作者:小新
来源:亿速云 阅读:275

这篇文章将为大家详细讲解有关怎么获取Maven项目的版本号,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

目前大多数Spring Boot项目都会打成Jar包,所以什么War包、Ear包的就先不摸索了。

Jar包的秘密

我们先解压一个Spring Boot应用Jar包看看里面能不能找到一些蛛丝马迹。在META-INF文件夹中找到了两个相关的东西,一个是MANIFEST.MF:

Manifest-Version: 1.0
Spring-Boot-Classpath-Index: BOOT-INF/classpath.idx
Implementation-Title: spring-boot-version
Implementation-Version: 1.0.23
Spring-Boot-Layers-Index: BOOT-INF/layers.idx
Start-Class: cn.felord.SpringBootVersionApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Build-Jdk-Spec: 1.8
Spring-Boot-Version: 2.4.5
Created-By: Maven Jar Plugin 3.2.0
Main-Class: org.springframework.boot.loader.JarLauncher

里面包含了我定义的版本号1.0.23,Implementation-Version这个值好像通过代码能够获得:

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

但是用IDE启动发现version=null,不过用java -jar运行时version = 1.0.23。可能与IDE运行并不是通过jar的方式有关。

另一个是pom.properties,Maven项目编译会生成一个Properties配置文件:

artifactId=spring-boot-version
groupId=cn.felord
version=1.0.23

这岂不是读取Properties文件就可以了?

  String path = "META-INF/maven/cn.felord/spring-boot-version/pom.properties";
  ClassPathResource resource = new ClassPathResource(path);

  InputStream inputStream = resource.getInputStream();
  try (InputStreamReader reader = new InputStreamReader(inputStream)) {
      try (BufferedReader bufferedReader = new BufferedReader(reader)) {
          bufferedReader.lines()
                                .forEach(System.out::println);
         }
     } catch (Exception ignored) {

  }

这岂不是读取Properties文件就可以了?

  String path = "META-INF/maven/cn.felord/spring-boot-version/pom.properties";
  ClassPathResource resource = new ClassPathResource(path);

  InputStream inputStream = resource.getInputStream();
  try (InputStreamReader reader = new InputStreamReader(inputStream)) {
      try (BufferedReader bufferedReader = new BufferedReader(reader)) {
          bufferedReader.lines()
                                .forEach(System.out::println);
         }
     } catch (Exception ignored) {

  }

依然只能从jar读取,而且比较麻烦。这两种方式都要依赖jar包,有木有不单纯依赖jar包的呢?

Maven资源插件过滤

Maven在构建项目时可以通过资源插件将构建属性即pom.xml中的属性注入到指定的资源文件中,具体操作为:

<build>
  ...
  <resources>
    <!-- include main.properties -->
    <resource>
      <directory>src/main/resources</directory>
      <filtering>true</filtering>
      <includes>
        <include>main.properties</include>
      </includes>
    </resource>
  </resources>
  ...
</build>

恰好spring-boot-starter-parent中已经设置了这种方式。

  <resource>
        <directory>${basedir}/src/main/resources</directory>
        <filtering>true</filtering>
        <includes>
          <include>**/application*.yml</include>
          <include>**/application*.yaml</include>
          <include>**/application*.properties</include>
        </includes>
      </resource>

如果你是application.properties,你可以通过下面的方式来接收版本号:

application.version = ${project.version}

如果是application.yaml,你可以通过下面的方式来接收版本号:

application:
  version: '@project.version@'

然后如何取值就不用多说了吧。这种方式不依赖jar包,使用起来也很简单。

Spring Boot提供

Spring Boot其实已经内置了获取项目构建信息的自动配置ProjectInfoAutoConfiguration,它包含一个条件BeanBuildProperties:

@ConditionalOnResource(
        resources = {"${spring.info.build.location:classpath:META-INF/build-info.properties}"}
    )
    @ConditionalOnMissingBean
    @Bean
    public BuildProperties buildProperties() throws Exception {
        return new BuildProperties(this.loadFrom(this.properties
                                                 .getBuild()
                                                 .getLocation(), "build",                                                     this.properties
                                                 .getBuild().getEncoding()));
    }

这个BuildProperties提供了不少构建信息:

public class BuildProperties extends InfoProperties {
    public BuildProperties(Properties entries) {
        super(processEntries(entries));
    }

    public String getGroup() {
        return this.get("group");
    }

    public String getArtifact() {
        return this.get("artifact");
    }

    public String getName() {
        return this.get("name");
    }

    public String getVersion() {
        return this.get("version");
    }

    public Instant getTime() {
        return this.getInstant("time");
    }
   }

其中的条件build-info.properties可以通过Spring Boot插件spring-boot-maven-plugin执行下面的命令生成:

mvn spring-boot:build-info

我们只需要配置插件为:

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

就能使得BuildProperties生效,我们可以轻松取得相关的信息:

{
  "version" : "1.0.23",
  "group" : "cn.felord",
  "artifact" : "spring-boot-version",
  "name" : "spring-boot-version",
  "time" : {
    "epochSecond" : 1620664643,
    "nano" : 591000000
  }
}

关于“怎么获取Maven项目的版本号”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

推荐阅读:
  1. golang 获取exe、dll、apk 版本号
  2. Android 获取应用程序版本号

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

maven

上一篇:Android给图片添加水印的方法

下一篇:如何解决Python绘制子图及子图刻度的变换等的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》