Explain in detail that spring boot obtains the version number and compile time stamp in Maven POM

Time:2020-9-19

This article first introduces how to obtain the compilation time and version number of a project in springboot, and provides an interface to the outside world. Then it introduces the solution to the time zone error of the new version of Maven. Finally, it introduces two methods of adding the timestamp to the package name.

The springboot project obtains the compilation time stamp and version number, and then provides the interface, which is roughly divided into the following steps:

  • Get the compile time stamp of Maven in POM file
  • Configure POM and render resource files with attributes in Maven application.yml
  • Provide interface

Get compile time in maven

Add two properties to the POM file properties

<properties>
 <!-- maven.build.timestamp Saved Maven compile timestamp -- >
 <timestamp>${maven.build.timestamp}</timestamp>
 <! -- specify time format -- > 
 <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>

Render with Maven properties application.yml

Configuration in POM file build


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

stay application.yml Medium configuration

Cannot use ${}


app:
 version: @[email protected]
 build:
 time: @[email protected]

Provide interface

Create AppController and provide interface get / APP

package cn.yshow.modules.sys.controller;

import cn.yshow.common.utils.restResult.RestResult;
import cn.yshow.common.utils.restResult.ResultGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author zhaod [email protected]
 * @version v1.0
 * @since 2018/12/16 16:19
 */
@Slf4j
@RestController
@RequestMapping("/app")
public class AppController {
 /**
  *Current version
  */
 @Value("${app.version}")
 private String version;
 /**
  *Packing time
  */
 @Value("${app.build.time}")
 private String buildTime;

 @GetMapping
 public RestResult uploadImg() {
  Map<String,String> ret = new HashMap<>();
  ret.put("version",version);
  ret.put("buildTime",buildTime);
  
  //Resultresult is the return object I encapsulate
  return ResultGenerator.genSuccessResult(ret);
 }
}

test

Yapi was used to test as follows

maven.build.timestamp Time zone error resolution

In Maven 3.2.2 +, maven.build.timestamp It has been redefined to show that the time in UTC is 8 hours slower than that in China. You can use the plug-in build helper Maven plugin to get the time in this time zone

Add a plug-in in to the plugins block. This configuration is different from the official website. An error will be reported according to the configuration mode of the official website

<project>
 <build>
  <! -- specifies the package name -- >
  <finalName>
   ${project.artifactId}-${project.version}-${build.time}
  </finalName>
  <plugins>
   <plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.8</version>
    <executions>
     <execution>
      <id>timestamp-property</id>
      <goals>
       <goal>timestamp-property</goal>
      </goals>
     </execution>
    </executions>
    <configuration>
     <name>build.time</name>
     <pattern>yyyy-MM-dd HH:mm</pattern>
     <timeZone>GMT+8</timeZone>
    </configuration>
   </plugin>
  </plugins>
 </build>
</project>

After the above processing, the${ build.time }The time already represents the GMT-8 time zone

application.yml The configuration is as follows


app:
 version: @[email protected]
 build:
 time: @[email protected]

The idea compilation project does not call the Maven lifecycle, resulting in the installed plugin not being executed. Although Maven’s own variables can be replaced, custom variables are not.

You can bind the build of idea with Maven goal, and execute Maven goal before build. On the right side of idea, Maven projects, lifecycle, compile check execute after build and execute after rebuild, as shown in the following figure

Add timestamp to package name

Do not repeat the two methods, otherwise

Method 1: add the timestamp to the version number


<project>
 <versioin>
  0.0.5.${build.time}
 </version>
</project>

Method 2: add the timestamp to the package name directly


<project>
 <build>
  <finalName>
   ${project.artifactId}-${project.version}-${build.time}
  </finalName>
 </build> 
 
</project>

reference material

Maven packing timestamp problem

The above is the whole content of this article, I hope to help you in your study, and I hope you can support developeppaer more.

Recommended Today

Application of tree

Application of tree Storage structure of tree Parental representation (sequential storage) Definition: in each node, the “pointer” (position subscript) pointing to the parents is saved. The root node is fixedly stored in 0, – 1 indicates that there are no parents. Add: add directly without following the logical order. Delete: ① set the pointer to […]