在idea中利用maven实现多环境配置自动打包的流程步骤

 更新时间:2024年11月29日 11:21:04   作者:物宇维  
这篇文章主要介绍了在idea中利用maven实现多环境配置自动打包的流程步骤,文中通过图文和代码示例介绍的非常详细,对大家的学习或工作有一定帮助,需要的朋友可以参考下

spring的多环境配置

spring提供了多环境配置文件的功能,只需要根据配置文件后缀即可加载对应的配置文件

spring:
  profiles:
    # 修改后缀即可加载对应的配置
    active: dev

还需要准备好对应的配置文件

在这里插入图片描述

利用maven实现多环境配置自动打包

但是这怎样写死的 spring.profiles.active 属性在使用idea给不同环境打包时每次都需要手动修改,可以使用maven提供的动能实现自动映射配置属性。

在pom.xml文件中主要添加以下配置

    <!--配置多环境打包-->
    <profiles>
        <!--开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <!--自定义的属性-->
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <activation>
                <!--如果不指定,则默认使用dev开发环境配置-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>pro</spring.profiles.active>
            </properties>
        </profile>
    </profiles>
    <build>
        <finalName>${project.name}</finalName>
        <resources>
            <resource>
                # mavan要对哪个目录解析 ${} 标签
                <directory>${project.basedir}/demo/src/main/resources</directory>
                # 是否过滤
                <filtering>true</filtering>
            </resource>
        </resources>
    </build>

在添加了上述配置后,在pom.xml文件中右击 --> Maven --> 重新加载项目,就能看到右边多了一些配置

在这里插入图片描述

然后修改application.yml文件的配置

spring:
  profiles:
    # 修改后缀即可加载对应的配置
    active: ${spring.profiles.active}

选中想打包的环境

在这里插入图片描述

打包后查看实际打出来的包配置文件的属性是否真正加载了

在这里插入图片描述

在这里插入图片描述

可以看到正是所选的 pro 环境,这样就不用每次打包时都手动修改 spring.profiles.active 属性,只需要在打包时勾选一下maven对应的profiles属性即可

完整的 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">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.itheima</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>demo</description>
    <properties>
        <project.name>demo</project.name>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.2.2</version>
        </dependency>

        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <!--配置多环境打包-->
    <profiles>
        <!--开发环境-->
        <profile>
            <id>dev</id>
            <properties>
                <!--自定义的属性-->
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <activation>
                <!--如果不指定,则默认使用dev开发环境配置-->
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 测试环境-->
        <profile>
            <id>test</id>
            <properties>
                <spring.profiles.active>test</spring.profiles.active>
            </properties>
        </profile>
        <!--生产环境-->
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>pro</spring.profiles.active>
            </properties>
        </profile>
    </profiles>

    <build>
        <finalName>${project.name}</finalName>
        <resources>
            <resource>
                <directory>${project.basedir}/demo/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.demo.DemoApplication</mainClass>
                    <skip>true</skip>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

以上就是在idea中利用maven实现多环境配置自动打包的流程步骤的详细内容,更多关于idea利用maven多环境配置打包的资料请关注脚本之家其它相关文章!

相关文章

  • Java 程序员容易犯的10个SQL错误

    Java 程序员容易犯的10个SQL错误

    本文介绍了Java 程序员容易犯的10个SQL错误。具有很好的参考价值,下面跟着小编一起来看下吧
    2017-01-01
  • 使用Spring注解@EventListener实现监听原理

    使用Spring注解@EventListener实现监听原理

    这篇文章主要介绍了使用Spring注解@EventListener实现监听原理,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-08-08
  • SpringBoot 异步线程间数据传递的实现

    SpringBoot 异步线程间数据传递的实现

    本文主要介绍了SpringBoot 异步线程间数据传递的实现,包括异步线程的基本概念、数据传递的方式、具体实现方式等,具有一定的参考价值,感兴趣的可以了解一下
    2024-03-03
  • SpringBoot配置log4j输出日志的案例讲解

    SpringBoot配置log4j输出日志的案例讲解

    这篇文章主要介绍了SpringBoot配置log4j输出日志的案例讲解,springboot框架中默认使用logback进行日志输出,当然它也可以配置其它的日志框架,需要的朋友可以参考下
    2022-11-11
  • Java文件选择对话框JFileChooser使用详解

    Java文件选择对话框JFileChooser使用详解

    这篇文章主要介绍了Java文件选择对话框JFileChooser使用详解的相关资料,需要的朋友可以参考下
    2015-07-07
  • springboot2.x 接入阿里云市场短信发送的实现

    springboot2.x 接入阿里云市场短信发送的实现

    本文主要介绍了springboot2.x 接入阿里云市场短信发送的实现,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-11-11
  • FastJSON字段智能匹配踩坑的解决

    FastJSON字段智能匹配踩坑的解决

    这篇文章主要介绍了FastJSON字段智能匹配踩坑的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-06-06
  • springboot热部署class XX cannot be cast to class XX解决方案

    springboot热部署class XX cannot be cast&nbs

    在使用DevTools进行热加载时遇到的`classXXcannotbecasttoclassXX`错误,以及解决该问题的方法,通过在`resources`目录下创建`META-INF/spring-devtools.properties`文件,并添加相应的配置,可以有效解决此问题,使DevTools热加载功能得以正常工作
    2025-02-02
  • SpringBoot整合MyBatis四种常用的分页方式(详细总结)

    SpringBoot整合MyBatis四种常用的分页方式(详细总结)

    这篇文章详细给大家总结了SpringBoot整合MyBatis四种常用的分页方式,文中通过代码示例为大家介绍的非常详细,需要的朋友可以参考下
    2023-07-07
  • 详解Java包装类及自动装箱拆箱

    详解Java包装类及自动装箱拆箱

    这篇文章主要介绍了Java包装类及自动装箱拆箱,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2019-03-03

最新评论