7.Maven隔离

文章介绍了在企业项目开发中,如何利用Maven的环境隔离功能来管理不同环境(如开发、测试、生产)的配置。通过创建不同的配置文件,设置Maven的profiles,并在SpringBoot应用中动态激活所需配置,实现无缝切换。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.简介
在企业项目开发时,开发环境和生产环境的配置信息是不一样的,比如连接MySQL、Redis等配置,如果每次部署或者开发都要更改配置文件就太麻烦了,这时就可以使用Maven的环境隔离。

2.隔离
(1).创建配置文件
在这里插入图片描述

(2).pom配置

<?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.7.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>springboot</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>springboot</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

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

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

    <build>
        <resources>
            <resource>
                <directory>src/main/java</directory>
                <filtering>true</filtering>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering>
                <includes>
                    <include>META-INF/*</include>
                    <include>*</include>
                </includes>
            </resource>
        </resources>
    </build>

    <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>
        <profile>
            <id>prd</id>
            <properties>
                <build.profile.id>prd</build.profile.id>
            </properties>
        </profile>
    </profiles>

</project>

(3).在SpringBoot配置文件中配置需要启用的配置文件

server:
  port: 8080

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

(4).选择prd配置启动项目
在这里插入图片描述
可以看到,激活配置项为prd。

2023-03-19 18:34:40.125  INFO 10509 --- [           main] c.e.s.SpringBootDemoApplication          : Starting SpringBootDemoApplication using Java 1.8.0_144 on wenleideMacBook-Pro.local with PID 10509 (/Users/wenlei/Documents/code/study/springboot/target/classes started by wenlei in /Users/wenlei/Documents/code/study/springboot)
2023-03-19 18:34:40.128  INFO 10509 --- [           main] c.e.s.SpringBootDemoApplication          : The following 1 profile is active: "prd"
2023-03-19 18:34:40.983  INFO 10509 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8080 (http)
2023-03-19 18:34:40.988  INFO 10509 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2023-03-19 18:34:40.988  INFO 10509 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.68]
2023-03-19 18:34:41.103  INFO 10509 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2023-03-19 18:34:41.103  INFO 10509 --- [           main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 906 ms
2023-03-19 18:34:41.457  INFO 10509 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8080 (http) with context path ''
2023-03-19 18:34:41.467  INFO 10509 --- [           main] c.e.s.SpringBootDemoApplication          : Started SpringBootDemoApplication in 1.658 seconds (JVM running for 2.304)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值