spring-boot之Maven多环境配置
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> <!– 自定义定界符 –>-->
<!-- </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的配置来构建项目
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· winform 绘制太阳,地球,月球 运作规律