欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

springboot使用maven實現(xiàn)多環(huán)境運行和打包問題

 更新時間:2023年07月20日 10:27:03   作者:碼農(nóng)小白白  
這篇文章主要介紹了springboot使用maven實現(xiàn)多環(huán)境運行和打包問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

在實際開發(fā)過程中,可能需要不斷進行環(huán)境的切換和打包部署,通常我們會選擇在application.properties中修改不同環(huán)境對應(yīng)的配置文件,這種方式不僅效率低,而且很容易發(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,分別對應(yīng)開發(fā)環(huán)境、測試環(huán)境、生產(chǎn)環(huán)境。

springboot默認(rèn)使用logback日志進行日志處理,如果日志配置也需要根據(jù)不同環(huán)境進行切換,則在classpath下創(chuàng)建logback-dev.xml、logback-test.xml、logback-pro.xml,

創(chuàng)建完成后,目錄結(jié)構(gòu)如下:

在這里插入圖片描述

在對應(yī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>
	<!--分別設(shè)置開發(fā),測試,生產(chǎn)環(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>
		<!-- 生產(chǎn)環(huán)境 -->
		<profile>
			<id>pro</id>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
			<properties>
				<environment>pro</environment>
			</properties>
		</profile>
	</profiles>
</project>
  • <environment>標(biāo)簽中的名稱對應(yīng)配置文件的profile名稱,即-后面的名稱。
  • <activeByDefault>設(shè)置為true時表示默認(rèn)激活該環(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參數(shù),顯然該參數(shù)不是靜態(tài)的,需要根據(jù)你選擇的環(huán)境進行動態(tài)切換,在pom文件中使用${}讀取標(biāo)簽配置,但是在application.properties中需使用占位符@@進行配置。

spring.profiles.active=@environment@

environment對應(yīng)pom文件中的<environment>標(biāo)簽

四、使用及注意事項

我們在使用時,需要先指定要運行或打包的環(huán)境,在Maven Projects/Profiles中勾選對應(yīng)的環(huán)境名稱后再運行或打包。

通常情況下,會默認(rèn)勾選pom中<activeByDefault>true</activeByDefault>設(shè)置為true的環(huán)境,當(dāng)我們同時勾選多個環(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
# 數(shù)據(jù)源配置
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配置了日志在服務(wù)器上面只存留十個備份 -->
            <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配置了日志在服務(wù)器上面只存留十個備份 -->
            <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配置了日志在服務(wù)器上面只存留十個備份 -->
            <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配置了日志在服務(wù)器上面只存留十個備份 -->
            <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配置了日志在服務(wù)器上面只存留十個備份 -->
            <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">
        <!-- 不丟失日志.默認(rèn)的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默認(rèn)的隊列的深度,該值會影響性能.默認(rèn)值為256 -->
        <queueSize>512</queueSize>
        <appender-ref ref="FILE_LOG"/>
    </appender>
    <appender name="ASYNC_LOG" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丟失日志.默認(rèn)的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默認(rèn)的隊列的深度,該值會影響性能.默認(rèn)值為256 -->
        <queueSize>512</queueSize>
        <appender-ref ref="FILE_LOG"/>
    </appender>
    <appender name="ASYNC_LOG_DEBUG" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丟失日志.默認(rèn)的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默認(rèn)的隊列的深度,該值會影響性能.默認(rèn)值為256 -->
        <queueSize>512</queueSize>
        <appender-ref ref="FILE_DEBUG"/>
    </appender>
    <appender name="ASYNC_LOG_INFO" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丟失日志.默認(rèn)的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默認(rèn)的隊列的深度,該值會影響性能.默認(rèn)值為256 -->
        <queueSize>512</queueSize>
        <appender-ref ref="FILE_INFO"/>
    </appender>
    <appender name="ASYNC_LOG_WARN" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丟失日志.默認(rèn)的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默認(rèn)的隊列的深度,該值會影響性能.默認(rèn)值為256 -->
        <queueSize>512</queueSize>
        <appender-ref ref="FILE_WARN"/>
    </appender>
    <appender name="ASYNC_LOG_ERROR" class="ch.qos.logback.classic.AsyncAppender">
        <!-- 不丟失日志.默認(rèn)的,如果隊列的80%已滿,則會丟棄TRACT、DEBUG、INFO級別的日志 -->
        <discardingThreshold>0</discardingThreshold>
        <!-- 更改默認(rèn)的隊列的深度,該值會影響性能.默認(rèn)值為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>
	<!--分別設(shè)置開發(fā),測試,生產(chǎn)環(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>
		<!-- 生產(chǎn)環(huán)境 -->
		<profile>
			<id>pro</id>
			<activation>
				<activeByDefault>false</activeByDefault>
			</activation>
			<properties>
				<environment>pro</environment>
			</properties>
		</profile>
	</profiles>
</project>

總結(jié)

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論