Springboot使用Maven Profile和Spring Profile进行多环境配置

 

目的

在实际的项目上,一般会分三种环境dev、test、prod来方便我们的开发和部署,要求我们在开发的时候可以方便地进行环境的切换,又要满足在发布版本的时候可以尽可能减少测试人员的配置。

Spring Profile

多环境

为了实现多环境配置,我们可以在本地开发的时候在Resource文件夹下新建不同环境的配置文件,如下图所示:

 

这时候我们只需要在主配置文件application.yml文件使用spring.profiles.active = dev/test/prod来进行不同环境的切换

 

spring:
  profiles:
    active: dev #加载dev配置文件

经过上述步骤,我们即可在本地开发时根据不同的环境进行测试。

主配置文件与不同环境的配置文件

Spring Profile有一个特性即可以在application.yml配置通用的内容,在application-dev.yml文件中配置不同文件的差异化配置,这样可以精简我们的配置文件。配置如下:

 

# 主配置文件主要用来存放公共设置,不受环境配置影响
server:
  port: 8082 # 指定端口号
  context-path: /server # 指定访问资源的根路径
spring:
  profiles:
    active: dev 

dev配置文件

 

# 主要用来存放不同环境差异化配置,包括ftp地址、数据库连接等
spring:
  datasource:
    username: xxx
    password: xxx
    url: xxx
    driver-class-name: oracle.jdbc.driver.OracleDriver

上述解决方案虽然可以解决我们的环境问题,但是不够优雅,还有一些值得优化的地方,比如打包出的配置文件有四个、每次需要手动修改主配置文件的环境信息等。


IDEA上运行的时候配置

idea上运行的时候,下图中的active profiles填写选择生效的配置文件,例如prod。

Spring 中为我们提供了 Profile 这个功能。我们只需要在启动的时候添加一个虚拟机参数,激活自己环境所要用的 Profile 就可以了。操作起来很简单,只需要为不同的环境编写专门的配置文件,如:application-dev.ymlapplication-prod.yml, 启动项目时只需要增加一个命令参数 --spring.profiles.active=环境配置 即可,启动命令如下:

java -jar hello-spring-cloud-alibaba-nacos-provider-1.0.0-SNAPSHOT.jar --spring.profiles.active=prod

 

Maven Profile

Profile配置

Maven 也提供了 Profile 支持,它允许我们在 pom.xml 中定义多个 Profile ,每个 profile 可以指定自己的一些配置、依赖、触发条件等。例如:

 

按照上述的配置,我们配置了dev/test/prod三种配置并默认选择dev环境。

资源过滤

在进行打包的时候,我们并不需要把dev或者test等配置文件打包进行,具体什么需要过滤,什么需要保留,按需配置。

 

IDEA上运行的时候配置

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用Spring Boot Maven插件可以很方便地根据不同环境使用不同配置文件来打包WAR文件。 首先,在项目的资源目录下创建多个不同环境配置文件,如application-dev.properties、application-prod.properties、application-test.properties等。 然后,在pom.xml文件中,为每个环境配置一个profile,指定相应的配置文件。 ``` <profiles> <profile> <id>dev</id> <activation> <activeByDefault>true</activeByDefault> </activation> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-dev.properties</include> </includes> </resource> </resources> </build> </profile> <profile> <id>prod</id> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-prod.properties</include> </includes> </resource> </resources> </build> </profile> <profile> <id>test</id> <build> <resources> <resource> <directory>src/main/resources</directory> <includes> <include>application-test.properties</include> </includes> </resource> </resources> </build> </profile> </profiles> ``` 接下来,在插件配置中,使用resource元素来指定每个环境配置文件。 ``` <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <executable>true</executable> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-assembly-plugin</artifactId> <executions> <execution> <id>make-assembly</id> <phase>package</phase> <goals> <goal>single</goal> </goals> <configuration> <finalName>my-project-${env}</finalName> <appendAssemblyId>false</appendAssemblyId> <descriptors> <descriptor>src/main/assembly/assembly.xml</descriptor> </descriptors> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 在上面的配置中,使用了一个单独的assembly.xml文件来定义如何打包WAR文件。在assembly.xml中,可以根据不同环境配置不同配置文件。 ``` <assembly xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/ASSEMBLY/2.0.0" xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd"> <id>war</id> <formats> <format>war</format> </formats> <fileSets> <fileSet> <directory>${project.basedir}/src/main/resources</directory> <outputDirectory>/</outputDirectory> <includes> <include>application*.properties</include> </includes> </fileSet> </fileSets> </assembly> ``` 最后,使用命令mvn -P dev package可以选择不同profile进行打包,生成的WAR文件会根据不同环境使用相应的配置文件。例如,使用mvn -P dev package会使用application-dev.properties配置文件进行打包,生成的WAR文件名为my-project-dev.war。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值