springboot 多环境配置及打包资源

  1. 配置多环境
	<!-- 多环境配置方案  命令dev包 : mvn -U clean compile package -P dev-->
	<profiles>
		<profile>
			<id>dev</id>
			<properties>
				<profileActive>dev</profileActive>
				<maven.test.skip>true</maven.test.skip>
				<scope.jar>compile</scope.jar>
			</properties>
			<activation>
				<activeByDefault>true</activeByDefault> <!-- 设置默认的打包环境 -->
			</activation>
		</profile>
		<profile>
			<id>prod</id>
			<properties>
				<profileActive>prod</profileActive> <!-- 自定义变量,在pom和yaml中引用 -->
				<maven.test.skip>true</maven.test.skip>
			</properties>
		</profile>
	</profiles>
  1. 把需要打包的资源添加进来,不用环境的配置文件放在 env/${profileActive}下,至于加载配置文件,请看我另外一篇文章springboot自定义yaml配置文件
	<build>
		<finalName>${project.artifactId}-${profileActive}-${project.version}</finalName>
		<resources>
			<resource>
				<directory>src/main/resources</directory>
				<filtering>true</filtering>
				<includes>
					<include>env/${profileActive}/**</include> <!-- 引用的是1中的多环境配置的自定义属性 -->
					<include>statics/**</include>
					<include>templates/**</include>
					<include>*.xml</include>
					<include>*.txt</include>
					<include>application-${profileActive}.yml</include>
					<include>application.yml</include>
				</includes>
			</resource>
		</resources>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.3.4.RELEASE</version>
			</plugin>
		</plugins>
	</build>
  1. 在application.yaml文件中引用profileActive属性,这样在打包的时候只需要执行 mvn -U clean compile package -P prod 就会自动把prod环境的资源都打进去了
spring:
  # 环境 dev|test|prod
  profiles:
    active: @profileActive@
posted @ 2020-12-17 15:15  一个小学僧  阅读(535)  评论(0编辑  收藏  举报