1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > pom通过profile设置打包运行环境

pom通过profile设置打包运行环境

时间:2021-05-04 09:10:38

相关推荐

pom通过profile设置打包运行环境

1.简介

spring boot项目在开发过程中,会涉及到开发、测试、线上的部署,而不同的部署环境需要加载不同的配置文件,此时可以使用profile对打包运行环境进行配置。

2.使用方式

在pom.xml中配置profile属性,先来看一个完整的配置样例:

<!--分别设置开发,本地,生产环境--><profiles><!-- 本地环境 --><profile><!--定义id与maven打包时候的参数对应--><id>local</id><activation><!--默认激活,true:激活,false:不激活--><activeByDefault>true</activeByDefault></activation><!--配置变量,在property或者yml中使用@xxx@进行引用--><properties><!--配置变量名及变量值,变量名可以任意定义--><environment>local</environment></properties></profile><!-- 用户体验环境 --><profile><id>dev</id><activation><activeByDefault>false</activeByDefault></activation><properties><environment>dev</environment></properties></profile><!-- 生产环境 --><profile><id>prod</id><activation><activeByDefault>false</activeByDefault></activation><properties><environment>prod</environment></properties></profile></profiles>

可以在profiles里面配置多个profile,每个profile有一个唯一的id值,使用activeByDefault配置默认激活的profile。在maven打包的时候,执行的打包命令:mvn clean package -P dev中的-P后的参数就是指定选中哪个profile,此参数对应于profile中的id值。

配置好profile后,在idea工具中,刷新maven,会出现profile的选项,选择哪一个,idea打包的时候激活的就是对应的配置。

我们可以根据激活的profile,获取到profile里面配置的属性,例如此处就是使用激活的profile获取到spring.profiles.active的值,当激活的profile为local时,spring boot激活的配置文件为application-local.yml。

3.打包方式

maven中打包,我们可以使用命令:

###激活的profile是devmvn clean package -P dev

在使用jenkins发布系统时,我们可以使用命令:

##-Pdev即指定激活的profile为dev,--pl只打包xxx目录下的aaa,有可能xxx目录下存在多个项目,am:also-make表示同时处理选定模块所依赖的模块clean package -Dmaven.test.skip=true -Pdev --pl xxx/aaa -am

在maven构建的时候,我们可以使用resources属性配置打包需要的文件以及排除的文件,在pom.xml中配置:

<build><resources><resource><!-- 指定配置文件所在的resource目录 --><directory>src/main/resources</directory><!--打包包含的文件--><includes><include>application.yml</include><include>application-${environment}.yml</include><include>logback-xxx.xml</include><include>mchange-xxx.properties</include></includes><!--打包需要排除的文件--><excludes><exclude>test/*</exclude></excludes><filtering>true</filtering></resource></resources></build>

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。