Java后端使用mvn区分dev,pro,local环境

该博客主要介绍Java项目多环境配置及Mvn编译相关内容。在pom.xml中设置了local、dev、pro三种环境,默认使用dev环境;还提及了yml文件配置。Mvn编译命令可通过 -P 指定不同环境配置,不指定则默认使用dev,启动后会输出使用的配置。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一,pom.xml配置

local代表本地环境
dev代表开发环境
pro代表生产环境
设置默认环境使用dev

 <!--环境配置-->
    <profiles>
        <profile>
            <id>local</id>
            <properties>
                <!-- 环境标识,需要与配置文件的名称相对应 -->
                <activatedProperties>local</activatedProperties>
            </properties>
        </profile>

        <profile>
            <id>dev</id>
            <properties>
                <activatedProperties>dev</activatedProperties>
            </properties>
            <activation>
                <!-- 默认环境 -->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>

        <profile>
            <id>pro</id>
            <properties>
                <activatedProperties>pro</activatedProperties>
            </properties>
        </profile>
    </profiles>


    <build>
        <resources>
            <resource>
                <!--配置文件路径  -->
                <directory>src/main/resources</directory> <!--这里对应项目存放配置文件的目录-->
                <!--开启filtering功能  -->
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <executions>
                    <execution>
                        <goals>
                            <!--创建一个自动可执行的jar或war文件 -->
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

 

 

二,yml文件配置

 

spring:
  profiles:
    active: @activatedProperties@

 

 

三,mvn编译命令

-P dev   指定使用dev配置
-P pro   指定使用pro配置
-P local 指定使用local配置

mvn clean install package -P pro -Dmaven.test.skip=true

如不指定 默认使用dev配置 

mvn clean install package -Dmaven.test.skip=true

 

 

四,验证

启动后会输出使用的配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值