springboot使用maven實現(xiàn)多環(huán)境運行和打包問題
在實際開發(fā)過程中,可能需要不斷進行環(huán)境的切換和打包部署,通常我們會選擇在application.properties中修改不同環(huán)境對應的配置文件,這種方式不僅效率低,而且很容易發(fā)生錯誤,造成不必要的麻煩降低工作效率。maven提供了多環(huán)境配置,可以方便實現(xiàn)不同環(huán)境的配置切換和打包。
一、配置文件
在classpath根目錄(在springboot工程中,classpath為resources目錄)下創(chuàng)建多個環(huán)境的配置文件,分別命名application-dev.properties、application-test.properties、application-pro.properties,分別對應開發(fā)環(huán)境、測試環(huán)境、生產環(huán)境。
springboot默認使用logback日志進行日志處理,如果日志配置也需要根據不同環(huán)境進行切換,則在classpath下創(chuàng)建logback-dev.xml、logback-test.xml、logback-pro.xml,
創(chuàng)建完成后,目錄結構如下:

在對應環(huán)境的配置文件中,指定使用的日志配置文件,以開發(fā)環(huán)境為例其他環(huán)境的配置以此類推,在application-dev.properties文件中指定使用的日志配置文件logback-dev.xml
# 日志配置 logging.config=classpath:logback-dev.xml
二、配置pom文件
1.配置多個環(huán)境
<project> <!--分別設置開發(fā),測試,生產環(huán)境--> <profiles> <!-- 開發(fā)環(huán)境 --> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <properties> <environment>dev</environment> </properties> </profile> <!-- 測試環(huán)境 --> <profile> <id>test</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <environment>test</environment> </properties> </profile> <!-- 生產環(huán)境 --> <profile> <id>pro</id> <activation> <activeByDefault>false</activeByDefault> </activation> <properties> <environment>pro</environment> </properties> </profile> </profiles> </project>
<environment>標簽中的名稱對應配置文件的profile名稱,即-后面的名稱。<activeByDefault>設置為true時表示默認激活該環(huán)境。
2.在pom文件中指定resource目錄和配置文件
<build>
<resources>
<resource>
<!-- 指定配置文件所在的resource目錄 -->
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>application-${environment}.properties</include>
<include>logback-${environment}.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>配置完成后,在Maven Projects/Profiles中會出現(xiàn)環(huán)境切換選項如下圖所示。

三、配置動態(tài)變量
我們想要切換不同的配置文件,需要在application.properties文件中指定spring.profiles.active參數,顯然該參數不是靜態(tài)的,需要根據你選擇的環(huán)境進行動態(tài)切換,在pom文件中使用${}讀取標簽配置,但是在application.properties中需使用占位符@@進行配置。
spring.profiles.active=@environment@
environment對應pom文件中的<environment>標簽
四、使用及注意事項
我們在使用時,需要先指定要運行或打包的環(huán)境,在Maven Projects/Profiles中勾選對應的環(huán)境名稱后再運行或打包。
通常情況下,會默認勾選pom中<activeByDefault>true</activeByDefault>設置為true的環(huán)境,當我們同時勾選多個環(huán)境時,則按pom文件中配置的先后順序,后配置的環(huán)境將被激活。
注意事項:
如果發(fā)現(xiàn)pom文件中${environment}報錯,說明你還沒有指定需要運行或打包的環(huán)境,在Maven Projects/Profiles中勾選一個即可。

按照以上步驟就可以實現(xiàn)多環(huán)境的輕松切換了,下面附上完整的配置文件
1.application.properties
spring.profiles.active=@environment@
2.application-dev.properties
server.port=7000 # 數據源配置 spring.datasource.name=dev spring.datasource.url=jdbc:mysql://127.0.0.1:3306/test spring.datasource.username=root spring.datasource.password= spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.type=com.alibaba.druid.pool.DruidDataSource # redis配置 spring.redis.database=0 spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.jedis.pool.max-active=-1 spring.redis.jedis.pool.max-wait=-1 spring.redis.jedis.pool.max-idle=-1 spring.redis.jedis.pool.min-idle=0 spring.redis.timeout=1000 spring.session.store-type=redis # 日志配置 logging.config=classpath:logback-dev.xml
3.logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!--<include resource="org/springframework/boot/logging/logback/base.xml"/>-->
<!-- 項目的appid -->
<property name="APP_ID" value="demo"/>
<property name="LOG_PATH" value="log"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg %n</pattern>
</encoder>
</appender>
<appender name="FILE_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
</filter>
<file>${LOG_PATH}/${APP_ID}/access.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${APP_ID}/access.log.%d{yyyy-MM-dd}.zip</fileNamePattern>
<!-- maxHistory配置了日志在服務器上面只存留十個備份 -->
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE_DEBUG"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>DEBUG</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/${APP_ID}/access_debug.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${APP_ID}/access_debug.log.%d{yyyy-MM-dd}.zip</fileNamePattern>
<!-- maxHistory配置了日志在服務器上面只存留十個備份 -->
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE_INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/${APP_ID}/access_info.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${APP_ID}/access_info.log.%d{yyyy-MM-dd}.zip</fileNamePattern>
<!-- maxHistory配置了日志在服務器上面只存留十個備份 -->
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE_WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/${APP_ID}/access_warn.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${APP_ID}/access_warn.log.%d{yyyy-MM-dd}.zip</fileNamePattern>
<!-- maxHistory配置了日志在服務器上面只存留十個備份 -->
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="FILE_ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>${LOG_PATH}/${APP_ID}/access_error.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_PATH}/${APP_ID}/access_error.log.%d{yyyy-MM-dd}.zip</fileNamePattern>
<!-- maxHistory配置了日志在服務器上面只存留十個備份 -->
<maxHistory>10</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%d{yyyy-MM-dd HH:mm:ss} %-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>
</encoder>
</appender>
<appender name="ASYNC_LOG" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 -->
<queueSize>512</queueSize>
<appender-ref ref="FILE_LOG"/>
</appender>
<appender name="ASYNC_LOG" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 -->
<queueSize>512</queueSize>
<appender-ref ref="FILE_LOG"/>
</appender>
<appender name="ASYNC_LOG_DEBUG" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 -->
<queueSize>512</queueSize>
<appender-ref ref="FILE_DEBUG"/>
</appender>
<appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 -->
<queueSize>512</queueSize>
<appender-ref ref="FILE_INFO"/>
</appender>
<appender name="ASYNC_LOG_WARN" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 -->
<queueSize>512</queueSize>
<appender-ref ref="FILE_WARN"/>
</appender>
<appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
<!-- 不丟失日志.默認的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
<discardingThreshold>0</discardingThreshold>
<!-- 更改默認的隊列的深度,該值會影響性能.默認值為256 -->
<queueSize>512</queueSize>
<appender-ref ref="FILE_ERROR"/>
</appender>
<root level="INFO">
<!-- appender referenced after it is defined -->
<appender-ref ref="STDOUT"/>
<appender-ref ref="ASYNC_LOG"/>
<appender-ref ref="ASYNC_LOG_DEBUG"/>
<appender-ref ref="ASYNC_LOG_INFO"/>
<appender-ref ref="ASYNC_LOG_WARN"/>
<appender-ref ref="ASYNC_LOG_ERROR"/>
</root>
<logger name="org.springframework" level="INFO"/>
</configuration>4.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.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.soft.springboot</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>
<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>
<executions>
<execution>
<goals>
<goal>repackage</goal><!--可以把依賴的包都打包到生成的Jar包中 -->
</goals>
</execution>
</executions>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>application.properties</include>
<include>application-${environment}.properties</include>
<include>logback-${environment}.xml</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>
</build>
<!--分別設置開發(fā),測試,生產環(huán)境-->
<profiles>
<!-- 開發(fā)環(huán)境 -->
<profile>
<id>dev</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<environment>dev</environment>
</properties>
</profile>
<!-- 測試環(huán)境 -->
<profile>
<id>test</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment>test</environment>
</properties>
</profile>
<!-- 生產環(huán)境 -->
<profile>
<id>pro</id>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
<properties>
<environment>pro</environment>
</properties>
</profile>
</profiles>
</project>總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
Java使用OpenOffice將office文件轉換為PDF的示例方法
OpenOffice是一個開源的辦公套件,它包含了文檔處理、電子表格、演示文稿以及繪圖等多種功能,類似于Microsoft Office,本文將給大家介紹Java使用OpenOffice將office文件轉換為PDF的示例方法,需要的朋友可以參考下2024-09-09
SSH框架網上商城項目第10戰(zhàn)之搭建商品類基本模塊
這篇文章主要為大家詳細介紹了SSH框架網上商城項目第10戰(zhàn)之搭建商品類基本模塊的相關資料,有一定的實用性,感興趣的小伙伴們可以參考一下2016-06-06
Java語言之LinkedList和鏈表的實現(xiàn)方法
LinkedList是由傳統(tǒng)的鏈表數據結構演變而來的,鏈表是一種基本的數據結構,它可以動態(tài)地增加或刪除元素,下面這篇文章主要給大家介紹了關于Java語言之LinkedList和鏈表的實現(xiàn)方法,需要的朋友可以參考下2023-05-05
springboot源碼中this::selfInitialize怪異用法的含義解析
這篇文章主要介紹了springboot源碼中this::selfInitialize怪異用法的含義解析,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-03-03
在 IntelliJ IDEA 中安裝和配置 Java 17的實戰(zhàn)記錄
文章介紹了如何在IntelliJ IDEA中安裝和配置Java 17,包括下載JDK、配置項目SDK、設置語言級別以及驗證配置,本文給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧2025-10-10
SpringBoot常用注解@RestControllerAdvice詳解
這篇文章主要介紹了SpringBoot常用注解@RestControllerAdvice詳解,@RestControllerAdvice是一個組合注解,由@ControllerAdvice、@ResponseBody組成,而@ControllerAdvice繼承了@Component,因此@RestControllerAdvice本質上是個Component,需要的朋友可以參考下2024-01-01

