mvn -P 结合Spring Boot profile使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/russle/article/details/81429949

Maven中的参数P代表(Profiles配置文件, 也就是我们在Spring Boot中使用的profile)
在指定的中,可以通过-P进行传递或者赋值。除了激活profile我们也可以通过-P !xxx来禁用某个profile
我们先看看Maven是如何解释-P的作用的:
-P,–activate-profiles Comma-delimited list of profiles to activate

POM文档

mvn package -P xxx

运行效果

pom.xm文档

Maven官方文档https://maven.apache.org/guides/introduction/introduction-to-profiles.html
Spring Boot中关于profiles的官方文档https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-profiles.html

假如pom.xml如下

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>beta</id>
            <properties>
                <spring.profiles.active>beta</spring.profiles.active>
            </properties>
        </profile>
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

mvn package -P xxx

打包时执行mvn clean package -P test将触发test环境的profile配置

mvn package -P test

然后我们查看生成的jar文件
这里写图片描述

运行效果

然后我们启动jar的时候不用添加–spring.profiles.active=test, 默认就使用test环境。当然如果我们使用–spring.profiles.active=xxx, 根据优先原则顺序,命令行的优先级高,会按照xxx来使用配置文件的。
注释:为了区分我在不同的profile使用了不同的端口,test环境使用8082端口。

默认不使用-spring.profiles.active=xxx的效果
这里写图片描述

猜你喜欢

转载自blog.csdn.net/russle/article/details/81429949