maven多环境打包部署

1、多环境properties文件创建

准备properties配置文件
在application.properties中配置标识环境
spring.profiles.active=@profiles.active@

在Spring Boot中多环境配置文件名需要满足application-{profiles.active}.properties的格式,其中{profiles.active}对应你的环境标识。

2、pom配置

<profiles>
    <profile>
        <id>tst</id>
        <properties>
            <profiles.active>tst</profiles.active>
        </properties>
    </profile>
    <profile>
        <!-- uat -->
        <id>uat</id>
        <properties>
            <profiles.active>uat</profiles.active>
        </properties>
        <!-- 是否默认 true表示默认-->
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
    </profile>
    <profile>
        <id>prd</id>
        <properties>
            <profiles.active>prd</profiles.active>
        </properties>
    </profile>
</profiles>
<build>
    <!-- 打包后包名称 -->
    <finalName>getJobData</finalName>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <configuration>
                <mainClass>com.java.springboot.Application</mainClass>
                <fork>true</fork>
                <addResources>true</addResources>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>8</source>
                <target>8</target>
            </configuration>
        </plugin>


    </plugins>

    <!-- mybatis配置文件 -->
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <includes>
                <include>mapper/*.xml</include>
            </includes>
        </resource>

        <!--排除配置文件-->
        <resource>
            <directory>src/main/resources</directory>
            <!--先排除所有的配置文件-->
            <excludes>
                <!--使用通配符,当然可以定义多个exclude标签进行排除-->
                <exclude>application*.properties</exclude>
            </excludes>
        </resource>

        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application.properties</include>
                <include>application-${profiles.active}.properties</include>
            </includes>
        </resource>
    </resources>
</build>

filetering是一个boolean变量,作用是将资源文件中的占位符替换成对应的值。
filtering为true,开启过滤,替换directory下的文件中的参数(eg. ${name})。会把过滤的文件打到classpath下
filtering为false,不过滤,不会进行占位符替换。会把不需要过滤的文件打到classpath下
如果不做filtering配置,pom中的${profiles.active}是可以被解析替换的。但是resource指定的资源文件application.properties中的@profiles.active@是不会被解析替换的

3、工程打包

在idea中,勾选不同的profile环境配置,就可以打包不同环境的jar包或war包
指定环境类型打包使用,比如使用-P指定为uat ,-DskipTests=true 跳过整个测试阶段,包括编译测试代码和运行测试用例
mvn compile package -DskipTests=true -Puat

posted @   swjdss  阅读(149)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· winform 绘制太阳,地球,月球 运作规律
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
点击右上角即可分享
微信分享提示