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

问题描述

项目中指定了多个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对文件占位符进行修改

相关推荐

  1. Go 语言 Viper 库来读取 YAML 配置文件

    2024-03-18 06:16:02       11 阅读
  2. rust - 使用serde_yaml读取配置文件

    2024-03-18 06:16:02       18 阅读
  3. Flask存储在内存密钥读取

    2024-03-18 06:16:02       38 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-18 06:16:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-18 06:16:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-18 06:16:02       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-18 06:16:02       20 阅读

热门阅读

  1. Python基础学习(12)环境搭建/ATM1

    2024-03-18 06:16:02       21 阅读
  2. 控制工程学 en 2

    2024-03-18 06:16:02       19 阅读
  3. ssh命令——安全远程连接服务

    2024-03-18 06:16:02       20 阅读
  4. 《C缺陷和陷阱》-笔记(5)

    2024-03-18 06:16:02       18 阅读
  5. 四级缓存实现

    2024-03-18 06:16:02       21 阅读