Springboot切换配置文件

Springboot切换配置文件

运行方式切换

第一种方式(yml和properties)

1、编辑配置文件(三个,总配置,测试环境,开发环境)

application.yml

spring:
  profiles:
    active: dev

application-dev.yml

com: 
  name: dev

application-pro.yml

com: 
  name: pro

第二种方式(yml)

server:
  port: 8081
#选择要激活那个环境块
spring:
  profiles:
    active: test

---
server:
  port: 8083
spring:
  profiles: dev #配置环境的名称


---

server:
  port: 8084
spring:
  profiles: prod  #配置环境的名称

注意:如果yml和properties同时都配置了端口,并且没有激活其他环境 , 默认会使用properties配置文件的!

运行命令

java -jar swdl-0.0.1-SNAPSHOT.jar --spring.profiles.active=dev -Dspring.config.location=application-dev.yml

-spring.profiles.active:指定环境,即文件后缀

-Dspring.config.location:指定具体的文件名

打包方式切换

<profiles>
    <!-- 本地开发环境 -->
    <profile>
        <!--执行打包命令时将使用此处的id进行定位 -->
        <id>dev</id>
        <properties>
            <!--对应的环境放置配置文件的目录,上一步创建的为dev,这里设置为dev。-->
            <env>dev</env>
        </properties>
        <activation>
            <!--将dev设置为默认环境 -->
            <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>

<build>
    <resources>
        <!--打包的时候排除不相关的目录 -->
        <resource>
            <!--配置文件在resources目录下创建的文件夹 -->
            <directory>src/main/resources</directory>
            <excludes>
                <exclude>dev/*</exclude>
                <exclude>test/*</exclude>
                <exclude>prod/*</exclude>
            </excludes>
            <!--开启过滤器,此处不能忽略! -->
            <filtering>true</filtering>
        </resource>
        
        <!--将放置于resources文件夹下mybatis的mapper文件和一些不需要多环境的配置文件正常打包进去-->
        <resource>
            <!--如果将mybatis的mapper文件放在dao接口的同目录下,应该将此处设置为src/main/java -->
            <directory>src/main/resources</directory>
            <includes>
                <include>**/*.xml</include>
            </includes>
            <!--不需要过滤器过滤 -->
            <filtering>false</filtering>
        </resource>
        <!--配置相应配置文件夹的路径 -->
        <resource>
            <directory>src/main/resources/${env}</directory>
        </resource>
    </resources>

    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <!--这里写上main方法所在类的路径 -->
            <configuration>
               <mainClass>com.hancy.test.ChangeEnv</mainClass>
            </configuration>
            <executions>
                <execution>
                    <goals>
                        <goal>repackage</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
    </plugins>

</build>

打包

mvn clean package -Ptest #其中-P指定对应的环境进行打包
mvn clean package -Dmaven.test.skip=true # 其中-Dmaven.test.skip=true为跳过单元测试,不执行测试用例,也不编译测试用例类

posted on   Chase_Hanky  阅读(697)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示