spring boot yml文件中使用占位符的问题

搭建spring boot工程,需要yml文件与pom.xml配合设置多环境profile的问题,使用${}占位符不起效果

pom.xml添加如下代码

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <build.profile.id>dev</build.profile.id>
        </properties>
    </profile>
    <profile>
        <id>test</id>
        <properties>
            <build.profile.id>test</build.profile.id>
        </properties>
    </profile>
</profiles>

这时idea的maven projects中会出现如下图所示:

 

iF3Ebq.jpg

 

这时在application.yml中添加相关环境代码:

spring:
  profiles:
    active: ${build.profile.id}
server:
  port: 3001

#----------------------------

---
spring:
  profiles: dev
server:
  port: 8081
---
spring:
  profiles: test
server:
  port: 8082

启动后并没有切换服务端口,也就是无法从pom文件中读取相关变量进行替换。

查询资料发现:

Spring Boot已经将maven-resources-plugins默认的${}方式改为了@@方式,如@name@

修改后代码如下:

Spring:
    profiles:
        active: @build.profile.id@

如果想继续使用${}的方式,需要添加一个maven相关的插件:

<build>
    <!--<plugins>-->

    <!--</plugins>-->
    <pluginManagement>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <configuration>
                    <encoding>utf-8</encoding>
                    <useDefaultDelimiters>true</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
    </pluginManagement>
</build>

这样就可以愉快的使用啦~
之前pom配置为:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>            
    </plugins>
</build>

如果不添加pluginManagement标签写成如下代码:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin> 
        <plugin>
            <artifactId>maven-resources-plugin</artifactId>
            <configuration>
                <encoding>utf-8</encoding>
                <useDefaultDelimiters>true</useDefaultDelimiters>
            </configuration>
        </plugin>
    </plugins>
</build>

也是没有效果的。

 

原文地址:https://www.jianshu.com/p/363ad244bb06

 

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值