spring-boot之Maven多环境配置

发布时间 2024-01-09 11:28:16作者: Jackpot_ABC

spring-boot之Maven多环境配置

项目默认只会打包classpath下的资源文件

1、在src/main/resource/config/zbj-fwApp下添加相关环境配置文件

· application.yml

· application-dev.yml

· application-test.yml

· application-pro.yml

application.yml 激活相关环境配置文件


spring:
  profiles:
    active: @enviroment@

2、配置maven

在pom.xml文件中添加以下部分

	<!-- 设置不同环境,这里设置了开发环境,测试环境,正式环境 -->
	<profiles>
        <profile>
            <!-- 配置文件标识id,命令行用到,如mvn package -P dev -->
            <id>dev</id>
            <!-- 设置变量 -->
            <properties>
                <enviroment>dev</enviroment>
            </properties>
<!--            <activation>-->
<!--                <activeByDefault>true</activeByDefault>-->
<!--            </activation>-->
        </profile>
        <profile>
            <id>test</id>
            <properties>
                <enviroment>test</enviroment>
            </properties>
        </profile>
        <profile>
            <id>pro</id>
            <properties>
                <enviroment>pro</enviroment>
            </properties>
        </profile>
    </profiles>
	<!-- 描述如何编译和打包项目 -->
    <build>
        <!-- 设置打包之后的jar包名称 -->
        <finalName>zbj-fwapp-api-${enviroment}</finalName>
        <!-- maven打包插件 -->
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <includeSystemScope>true</includeSystemScope>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <!-- 资源文件相关插件,用于将资源文件复制到输出目录,在相关生命周期中调用 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <!-- 使用你需要的版本 -->
                <version>3.2.0</version> 
                <!-- 在这里添加你的配置 -->
                <configuration>
                    <!-- 资源文件的字符集编码 -->
                    <encoding>UTF-8</encoding>
<!--                    <delimiters>-->
<!--                        <delimiter>@</delimiter> &lt;!&ndash; 自定义定界符 &ndash;&gt;-->
<!--                    </delimiters>-->
                    <useDefaultDelimiters>false</useDefaultDelimiters>
                </configuration>
            </plugin>
        </plugins>
        <!-- maven-resources-plugin的配置 -->
        <resources>
            <!-- 每个资源目录一个resource标签 -->
            <resource>
                <!-- 指定资源文件目录 -->
                <directory>src/main/resources/config/zbj-fwApp</directory>
                <!-- 哪些文件需要打包,不配置就是全部进行打包 -->
                <includes>
                    <include>application.yml</include>
                    <include>application-${enviroment}.yml</include>
                </includes>
                <!-- 是否需要进行变量替换,没有必要使用变量取值的,千万不要将filtering设置为true,针对于这一点官网也明确指出二进制文件一定不要过滤,例如Excel模板 -->
                <filtering>true</filtering>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>*.*</include>
                </includes>
                <!-- 哪些文件不需要打包 -->
                <excludes>
                    <exclude>application.yml</exclude>
                </excludes>
                <!-- 默认为false -->
                <filtering>false</filtering>
            </resource>
        </resources>
    </build>

3、执行命令 mvn clean package -P pro,使用正式环境配置文件打包

基于maven构建的项目,编译运行打包都会根据maven的配置来构建项目