IDEA结合Maven的profile配置实现动态切换环境

之前的切换环境的方式 , 就是在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。定义的地方不同,它的作用范围也不同。

  1. 针对于特定项目的profile配置我们可以定义在该项目的pom.xml中。
  2. 针对于特定用户的profile配置,我们可以在用户的settings.xml文件中定义profile。该文件在用户家目录下的“.m2”目录下。
  3. 全局的profile配置。全局的profile是定义在Maven安装目录下的“conf/settings.xml”文件中的。

profile中能够定义的配置信息跟profile所处的位置是相关的。以下就分两种情况来讨论,一种是定义在settings.xml中,另一种是定义在pom.xml中。

  1. settings.xml中的profile元素是pom.xmlprofile元素的裁剪版本。它包含了idactivationrepositoriespluginRepositories和 properties元素。这里的profile元素只包含这五个子元素是因为这里只关心构建系统这个整体(这正是settings.xml文件的角色定位),而非单独的项目对象模型设置。如果一个settings.xml中的profile被激活,它的值会覆盖任何其它定义在pom.xml中带有相同id的profile
  2. 而一些比较细致一点的需要根据项目的不同来定义的就需要定义在项目的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>

  • 5
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值