多环境开发兼容问题

多环境开发兼容问题

如果在maven和boot中都有多环境开发,那么由谁来做主呢?

在maven中我们默认是pro环境:

 <!--配置多环境-->
    <profiles>
        <!--开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
        </profile>
        
        <!--生产环境-->
        <profile>
            <id>pro</id>
            <properties>
                <profile.active>pro</profile.active>
            </properties>
            <!--设置是否使默认启动环境-->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <!--测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <profile.active>test</profile.active>
            </properties>
        </profile>
    </profiles>

在root中我们默认dev环境:

#设置启用的环境
spring:
  profiles:
    active: dev

---
#开发
#spring:
#  profiles: dev
#server:
#  port: 80

#第二种写法
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80

---
#生产
spring:
  profiles: pro
server:
  port: 81



---
#测试

spring:
  profiles: test
server:
  port: 82

我们会发现如果不配置,是boot中的为主。

怎样配置?

#设置启用的环境
spring:
  profiles:
    active: ${profile.active}

---
#开发
#spring:
#  profiles: dev
#server:
#  port: 80

#第二种写法
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80

---
#生产
spring:
  profiles: pro
server:
  port: 81



---
#测试

spring:
  profiles: test
server:
  port: 82

此时并不能解析${profile.active},我们需要在pom.xml中配置

加入插件:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.2.0</version>
    <configuration>
        <encoding>utf-8</encoding>
        <useDefaultDelimiters>true</useDefaultDelimiters>
    </configuration>
</plugin>

这样再打包运行就是以maven中的为主了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值