springboot maven多环境打包配置

resources下的文件:application.yml、application-dev.yml、application-prd.yml和application-test.yml。
通过配置application.yml下spring:profiles:active:的值来使用不同的配置文件。
application.yml如下:

spring:
  profiles: #此处为占位符,maven打包时会自动替换为实际使用的配置文件
    active: #spring.profiles.active#
  cache:
    type: redis
pagehelper:
  supportMethodsArguments: true
  reasonable: true
  helperDialect: mysql
  params: count=countSql
mybatis:
  type-aliases-package: com.tianren.bean
  mapper-locations: classpath:mapper/*.xml
mapper:
  identity: MYSQL

上面用了#作为占位符,因为$在maven中已经被定义为取变量的符号了。
再看maven中的配置:


  
    
    
      dev
      
        dev
      
      
        true
      
    
    
    
      prd
      
        prd
      
    
    
    
      test
      
        test
      
    
  

  
    
      
      
        org.springframework.boot
        spring-boot-maven-plugin
      
      
      
        org.apache.maven.plugins
        maven-resources-plugin
        
          
            default-resources
            validate
            
              copy-resources
            
            
              target/classes
              false
              
                #
              
              
                
                  src/main/resources/
                  true
                  
                    **/*.yml
                  
                
                
                  src/main/resources/
                  false
                  
                    **/*.yml
                  
                
              
            
          
        
      
      
      
        org.apache.maven.plugins
        maven-resources-plugin
        
          
            org.apache.maven.shared
            maven-filtering
            3.1.1
          
        
      
    
  

上面首先定义了一组profiles。注意properties里面的标签名应该与application.yml中占位符之间的内容一样!
delimiter定义了一个占位符。
resources里面定义了需要过滤的文件已经不需要过滤的文件。像上面那样配置,maven打包时会检查resources下面所有以.yml结尾的文件,遇到#spring.profiles.active#则替换为实际的值。
这个值若在打包命令中不指定则以profiles中activeByDefault为true的为准。
若要在打包命令中指定,可以这样:

mvn clean package -P prd -Dmaven.test.skip=true;

-P 后面的参数就是要使用的配置名称。
后面的参数-Dmaven.test.skip=true也必须要有,不然运行maven测试会报错。

你可能感兴趣的:(maven)