Package the specified environment configuration through maven profile (excludes removes useless packages) and select the corresponding configuration file according to different environments when packaging through configuration pom

1. Background: When publishing projects, there are often different configurations in different environments, which are changed every time, resulting in repeated work. Therefore, the configuration files of different environments are packaged into the specified file by configuring the pom file of maven.

2. Not much nonsense, the code above, the following is the location where my current configuration file is stored, currently stored in the config folder, and created 3 directories to store configuration files in different environments, as shown below

3. The final effect is to package the configuration file of the corresponding environment into the class directory through the packaging command (so that the configuration file can be directly accessed after the springboot project is started), the following is the final effect
 

4. First, you need to configure the pom file, add the profiles node in the pom file, and configure the corresponding environment parameters

<profiles>
		<profile>
			<!-- 本地开发环境 -->
			<id>dev</id>
			<properties>
				<profiles.active>dev</profiles.active>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault>
			</activation>
		</profile>
		<profile>
			<!-- 测试环境 -->
			<id>test</id>
			<properties>
				<profiles.active>test</profiles.active>
			</properties>
		</profile>
		<profile>
			<!-- 生产环境 -->
			<id>pro</id>
			<properties>
				<profiles.active>pro</profiles.active>
			</properties>
		</profile>
	</profiles>

5. Configure the build node, as follows (the main function is the content of the resources node)
 

<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<skip>true</skip>
				</configuration>
			</plugin>
		</plugins>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>false</filtering>
				<excludes>
					<exclude>config/**</exclude>
				</excludes>
			</resource>
			<resource>
				<directory>src/main/resources/config/${profiles.active}</directory>
				<!--targetPath是指定目标文件打包到哪个文件夹下,这里默认就是放到resources下,也就是打包后的class文件夹下-->
				<!--<targetPath>config</targetPath>-->
			</resource>
		</resources>
	</build>

6. Let’s popularize the functions of the above nodes first.

(1) targetpath is to specify which directory the build resources go to, the default is the class directory

  (2) ${profiles.active} This is a dynamic parameter, that is, the dev, test, and pro variables we correspond to in step 4

  (3) directory is the directory of the specified attribute file, which is scanned and put into the targetpath directory. The default is ${basedir}/src/main/resourses

  (4) includes scans that include specified files

  (5) excludes specified file does not contain

  (6) Whether filtering makes the variable value of the filter file (that is, the properties file inside) effective in resourses
7. Use the packaging command to package

## 开发环境打包
mvn clean package -P dev

## 测试环境打包
mvn clean package -P test

## 生产环境打包
mvn clean package -P pro

8. If you are using ide, it also provides direct operation on the page, as shown in the figure below, you can package and formulate the file after selecting the specified environment configuration

 

Guess you like

Origin blog.csdn.net/fengbaozonghuiguoqu/article/details/123366228