如果文章对你有帮助欢迎【关注❤️❤️❤️点赞👍👍👍收藏⭐⭐⭐】一键三连!一起努力!
各位家人,本次我们介绍一下SpringBoot + Maven 的项目在不同的环境下的配置,以及不同环境下的打包。
做为开发,大家在日常的工作当中肯定都有环境的切换操作,这些也都是我们开发的一些常规操作了,毕竟在开发,测试,生产的时候大概率用的环境都不一样(包括数据库,redis等等)。
多的不说,直接扔几个yml配置文件,想必大家都眼熟
这三个文件中:
但是请大家注意:
后面这两个文件在SpringBoot服务启动时,Spring并不会自动加载他们,想让他们加载还需要我们在application.yml 中添加一些配置如下所示。
application.yml:
spring:
profiles:
# active: @activatedEnv@
# 此时加载dev开发环境
active: dev
# 此时加载pro生产环境
# active: pro
对应关系如下图所示:
在服务启动加载的时候,服务器就会加载
application.yml
文件,然后通过配置去调用application-dev.yml
文件,选择开发环境。当然如此,当active:prod,那么服务在启动时,Spring就会调用application-prod.yml
文件进入生产环境。
上面我们已经介绍了各个环境直接的切换,但是想要完成多环境的打包还需要再pom中添加对应的依赖
pom.xml:
<profiles>
<profile>
<id>devid>
<properties>
<activatedEnv>devactivatedEnv>
properties>
<activation>
<activeByDefault>trueactiveByDefault>
activation>
profile>
<profile>
<id>proid>
<properties>
<activatedEnv>proactivatedEnv>
properties>
profile>
profiles>
<build>
<finalName>Jesus-${activatedEnv}finalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
exclude>
excludes>
configuration>
plugin>
<plugin>
<groupId>org.apache.maven.pluginsgroupId>
<artifactId>maven-resources-pluginartifactId>
<executions>
<execution>
<id>test-resourcesid>
<phase>compilephase>
<goals>
<goal>testResourcesgoal>
goals>
<configuration>
<overwrite>trueoverwrite>
<outputDirectory>${project.build.outputDirectory}outputDirectory>
<resources>
<resource>
<directory>src/main/resources/${activatedEnv}directory>
<filtering>falsefiltering>
resource>
resources>
configuration>
<inherited>falseinherited>
execution>
executions>
plugin>
plugins>
build>
大家可以根据需要去修改上面的配置信息。
然后在我们的 application.yml 配置动态的使用即可:active: @activatedEnv@
这里的 @activatedEnv@
,使用的就是我们在pom.xml里设置的
最后在Maven打包时,我们只需要指定参数就好了
// 开发
mvn clean package -P dev
// 生产
mvn clean package -P prod
如果你的项目要打很多很多的包,同时又在多个地方使用,每个地方环境都不一样,改怎么办呢? 这样一个个的搞岂不是要了老命。
这里只提供一个思路,有需要的家人可以看看,并不是最好的解决方案,不喜勿喷!
1、在你的项目中新建一个文件夹config,用来保存你的配置文件
2、修改pom文件,在里面添加你需要的环境配置,我这里是根据不同的城市来切换环境
3、同样的编写你需要的build
4、指定你配置文件的路径,让maven打包的时候去对应的地方拿对应的配置文件:
5、编写一大堆脚本文件,用于打包
6、再编写一个脚本,去运行这些脚本,并且把打好的jar包拿到指定地点
MvnBuildAll-EapStdAll-js-test.bat:
@echo off
chcp 936
:INPUT
echo ===================命令列表=======================
echo 命令编号: 1 语意:执行打包文件本机仓库安装 -install
echo 命令编号: 9 语意:退出控制台 -quit
echo ===================命令列表=======================
echo 请输入你要选择的命令:
set /p cmd=
IF %cmd% == 1 GOTO INSTALL
IF %cmd% == 9 GOTO END
:INSTALL
echo ===================mvnbulid-gcc.bat=======================
call D:
call cd D:\workspace\code\eap-std\eap-std-webapp\buildzjtest
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-gcc.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-gcc-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-baseservice.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-baseservice.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-baseservice-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-platform.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-platform.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-platform-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-manage.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-manage.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-manage-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-tcservice.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-tcservice.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-tcservice-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-scm.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-scm.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-scm-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-cas.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-cas.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-cas-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-userconsole.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-userconsole.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-userconsole-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-analysis.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-analysis.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-analysis-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-mqlis.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-mqlis.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-mqlis-js-test.war D:\workspace\maven_jar\zjtest
echo ============================ next continue ============================
echo ===================mvnbulid-dep.bat=======================
call D:\workspace\code\eap-std\eap-std-webapp\buildzjtest\mvnbulid-dep.bat
echo ===================copy war to build-new-war =======================
call copy D:\workspace\code\eap-std\eap-std-webapp\target\eap-std-dep-js-test.war D:\workspace\maven_jar\zjtest
echo ===================执行完毕=======================
:END
echo ===================按任意键退出=======================
@pause
最后你只需要运行这个bat文件等着就行了。
如果你们是用Jenkins 等自动化部署,请一定要忽略这古老的方式。切记!切记!!切记!!!
感谢各位家人的观看喜欢的话点帮忙点赞👍👍👍哦