springboot application無法使用$獲取pom變量的問題及解決
application無法使用$獲取pom變量問題
在maven的pom文件中進行了多環(huán)境變量配置,引用了maven-resources-plugin,在application.properties文件中通過以下配置指定不同環(huán)境下的配置文件,
spring.profiles.active = ${profiles.active}
但是${profiles.active}無法從pom文件中獲取變量值替換。
由于${}方式會被maven處理。如果你pom繼承了spring-boot-starter-parent,Spring Boot已經(jīng)將maven-resources-plugins默認的${}方式改為了@@方式,如@name@
如果還想繼續(xù)使用${}占位符方式
只需要在pom文件中加上下面配置即可:
<build> ? ? <pluginManagement> ? ? ? ? <plugins> ? ? ? ? ? ? <plugin> ? ? ? ? ? ? ? ? <artifactId>maven-resources-plugin</artifactId> ? ? ? ? ? ? ? ? <configuration> ? ? ? ? ? ? ? ? ? ? <encoding>utf-8</encoding> ? ? ? ? ? ? ? ? ? ? <useDefaultDelimiters>true</useDefaultDelimiters> ? ? ? ? ? ? ? ? </configuration> ? ? ? ? ? ? </plugin> ? ? ? ? </plugins> ? ? </pluginManagement> </build>
或者使用
<configuration> ? ? ? ? ? ? ? ? ? ? <delimiters> ? ? ? ? ? ? ? ? ? ? ? ? <delimiter>@</delimiter> ? ? ? ? ? ? ? ? ? ? </delimiters> ? ? ? ? ? ? ? ? ? ? <useDefaultDelimiters>false</useDefaultDelimiters> ? ? ? ? ? ? ? ? </configuration>
將<useDefaultDelimiters>false</useDefaultDelimiters>
改為<useDefaultDelimiters>true</useDefaultDelimiters>
application.yml無法使用@@讀取pom.xml中標簽值
在application.yml中使用了@@讀取標簽值,
報下面的錯誤
Caused by: 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 5, column 11:
name: @artifactId@
解決辦法
在模塊的pom.xml文件下引入一下配置
<build> ?? <!--如果不設置resource 會導致application.yml中的@@找不到pom文件中的配置--> ? ? ? ? <resources> ? ? ?? ? ? ? ? <resource> ? ? ? ? ? ? ? ? ? ? ? ? <directory>src/main/resources</directory> ? ? ? ? ? ? <filtering>true</filtering> ? ? ? ? ? ? ? ? </resource> ? ? ? ? </resources> </build>
然后重新啟動,即可成功。
application.yml無法使用@@讀取pom.xml中標簽值
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Springboot啟動后立即某個執(zhí)行方法的四種方式
spring項目如何在啟動項目是執(zhí)行一些操作,在spring中能通過那些操作實現(xiàn)這個功能呢,下面這篇文章主要給大家介紹了關于Springboot啟動后立即某個執(zhí)行方法的四種方式,需要的朋友可以參考下2022-06-06詳解springboot+atomikos+druid?數(shù)據(jù)庫連接失效分析
本文主要介紹了springboot+atomikos+druid?數(shù)據(jù)庫連接失效分析,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2022-02-02