pom.xml中的配置无法被yaml读取

问题描述

项目中指定了多个profiles, 但是application.yaml读取报错,报错信息如下

txt 复制代码
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配置如下

xml 复制代码
<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中的配置

yaml 复制代码
spring:
  profiles:
    active: @env@

无论怎么切换profiles,或者刷新maven项目,一直报这个错误。

之前的项目也使用了这个写法,但是没有这个问题

解决方案

在Maven中添加如下配置

xml 复制代码
<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <!--开启过滤,用指定的参数替换directory下的文件中的参数-->
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

如果继承了spring-boot-parent就不会有这个问题

原因在于,spring-boot-parent中也有这个maven配置,默认将resources下的文件过滤掉,防止maven对文件占位符进行修改

相关推荐
ddfa12348 小时前
XML 笔记
xml·服务器
一勺菠萝丶1 天前
Spring Boot + MyBatis/MyBatis Plus:XML中循环处理List参数的终极指南
xml·spring boot·mybatis
李少兄13 天前
解决OSS存储桶未创建导致的XML错误
xml·开发语言·python
阳光开朗_大男孩儿13 天前
XML读取和设置例子
xml·java·数据库
悟能不能悟15 天前
在 MyBatis 的xml中,什么时候大于号和小于号可以不用转义
xml·java·mybatis
慌糖15 天前
XML重复查询一条Sql语句??怎么解决
xml·数据库·sql
胖大和尚15 天前
在 XML 节点名称前添加前缀
xml
casual_clover16 天前
Android 中 解析 XML 字符串的几种方式
android·xml
SuperherRo17 天前
Web攻防-XSS跨站&文件类型&功能逻辑&SVG&PDF&SWF&HTML&XML&PMessage&LocalStorage
xml·pdf·html·svg·localstorage·swf·pmessage
'tubug'18 天前
XXE(XML外部实体注入)详解
xml·web安全