之前的切换环境的方式 , 就是在application.properties中配置 spring.profiles.active=dev , 然后通过修改dev为test或prod来切换项目环境 , 这样做的话每次切换环境都要重新改一下配置 , 而且如果不小心把本地改动提交到中央仓库了 , 可能会影响到其他同事拉取代码。
maven中提供了一个profile配置项,可以在打包时动态的指定环境配置.结合idea使用 , 我们可以实现不动任何代码来随意的切换我们的工作环境。
一、目录结构
二、配置详情
pom.xml
<!--pom.xml 文件-->
…… ……
…… ……
<!--配置不同的profile,对应不同的生产环境-->
<profiles>
<profile>
<!--开发-->
<id>dev</id>
<activation>
<!--默认开发环境-->
<activeByDefault>true</activeByDefault>
</activation>
<properties>
<activatedProperties>dev</activatedProperties>
</properties>
</profile>
<profile>
<!--生产-->
<id>pro</id>
<properties>
<activatedProperties>pro</activatedProperties>
</properties>
</profile>
<profile>
<!--测试-->
<id>test</id>
<properties>
<activatedProperties>test</activatedProperties>
</properties>
</profile>
</profiles>
</project>
application.properties
把原来固定的配置写成一个变量@XXXXX@ , 然后通过在pom.xml中配置不同的XXXXX节点来配置多个环境
#application.properties 文件
#spring.profiles.active = test 原先的写法
spring.profiles.active=@activatedProperties@
server.port=8880
application-{profile}.properties
#application-dev.properties 文件
server.port=8887
#application-pro.properties 文件
server.port=8888
#application-test.properties 文件
server.port=8889
三、动态切换环境
到这一步 , 我们就默认以dev为工作环境了
如果需要切换 , 只需要点开maven面板 , 在顶层的Profiles节点下勾选对应的环境即可
(如找不到该面板 , 可单击 View -> Tool Windows -> Maven Projects 展示出来)
不过在实操的时候发现,勾选之后需要点击重新载入(就是左上角那个), 然后重启项目 . 才能看到切换环境
点击pro环境后的结果如下
四、其他
疑问1:如果同时选择pro和test,那么应用会从哪个端口起来呢?
实操后发现,并不会报错,而是会使用后者覆盖前者,所谓前者后者就是在pom里定义的先后
疑问2:可以看到我图例里打码了两个profile,这明明没有在pom里定义?为什么会出现呢?
打码是因为这两个是公司的私库,不便于透露。那么他们是怎么出现的呢?
对于使用Maven3,我们可以有多个地方定义profile。定义的地方不同,它的作用范围也不同。
- 针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。
- 针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。
- 全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。
profile中能够定义的配置信息跟profile所处的位置是相关的。以下就分两种情况来讨论,一种是定义在settings.xml中,另一种是定义在pom.xml中。
settings.xml
中的profile
元素是pom.xml
中profile
元素的裁剪版本。它包含了id
、activation
、repositories
、pluginRepositories
和properties
元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings.xml
中的profile
被激活,它的值会覆盖任何其它定义在pom.xml
中带有相同id的profile
。- 而一些比较细致一点的需要根据项目的不同来定义的就需要定义在项目的pom.xml中。
比如以下 settings.xml 代码: profile 就做一件事:设置全局的 profile,一个是 nexus 仓库,一个是 aliyun 仓库,默认激活的是 nexus 仓库。 <activeProfile>nexus</activeProfile>
<?xml version="1.0" encoding="UTF-8"?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
<pluginGroups>
</pluginGroups>
<proxies>
</proxies>
<profiles>
<profile>
<id>nexus</id>
<repositories>
<repository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>nexus</id>
<url>http://192.168.1.73:8081/repository/maven-public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
<profile>
<id>aliyun</id>
<repositories>
<repository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun</id>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
<activeProfiles>
<activeProfile>nexus</activeProfile>
</activeProfiles>
</settings>