Springboot Maven yaml 自動多環境打包工具

    在很多情況下,我們需要爲開發環境,集成測試環境,生產環境打包.它們的配置文件有細微的差異,最好是一次能夠生產所有環境的war包.比較常見的是設置application.properties中的spring.profiles.active環境變量
    但是yml的精簡特性很值得稱道,用過之後就不能忍受property 文件的臃腫了.試着google基於yml的多環境打包,並沒有一個很直觀的例子.雖然實現很簡單,還是要提供一個例子的,拿來就用,不用思考.

    實現的核心的Maven 插件maven resources plugin的拷貝文件和過濾文件的功能. 思路是把開發環境(dev),集成測試環境(sit),環境打包(prd)的配置文件分別放在src/main/resources 目錄下, 通過maven resources plugin把配置文件從src/main/resources/${profiles.active}拷貝到src/main/resources/ 然後打包
例如:
mvn clean
mvn package -P dev
 

pom.xml的profile配置和resources插件部分:

<?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>

	<artifactId>spring-boot-profile</artifactId>
	<packaging>jar</packaging>
	<name>Spring Boot Multiple Profiles yaml Example</name>
	<description>Spring Boot Multiple Profiles yaml Example</description>
	<url>https://www.mkyong.com</url>
	<version>1.1</version>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.1.2.RELEASE</version>
	</parent>

	<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>
	</dependencies>

	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<profiles.active>dev</profiles.active>
			</properties>
		</profile>
		<profile>
			<id>sit</id>
			<properties>
				<profiles.active>sit</profiles.active>
			</properties>
			<activation>
			    <!-- default active profile -->
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<id>prd</id>
			<properties>
				<profiles.active>prd</profiles.active>
			</properties>
		</profile>
	</profiles>

	<build>

		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<!-- don't copy below files -->
				<excludes>
					<exclude>dev/*</exclude>
					<exclude>sit/*</exclude>
					<exclude>prd/*</exclude>
				</excludes>
			</resource>
		</resources>

		<plugins>
			<!-- Package as an executable jar/war -->
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>

			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-surefire-plugin</artifactId>
			</plugin>

			<plugin>
				<artifactId>maven-resources-plugin</artifactId>
				<executions>
					<execution>
						<id>copy-resources</id>
						<phase>validate</phase>
						<goals>
							<goal>copy-resources</goal>
						</goals>
						<configuration>
						    <!-- copy files from active profile folder  -->
							<outputDirectory>src/main/resources</outputDirectory>
							<resources>
								<resource>
									<directory>src/main/resources/${profiles.active}</directory>
									<filtering>true</filtering>
								</resource>
							</resources>
						</configuration>
					</execution>
				</executions>
			</plugin>

		</plugins>
	</build>
</project>

每個profile都有自己的配置文件目錄,這些目錄的文件在打包的時候會被拷貝到resources目錄下:

使用此種方法打包生成的war包直接放到tomcat啓動就可以,不需要特別的設置.

 

命令行的打包命令:

 

Eclipse打包:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章