欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot 打包文件名增加編譯時間

 更新時間:2023年08月03日 09:23:36   投稿:ychy  
這篇文章主要為大家介紹了SpringBoot 打包編譯時間實(shí)現(xiàn)過程詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

場景:

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>

不過這種方式獲取到的時間有時區(qū)問題,拿到的是 UTC 時間,無法指定時區(qū),因此比中國晚 8 小時;為解決時區(qū)問題,可使用插件 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 標(biāo)簽配置的 jar 包名稱格式中,如果存在冒號:時,雖然可以在 IDEA 中點(diǎn)擊 Run 按鈕啟動,但是部署時使用 java -jar ***.jar 無法啟動并報錯:

找不到或無法加載主類 org.springframework.boot.loader.JarLauncher

上面三種方式通過 maven 打包之后在運(yùn)行,都可以在 yml 配置中獲取到編譯的時間值;但是如果沒打包,直接點(diǎn)擊 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 打包編譯時間實(shí)現(xiàn)過程詳解的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot 打包編譯時間的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論