yml配置文件引用pom.xml中的变量

常用的profile中的properties子标签动态环境引用,项目组件artifactId引用

pom.xml

<profiles>
    <profile>
        <id>local</id>
        <properties>
            <environment>local</environment>
        </properties>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>dev</id>
        <properties>
            <environment>dev</environment>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <environment>test</environment>
        </properties>
    </profile>
    <profile>
        <id>uat</id>
        <properties>
            <environment>uat</environment>
        </properties>
    </profile>
    <profile>
        <id>prod</id>
        <properties>
            <environment>prod</environment>
        </properties>
    </profile>
</profiles>

bootstrap.yml

spring:
  profiles:
    active: @environment@

如遇到ScannerException异常

Caused by: 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 26, column 13: active: @environment@

错误原因:

报了ScannerException异常,yml格式错误的时候抛出的异常,格式哪里有问题呢?active: @environment@解析错误,environment变量在pom中,说明没有加载到pom配置

解决方法:

在右侧的maven栏中重新reload的一下就行了

如果还是不行
添加resource到classpath
spring-boot:run可以将src/main/resources直接添加到classpath

<build>
  <resources>
      <resource>
          <filtering>true</filtering>
          <directory>src/main/resources</directory>
          <includes>
              <include>bootstrap-${environment}.yml</include>
              <include>bootstrap.yml</include>
              <include>**/*</include>
          </includes>
      </resource>
  </resources>
</build>

或者自定义该插件的配置

<plugins>
  <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>2.7</version>
      <configuration>
          <delimiters>
              <delimiter>@</delimiter>
          </delimiters>
          <useDefaultDelimiters>false</useDefaultDelimiters>
      </configuration>
  </plugin>
<plugins/>
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大佬腿好粗

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值