SpringBoot配置多环境

通过yml文件

server:
  port: 8080
my:
  name: demo
# 激活文件
spring:
  profiles:
    active: test

---
# dev
spring:
  profiles: dev
server:
  port: 8081
my:
  name: Durant

---
# test
spring:
  profiles: test
server:
  port: 8082
my:
  name: Irving

---
# prod
spring:
  profiles: prod
server:
  port: 8083
my:
  name: Luka

如果没有指定spring.profiles.active 属性配置,会走通用配置也就是上述当中的"demo"

通过Maven的pom文件

pom文件

<properties>
        <java.version>1.8java.version>
    properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
        dependency>

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
    dependencies>

    <profiles>
        <profile>
            
            <id>devid>
            
            
            <properties>
                <env.start>devenv.start>
            properties>
            <activation>
                
                <activeByDefault>trueactiveByDefault>
            activation>
        profile>
        <profile>
            
            <id>testid>
            <properties>
                <env.start>testenv.start>
            properties>
        profile>
        <profile>
            
            <id>prodid>
            <properties>
                <env.start>prodenv.start>
            properties>
        profile>
    profiles>

    <build>
        
        <finalName>${project.artifactId}finalName>
        
        <resources>
            <resource>
                <directory>src/main/resourcesdirectory>
                
                <filtering>truefiltering>
                <includes>
                    <include>application-${env.start}.ymlinclude>
                    <include>application.ymlinclude>
                includes>
            resource>
        resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
            <plugin>
                <groupId>org.apache.maven.pluginsgroupId>
                <artifactId>maven-resources-pluginartifactId>
                <version>3.0.2version>
                <configuration>
                    <encoding>UTF-8encoding>
                    <useDefaultDelimiters>falseuseDefaultDelimiters>
                    <delimiters>
                        <delimiter>${*}delimiter> 
                        <delimiter>@delimiter>    
                    delimiters>
                configuration>
            plugin>
        plugins>
    build>

在src/main/resoucrces/下面创建三个文件

# application-dev.yml
my:
  name: Durant
# application-test.yml
my:
  name: Irving
# application-prod.yml
my:
  name: Luka

applicaion.yml

#配置项目启动的决定使用哪个开发环境 @可以加载到pom中的配置
spring:
  profiles:
    active: @env.start@

打包命令:

mvn clean package -P {配置的profileId}

你可能感兴趣的:(maven,spring,boot,java,spring)