您的位置:首页技术文章
文章详情页

Springboot与Maven多环境配置的解决方案

浏览:10日期:2023-03-01 14:56:57
目录Profile用法resourcesfilters多环境配置解决方案Profile用法

我们在application.yml中为jdbc.username赋予一个值,这个值为一个变量

jdbc: username: ${jdbc.username}

Maven中的profiles可以设置多个环境,当我们选择a环境后,<jdbc.username>内的值将替换上述配置文件中的变量

</profiles><profile> <id>a</id> <properties><jdbc.username>root</jdbc.username> </properties> <!-- 默认使用此环境 --> <activation><activeByDefault>true</activeByDefault> </activation></profile> </profiles>

我们查看编译后的application.yml文件,果然变量已经被赋值。我们猜想是否可以利用Profile的这一特性设置开发、测试、生产环境,选择不同环境时使用不同变量,配合Resources和Filter来指定打包内容以及替换变量。

jdbc: username: rootresources

用来操作编译文件

filters

过滤器,设置过滤器的资源将会对同名变量进行赋值(被赋值的资源文件需要设置filtering为true)

多环境配置解决方案

网上大多数都是分为application-dev.xml、application-test.xml、application-prod.xml三个文件,可是我们在真实项目开发中,将会用到很多各式各样的文件(例如log4j的配置文件),它们在不同环境中应该也是不同的配置,不能在测试和生产环境使用同一个配置文件。所以我们将分为三个文件夹分别代表开发环境、测试环境、生产环境,他们里面的配置文件种类一致但是内容不一样。选择完当前环境后,打的jar包只包含当前环境文件夹下的配置文件。

├─main│ ├─java│ │ └─......│ └─resources│ ├─dev│ │ └─config│ │ │ └─mq.yml│ │ │ └─redis.yml│ │ └─application-dev.yml│ ├─prod│ │ └─config│ │ │ └─mq.yml│ │ │ └─redis.yml│ │ └─application-prod.yml│ └─test│ │ └─config│ │ │ └─mq.yml│ │ │ └─redis.yml│ │ └─application-test.yml│ └─application.yml│ └─a.xml└─test └─java└─......

dev下的config下的mq.yml

mq: mq-dev

dev下的config下的redis.yml

redis: redis-dev

dev下的application-dev.yml

profiles.active: devport: dev-portapplication.yml

spring: profiles: active: ${profiles.active} port: ${port}

查看编译后的结果

Springboot与Maven多环境配置的解决方案

其中application.yml中变量已经被替换为

spring: profiles: active: devport: dev-port

完整的pom.xml

<build><plugins> <plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId> </plugin> <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-resources-plugin</artifactId><version>3.1.0</version><!--使用默认的变量分割符即${}--><configuration> <useDefaultDelimiters>true</useDefaultDelimiters></configuration> </plugin></plugins><!-- 测试文件的编译路径设置 --><testResources> <testResource><!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包--><directory>src/main/resources</directory><includes> <include>application.yml</include></includes><filtering>true</filtering> </testResource> <testResource><!--这里是关键! 根据不同的环境,把对应文件夹里的配置文件打包--><directory>src/main/resources/${profiles.active}</directory><includes> <include>**/*.yml</include></includes><filtering>false</filtering> </testResource></testResources><resources> <resource><!--打包该目录下的 application.yml --><directory>src/main/resources</directory><includes> <include>application.yml</include></includes><!-- 启用过滤 即该资源中的变量将会被过滤器中的值替换 --><filtering>true</filtering> </resource> <resource><directory>src/main/resources</directory><includes> <include>**/*.properties</include> <include>**/*.xml</include></includes><filtering>false</filtering> </resource> <resource><!-- ${profiles.active}由profile提供 --><directory>src/main/resources/${profiles.active}</directory><includes> <include>**/*.yml</include></includes><filtering>false</filtering> </resource></resources><!-- 定义 filter,即该资源中的值将会用来替换同名属性(设置 filtering 为 true 的资源中的属性)--><filters> <filter> src/main/resources/${profiles.active}/application-${profiles.active}.yml </filter></filters> </build> <profiles><profile> <!-- 本地开发环境 --> <id>dev</id> <properties><profiles.active>dev</profiles.active> </properties> <activation><activeByDefault>true</activeByDefault> </activation></profile><profile> <!-- 测试环境 --> <id>test</id> <properties><profiles.active>test</profiles.active> </properties></profile><profile> <!-- 生产环境 --> <id>prod</id> <properties><profiles.active>prod</profiles.active> </properties></profile> </profiles>

到此这篇关于Springboot与Maven多环境配置的解决方案的文章就介绍到这了,更多相关Springboot Maven多环境配置内容请搜索好吧啦网以前的文章或继续浏览下面的相关文章希望大家以后多多支持好吧啦网!

标签: Spring
相关文章: