Maven与SpringBoot多环境兼容

发布时间 2023-03-23 15:05:51作者: qintee

 

Maven的pom.xml设了多环境,Boot的application.yml也设置了多环境

要使得Boot读取到Maven中的配置就需要在Maven的pom.xml中设置<profile.active>

pom.xml中主要修改这里

 

 

 pom.xml全部

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.0</version>
    </parent>
    <groupId>com.itheima</groupId>
    <artifactId>springboot_05_maven_and_boot_profile</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.0</version>
            </plugin>
            <plugin>
<!--                一个插件,作用是解析application.yml中的占位符-->
                <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>
        </plugins>
    </build>

    <profiles>
        <!--开发环境-->
        <profile>
            <id>dev</id>
            <properties>
<!--                设置当前生效的值,让SpringBoot听从Maven进行打包-->
                <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>

</project>

 

application.yml

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

---
#不过时的写法
#开发
spring:
  config:
    activate:
      on-profile: dev
server:
  port: 80
---
#过时的写法,但也能用
#生产
spring:
  profiles: pro
server:
  port: 81
---
#测试
spring:
  profiles: test
server:
  port: 82
---

 

程序启动时

Boot的application.yml就修改为Maven的pom.xml配置

 

 

 注意要使得application.yml的${profile.active}被正确解析,在pom.xml添加 maven-resources-plugin 插件才行

多说一句,如果这个插件标红,要加入<version>才行,version与org.springframework.boot对于一致即可

 

 

代码来源:itheima