springboot 多环境配置
1.案例简单介绍
- 1.1项目结构
- 1.2 yml中的内容(不包括application.yml)
2.不借助maven的profile
- 2.1 application.yml中的内容----->这里指定dev为默认环境配置
spring:
profiles:
active: dev
- 2.2 打包----->在项目根目录下运行如下命令
mvn clean package -Dmaven.test.skip=true
- 2.3 项目部署 ----->在jar包所在目录运行如下命令
java -jar demo-0.0.1-SNAPSHOT.jar --spring.profiles.active=prod
等号后面等于prod则实际使用的是application-prod.yml文件里的配置,等于test则实际使用的是application-test.yml文件里的配置,以此类推,去掉 --spring.profiles.active=prod,则使用默认的application-dev.yml文件里的配置.
3.借助maven的profile标签
- 3.1 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 http://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.1.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.nrsc.application</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<profiles>
<!-- ++++++++++++++++++++++++++多环境配置++++++++++++++++++++++++++++++++++ -->
<profile>
<id>dev</id>
<properties>
<packaging>jar</packaging>
<activatedProperties>dev</activatedProperties>
<main-class>com.nrsc.application.demo.DemoApplication</main-class>
</properties>
<activation>
<!--默认使用的环境-->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<id>test</id>
<properties>
<packaging>jar</packaging>
<activatedProperties>test</activatedProperties>
<main-class>com.nrsc.application.demo.DemoApplication</main-class>
</properties>
</profile>
<profile>
<id>uat</id>
<properties>
<packaging>jar</packaging>
<activatedProperties>uat</activatedProperties>
<main-class>com.nrsc.application.demo.DemoApplication</main-class>
</properties>
</profile>
<profile>
<id>prod</id>
<properties>
<packaging>jar</packaging>
<activatedProperties>prod</activatedProperties>
<main-class>com.nrsc.application.demo.DemoApplication</main-class>
</properties>
</profile>
<!-- ++++++++++++++++++++++++++多环境配置++++++++++++++++++++++++++++++++++ -->
</profiles>
<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>
<!--指定打包的名称-->
<!--<finalName>application-demo</finalName> -->
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
- 3.2 application.yml中需要修改成下面的样子
spring:
profiles:
active: @[email protected]
- 3.3 打包和部署同不借助maven的profile标签中内容相同.
4. 两者区别
我所知道的区别主要在idea的显示上,描述如下: