Spring Boot多环境打包

80 阅读2分钟

例如这里有两个文件

application.yml

spring:  
  profiles:  
    active: @env@

application-dev.yml

# dev的相关配置

application-prod.yml

# prod的相关配置

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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<!-- 需要增加两个地方的配置 -->

<!-- 1.在根标签project下增加profiles配置 -->
<!-- 多环境打包 -->
    <profiles>
        <!-- 本地开发环境 -->
        <profile>
            <id>dev</id>
            <properties>
                <spring.profiles.active>dev</spring.profiles.active>
            </properties>
            <!-- 是否默认启用 -->
            <activation>
                <activeByDefault>true</activeByDefault>
            </activation>
        </profile>
        <!-- 生产环境 -->
        <profile>
            <id>prod</id>
            <properties>
                <spring.profiles.active>prod</spring.profiles.active>
            </properties>
        </profile>
    </profiles>
    <!-- 1.在根标签project下的build中增加resources配置,具体如下-->
    <build>
        <!-- 加上这个才能实现多环境打包 -->
        <resources>
            <resource>
                <directory>src/main/resources</directory>
                <filtering>true</filtering> <!-- 启用变量替换 -->
            </resource>
        </resources>
    </build>
</project>

然后记得刷一下maven依赖

怎么打包:

maven命令

-Pdev中的dev替换成实际的环境就行了

# 跳过测试编译和执行:
mvn clean package -Pdev -Dmaven.test.skip=true
# 只跳过测试执行,但仍然编译测试:
mvn clean package -Pdev  -DskipTests

如果报这种错:

[INFO] BUILD FAILURE [INFO]
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.3.1:
resources (default-resources) on project micro-fire-station: filtering
\src\main\resources\static\xxxx/xxxx.xxxx \src\main\resources\static\xxxx/xxxx.xxxx 
failed with MalformedInputException: Input length = 1 -> 
[Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging. 
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles: 
[ERROR] 
[Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

可能需要修改pom build中的配置

<build>   
    <!-- 加上这个才能实现多环境打包 -->  
    <resources>  
        <resource>  
            <directory>src/main/resources</directory>  
            <filtering>true</filtering> <!-- 启用变量替换 -->  
            <excludes>  
                <exclude>static/**</exclude> <!-- 排除 static 目录 -->  
            </excludes>  
        </resource>  
        <resource>  
            <directory>src/main/resources/static</directory>  
            <filtering>false</filtering> <!-- 禁止 static 目录资源过滤 -->  
        </resource>  
    </resources>  
</build>

这样写在IDEA中可能运行不了,就在启动启动类运行的名字上面

点击下拉箭头编辑配置

image.png

点击修改选项

image.png

然后勾选添加VM选项

image.png

然后添加-Dspring.profiles.active=dev,再点确定保存好就能正常运行了

image.png

IDEA 一键打包:

新增一个mvn打包命令即可

image.png

在运行一栏填写命令clean package -Pprod -Dmaven.test.skip=true,不需要加mvn命令前缀,IDEA运行的时候自己调用了mvn的

image.png