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)