Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the ways to get the Maven project version number

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you what are the ways to get the version number of the Maven project. I hope you will get something after reading this article. Let's discuss it together.

Maven is a tool that is often used to create projects, mainly because it makes it easy to build, manage, and download jar packages.

At present, most Spring Boot projects will be packed into Jar packages, so what War packages and Ear packages will not be explored first.

The Secret of Jar package

Let's unpack a Spring Boot app Jar package and see if we can find some clues inside. Two related things were found in the META-INF folder, one is MANIFEST.MF:

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

It contains the version number 1.0.23 that I defined, which seems to be available through the code:

String version = this.getClass (). GetPackage (). GetImplementationVersion ()

But start discovery version=null with IDE, but run version= 1.0.23 with java-jar. It may have something to do with the way that IDE does not run through jar.

The other is that the pom.properties,Maven project compilation generates a Properties configuration file:

ArtifactId=spring-boot-versiongroupId=cn.felordversion=1.0.23

Isn't it just enough to read the Properties file?

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) {}

Isn't it just enough to read the Properties file?

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) {}

It can still only be read from jar, and it's troublesome. Both of these methods rely on the jar package, is there anyone who does not rely solely on the jar package?

Maven resource plug-in filtering

When building a project, Maven can inject the build properties, that is, the properties in pom.xml, into the specified resource file through the resource plug-in, as follows:

... Src/main/resources true main.properties...

It just so happens that this method is already set in spring-boot-starter-parent.

${basedir} / src/main/resources true * / application*.yml * * / application*.yaml * * / application*.properties

If you are application.properties, you can receive the version number in the following ways:

Application.version = ${project.version}

If it is application.yaml, you can receive the version number in the following ways:

Application: version:'@ project.version@'

Then there is no need to say much about how to take the value. This approach does not rely on the jar package and is easy to use.

Provided by Spring Boot

Spring Boot actually has built-in auto-configuration ProjectInfoAutoConfiguration to get project build information, which contains a conditional 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 ()) }

This BuildProperties provides a lot of build information:

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

The conditional build-info.properties can be generated by executing the following command through the Spring Boot plug-in spring-boot-maven-plugin:

Mvn spring-boot:build-info

We just need to configure the plug-in as follows:

Org.springframework.boot spring-boot-maven-plugin build-info

We can make BuildProperties effective, and we can easily get the relevant information:

{"version": "1.0.23", "group": "cn.felord", "artifact": "spring-boot-version", "name": "spring-boot-version", "time": {"epochSecond": 1620664643, "nano": 591000000}} after reading this article, I believe you have some understanding of "how to obtain the Maven project version number" If you want to know more related knowledge, welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report