maven打包时配置多环境参数

1. pom配置

xml 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    
    <!-- 其他配置 -->

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <!-- 只打包指定环境的配置文件 -->
                <includes>
                    <include>application-${profile.active}.properties</include>
                    <include>application.properties</include>
                </includes>
                <!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 -->
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

    <profiles>
        <profile>
            <id>dev</id>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
            <!-- 默认环境 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <profile>
            <id>long</id>
            <properties>
                <profile.active>long</profile.active>
            </properties>
        </profile>
        <profile>
            <id>sit</id>
            <properties>
                <profile.active>sit</profile.active>
            </properties>
        </profile>
    </profiles>

</project>

2. application配置

yaml 复制代码
spring:
  profiles:
    active: @profile.active@

此处的 profile.active 与 上述 pom.xml 配置文件中的 properties 属性一致

3. 打包

bash 复制代码
mvn clean package -Plong -Dmaven.test.skip=true

-Plong:指定打包的环境为 long

4. 注意

因为在 pom 文件中,修改了打包配置,只会打包进指定环境的配置文件,所以在启动时不用再指定环境参数了

相关推荐
弹简特6 小时前
【JavaEE04-后端部分】Maven 小介绍:Java 开发的构建利器基础
java·maven
计算机毕设指导67 小时前
基于微信小程序的智能停车场管理系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea
A懿轩A7 小时前
【Maven 构建工具】Maven 依赖管理详解:坐标、传递、作用域与依赖冲突解决(一篇搞懂)
java·linux·maven
无尽的沉默1 天前
使用Spring Initializr 快速创建Maven管理的springBoot项目
spring boot·spring·maven
苍煜2 天前
万字详解Maven打包策略:从基础插件到多模块实战
java·maven
2301_780029042 天前
支付宝sdk导入错误
java·开发语言·maven
缘空如是2 天前
java 构建代码推送maven 公共仓
java·开发语言·maven
Knight_AL2 天前
在 Windows 上安装本地 JAR 到 Maven 仓库
windows·maven·jar
爱敲代码的小鱼2 天前
Maven的简介:
java·maven
不会c+3 天前
Maven私服的搭建与使用
java·maven