问题描述
项目中指定了多个profiles, 但是application.yaml读取报错,报错信息如下
Standard Commons Logging discovery in action with spring-jcl: please remove commons-logging.jar from classpath in order to avoid potential conflicts
12:41:52.325 [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 3, column 13:active: @env@^at org.yaml.snakeyaml.scanner.ScannerImpl.fetchMoreTokens(ScannerImpl.java:445)at org.yaml.snakeyaml.scanner.ScannerImpl.checkToken(ScannerImpl.java:238)at org.yaml.snakeyaml.parser.ParserImpl$ParseBlockMappingValue.produce(ParserImpl.java:669)at org.yaml.snakeyaml.parser.ParserImpl.peekEvent(ParserImpl.java:161)at org.yaml.snakeyaml.comments.CommentEventsCollector$1.peek(CommentEventsCollector.java:57)at org.yaml.snakeyaml.comments.CommentEventsCollector$1.peek(CommentEventsCollector.java:43)
profiles配置如下
<profiles><profile><id>dev</id><properties><env>dev</env></properties><activation><activeByDefault>true</activeByDefault></activation></profile><profile><id>test</id><properties><env>test</env></properties></profile><profile><id>prod</id><properties><env>prod</env></properties></profile>
</profiles>
application.yaml中的配置
spring:profiles:active: @env@
无论怎么切换profiles,或者刷新maven项目,一直报这个错误。
之前的项目也使用了这个写法,但是没有这个问题
解决方案
在Maven中添加如下配置
<build><resources><resource><directory>src/main/resources</directory><!--开启过滤,用指定的参数替换directory下的文件中的参数--><filtering>true</filtering></resource></resources>
</build>
如果继承了spring-boot-parent就不会有这个问题
原因在于,spring-boot-parent中也有这个maven配置,默认将resources下的文件过滤掉,防止maven对文件占位符进行修改