SpringBoot 打包文件名增加编译时间

 更新时间:2023年08月03日 09:23:36   投稿:ychy  
这篇文章主要为大家介绍了SpringBoot 打包编译时间实现过程详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

场景:

Spring Boot 打 jar 包时,文件命名增加编译时间;以及在 application.yml 配置文件中拿到编译时间;

Apache Maven 3.8.2

Spring Boot 2.4.1

一、maven.build.timestamp

1、pom.xml

<properties>
    <buildTime>${maven.build.timestamp}</buildTime>
    <maven.build.timestamp.format>yyyy-MM-dd HH:mm:ss</maven.build.timestamp.format>
</properties>

2、application.yml

app:
  version: @project.version@
  buildTime: @buildTime@

要在 application.yml 配置中获取到 pom.xml 中的属性,需要添加如下配置

<build>
    <!-- jar 包命名格式 -->
    <finalName>${project.artifactId}-${version}-${buildTime}</finalName>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

不过这种方式获取到的时间有时区问题,拿到的是 UTC 时间,无法指定时区,因此比中国晚 8 小时;为解决时区问题,可使用插件 buildnumber-maven-pluginbuild-helper-maven-plugin 获取编译时间

二、buildnumber-maven-plugin

1、pom.xml

<build>
    <finalName>${project.artifactId}-${version}-${timestamp}</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>buildnumber-maven-plugin</artifactId>
            <version>3.0.0</version>
            <configuration>
                <timestampFormat>yyyy-MM-dd HH:mm:ss</timestampFormat>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>create-timestamp</goal>
                    </goals>
                </execution>
            </executions>
            <inherited>false</inherited>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

2、application.yml

app:
  version: @project.version@
  buildTime: @timestamp@

三、build-helper-maven-plugin

1、pom.xml

<build>
    <finalName>${project.artifactId}-${version}-${buildTime}</finalName>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <version>3.2.0</version>
            <executions>
                <execution>
                    <id>timestamp-property</id>
                    <goals>
                        <goal>timestamp-property</goal>
                    </goals>
                    <configuration>
                        <name>buildTime</name>
                        <pattern>yyyyMMddHHmmss</pattern>
                        <locale>zh_CN</locale>
                        <timeZone>GMT+8</timeZone>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

2、application.yml

app:
  version: @project.version@
  buildTime: @buildTime@

⚠️注意:finalName 标签配置的 jar 包名称格式中,如果存在冒号:时,虽然可以在 IDEA 中点击 Run 按钮启动,但是部署时使用 java -jar ***.jar 无法启动并报错:

找不到或无法加载主类 org.springframework.boot.loader.JarLauncher

上面三种方式通过 maven 打包之后在运行,都可以在 yml 配置中获取到编译的时间值;但是如果没打包,直接点击 Run 按钮启动 Spring Boot 程序;maven.build.timestamp 可以正常获取到时间,buildnumber-maven-plugin 的 timestamp 为空;而 build-helper-maven-plugin 直接报错:

17:55:22.077 [main] ERROR org.springframework.boot.SpringApplication - Application run failed
org.yaml.snakeyaml.scanner.ScannerException: while scanning for the next token
found character '@' that cannot start any token. (Do not use @ for indentation)
 in 'reader', line 23, column 14:
      buildTime: @buildTime@
                 ^

以上就是SpringBoot 打包编译时间实现过程详解的详细内容,更多关于SpringBoot 打包编译时间的资料请关注脚本之家其它相关文章!

相关文章

最新评论