版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
在很多情况下,我们需要为开发环境,集成测试环境,生产环境打包.它们的配置文件有细微的差异,最好是一次能够生产所有环境的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打包: