你还在手动修改SpringBoot配置环境!点进来吧!不后悔的!
「这是我参与11月更文挑战的第24天,活动详情查看:2021最后一次更文挑战」
痛点在哪里
-
对于刚开始接触Springboot项目的新人,每次使用不同环境都是
先找到application.properties文件,找到里面的spring.profiles.active=位置,再改为不同的环境。 -
如果配置文件够长,找也需要时间,可能你会说我用搜索,我把它放到首行位置等。但这也带来了其他额外的动作:如找application.properties文件,再搜索输入查找等。
-
次数少可能觉得还行,不麻烦;但是次数多了,感觉这就是在浪费我摸鱼的时间啊。哪我们能不能把切换环境步骤减少,让它习惯化,位置固定化呢?
怎么解决
1. 配置pom文件
1.1. 配置环境profiles环境
配置环境profiles环境有哪些,与application-x.properties后面的x对应即可
<!-- profile配置 -->
<profiles>
<profile>
<!-- 本地开发环境 -->
<id>dev</id>
<!-- 定义该配置的属性 -->
<properties>
<profiles.active>dev</profiles.active>
</properties>
<activation>
<!-- 设置默认激活这个配置 -->
<activeByDefault>true</activeByDefault>
</activation>
</profile>
<profile>
<!-- 生产环境 -->
<id>pro</id>
<!-- 定义该配置的属性 -->
<properties>
<profiles.active>pro</profiles.active>
</properties>
<activation>
<activeByDefault>false</activeByDefault>
</activation>
</profile>
</profiles>
1.2. 配置需要打包哪些文件:resources标签
配置看1.3
1.3. 配置maven插件:plugins标签
<build>
<!-- 打包后jar包名 -->
<finalName>${project.artifactId}</finalName>
<resources>
<resource>
<!-- 配置部署时打包的配置文件 -->
<filtering>true</filtering>
<directory>src/main/resources</directory>
<!--部署时排除这些环境文件 -->
<excludes>
<!--<exclude>application-dev.yml</exclude>-->
<!--<exclude>application-pro.yml</exclude>-->
<!--<exclude>application-test.yml</exclude>-->
</excludes>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
2. application文件
2.1. application.properties
在application.properties主文件中配置
spring.profiles.active=@profiles.active@
低版本的springboot可能要加单引号,如
server.port=8080
spring.profiles.active='@profiles.active@'
2.2. application-dev.properties
server.port=7777
2.3. application-pro.properties
server.port=6666
idea测试
检查idea配置的maven路径和系统环境变量配置的maven是否一致,不是idea自带的。
- 使用默认的dev环境
2. 切换到pro环境
先勾选环境,再点击刷新按钮刷新maven环境
- 不使用子配置
打包
方法1:gui打包
注:
- 选择环境时点击刷新按钮刷新maven环境配置
- 不点击跳过测试按钮,打包时会运行单元测试选择环境时请刷新环境配置
方法2:命令行打包
- PowerShell(或idea的Terminal或Git bash Here):运行命令
mvn clean package '-Ppro' '-Dmaven.test.skip=true'
- 使用PowerShell的参数有
单引号,否则报错 - '-Ppro' :使用pro环境
- '-Dmaven.test.skip=true' : 跳过测试
- 普通的cmd(或Git bash Here): 运行命令
mvn clean package -Ppro -Dmaven.test.skip=true
- 参数无单引号,否则报错
打包跳过测试
方法1. -Dmaven.test.skip=true
命令行增加 -Dmaven.test.skip=true参数
或 :mvn clean install -Pdev -DskipTests
方法2. pom配置跳过测试
<!-- 打包跳过测试 mvn命令可以不用加跳过测试了,这里配置-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<skipTests>true</skipTests>
</configuration>
</plugin>
打包避免二进制文件格式压缩破坏
<!-- 避免font文件的二进制文件格式压缩破坏 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<configuration>
<encoding>UTF-8</encoding>
<nonFilteredFileExtensions>
<!-- 需要过滤掉不需要编码的文件:过滤后缀为.xlsx或者.xls的所有文件,不对其进行统一编码-->
<nonFilteredFileExtension>woff</nonFilteredFileExtension>
<nonFilteredFileExtension>woff2</nonFilteredFileExtension>
<nonFilteredFileExtension>eot</nonFilteredFileExtension>
<nonFilteredFileExtension>ttf</nonFilteredFileExtension>
<nonFilteredFileExtension>svg</nonFilteredFileExtension>
<nonFilteredFileExtension>xlsx</nonFilteredFileExtension>
<nonFilteredFileExtension>xls</nonFilteredFileExtension>
</nonFilteredFileExtensions>
</configuration>
</plugin>
我的环境版本
| 环境 | 版本 |
|---|---|
| jdk | 1.8.0_202 |
| maven | 3.8.3 |
| springboot | 2.5.6 |
| 系统 | win10 64位 |
| idea | 2021.2.3 |