zoukankan      html  css  js  c++  java
  • Spring boot使用Maven Profile配合Spring Profile进行多环境配置和打包

    这是很简单的事,我都不想写

    但是用了Maven Profile之后,我打包就不用每次都改application.yml文件了

    所以特意记录一下

    首先你的Spring Profile要有多环境配置文件

    在pom.xml添加

    #识别多环境配置
    <profiles>
        <profile>
            <id>dev</id>
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
            <properties>
                <profile.active>dev</profile.active>
            </properties>
        </profile>
        .....test就不写了
        <profile>
            <id>prod</id>
            <properties>
                <profile.active>prod</profile.active>
            </properties>
        </profile>
    </profiles>    

    此时打包就是这样了

    三 追求完美

    在进行打包的时候,我们并不需要把dev或者test等配置文件打包进行,所有我们在打包的时候进行如下配置

    所以在pom.xml添加

    <build>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <!--先排除所有的配置文件-->
                    <excludes>
                        <exclude>application*.yml</exclude>
                    </excludes>
                </resource>
                <resource>
                    <directory>src/main/resources</directory>
                    <!--引入所需环境的配置文件-->
                    <filtering>true</filtering>
                    <includes>
                        <include>application.yml</include>
                        <include>application-${profile.active}.yml</include>
                    </includes>
                </resource>
            </resources>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <classifier>exec</classifier>
                    </configuration>
                </plugin>
            </plugins>
        </build>

    修改application.yml

    spring:
      profiles:
        active: @profile.active@ #此处由maven的环境选择决定
  • 相关阅读:
    SpringCloud--gateway网关基础入门
    SpringCloud--gateway网关基本介绍
    编程的命名规则
    [java基础知识]java安装步骤
    [java基础]java跨平台的基础知识
    [java基础]计算机基础知识
    外观模式
    适配器模式
    设计模式UML类图基础
    拾取坐标
  • 原文地址:https://www.cnblogs.com/ydymz/p/14225650.html
Copyright © 2011-2022 走看看