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对文件占位符进行修改

相关推荐
泡海椒1 天前
Spring 项目中如何组织 JQuick-Excel 的 XML 与 Bean
xml·spring·excel
驭渊的小故事1 天前
MyBatis XML 开发实战:从配置到 CRUD 全解析
xml·java·mybatis
芦芭荞2 天前
QT使用xml模版生成word文档
xml·qt·word
楚识科技5 天前
XML配置OCR接入实战:自定义OCR模板从字段定义到API调用全流程
xml·java·ocr
hasty10 天前
policy.xml 在,不代表策略生效:ImageMagick 补丁给安全配置验收的提醒
xml·mysql·安全
泡海椒13 天前
JQuick-Excel JQuickRow 数据准备实战:JObjectConverter、JQuickRow.toRows 与 XML 导出链路
xml·windows·excel
泡海椒15 天前
JQuick-Excel 字段映射实战:用 MAPPING 固化 Excel 表头与业务字段契约
xml·java·开发语言·excel
执明wa1 个月前
Android RecyclerView 多类型, 多种 Item
android·xml·开发语言·设计模式·android studio
wl85111 个月前
SAP CPI 教程004 如何通过groovy修改XML节点属性值
xml
码匠许师傅1 个月前
【C++三方组件】TinyXML2:两个文件的极简 XML 解析器
xml·c++