資源文件默認(rèn)使用${...}
為占位符迟螺,但如果pom繼承了 spring-boot-starter-parent
裂垦,那么默認(rèn)占位符就改為了 @...@
例如:
#application.yml
spring:
profiles:
active: @profiles.active@
# pom.xml
<project>
...
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
<relativePath/>
</parent>
<profiles>
<profile>
<id>dev</id>
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>prod</id>
<properties>
<profiles.active>prod</profiles.active>
</properties>
</profile>
</profiles>
<build>
<finalName>Example</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>false</filtering>
<excludes>
<exclude>application*.yml</exclude>
<exclude>config/**</exclude>
</excludes>
<includes>
<include>**/*</include>
</includes>
</resource>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
<includes>
<include>application.yml</include>
<include>application-${profiles.active}.yml</include>
<include>config/${profiles.active}/*</include>
</includes>
</resource>
</resources>
</build>
</project>
另外注意一點(diǎn)寺枉,Intellij IDEA運(yùn)行Springboot項(xiàng)目時(shí)括蝠,默認(rèn)會(huì)使用內(nèi)置的編譯器来候,不去執(zhí)行maven中的編譯配置缸托。這將導(dǎo)致maven build正常左敌,但run/debug報(bào)錯(cuò),報(bào)錯(cuò)信息如下:
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 11, column 15:
active: @profiles.active@
^
這個(gè)問題可以通過啟用IDEA的Delegate IDE build/run actions to Maven
來解決俐镐。這個(gè)選項(xiàng)為項(xiàng)目級設(shè)置矫限,啟用后每次編譯時(shí)將不再使用內(nèi)置編譯,而是調(diào)用maven佩抹。官方選項(xiàng)說明
image.png