摘要
多环境支持是每个项目都要面对的问题,且不说很多环境,哪怕简单的项目也是需要区分本地开发的环境和正式运行的生产环境,不同的项目架构有不同的实现方式,这篇文章先说明利用Profile来进行配置的方式。
如今使用Spring Boot的项目已经很多了,用起来也算方便,下面来看看使用Maven作为构建工具,Spring Boot为基础框架,如何在不同的环境实现不同的运行状态。
Maven的profile
众所周知,Maven是支持profile配置的,在pom.xml配置文件中可以配置,在运行mvn命令时,可以指定profile:
mvn -P [profile]
很容易想到利用profile配置参数,然后根据参数打包不同的资源,这样就可以实现指定不同的profile时可以用不同的资源打包,就可以以不同的状态运行,实现目的。
下面将举一个简单的例子,实现打包不同的配置资源目录。
pom.xml
……
<profiles>
<profile>
<id>local</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<env>local</env>
</properties>
</profile>
<profile>
<id>real</id>
<properties>
<env>release</env>
</properties>
</profile>
</profiles>
<build>
<resources>
……
<resource>
<directory>src/main/resources-${env}</directory>
<includes>
<include>**/*</include>