• Maven项目Profile最佳实践


     
    Maven项目Profile打包最佳实践
     
    一、 背景
        做项目的时候通常会有多个环境如:product、pre、test、dev,这个时候我们通常在项目中有5个配置文件,application.properties存放公共的配置,每个环境要自己有独自的配置文件application-{profile}.properties,
           问题:这么多配置文件,我们在运行的时候到底是用哪一个呢? 是在运行时区分还是在打包的时候区分开呢?
     
    二、Profile简介
        1、Maven中引入Profile,以便构建项目的时候动态激活某个Profile
             
        2、Spring中的Profile:程序启动时候根据主配置文件中指定的Profile,决定最终加载对应环境中的配置文件
    •  主配置文件指定spring.profiles.active,如果没有则加载默认的 application-default.properties
    • 环境相关的配置文件:application-{profile}.properties
    • 不同环境的配置文件指定不同的日志配置文件:logging. config: classpath:logback-pre.xml
    • springboot 打包指定的环境变量(spring.profiles.active=local)参数会用在2个地方:1)  加载对应环境的属性文件 2)变量会写到Envement中,然后映射到属性文件配置的参数上如: spring.profiles.active=${spring.profiles.active}
            
     
    三、打包最佳实践
    1.   文件拷贝-根据打包指定的profile来打包对应的配置文件-对应代码五中的1和2
    2. 变量占位符:根据mvn指定的profile来过滤属性文件中的环境变量,对应代码五中的3
     
        
    四、遇到的问题
          当对属性文件进行占位符过滤的时候,maven可以使用${}的形式进行对变量处理,但是如果spring-boot项目的话,因为spring-boot-starter-parent这里面指定了资源过滤的字符位@,所以变量替换需要是用@符号(${}这个符号被spring占用了)
     
     
    五、代码说明
     
    1)环境说明
     
    <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>
     
    1. maven资源打包过滤
              
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <excludes>
                <exclude>application.properties</exclude>
                <exclude>application-dev.properties</exclude>
                <exclude>application-test.properties</exclude>
                <exclude>application-product.properties</exclude>
            </excludes>
        </resource>
     
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
            <includes>
                <include>application.properties</include>
                <include>application-${profiles.active}.properties</include>
            </includes>
        </resource>
     
    </resources>
     
    3)属性文件指定环境
         spring.profiles.active=@profiles.active@
       
    4)打包插件
         
     
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>3.0.2</version>
        <configuration>
          <encoding>UTF-8</encoding>
          <overwrite>true</overwrite><!-- 目标文件存在时覆盖 -->
        </configuration>
      </plugin>
    </plugins>
     
    六、springboot启动指定端口的集中方式:
     
    • 第一配置文件中添加server.port=7020
    • 第二在命令行中指定启动端口,比如传入参数--server. port=6000     java -jar bootServer. jar -- server.port=9000
    • 第三传入虚拟机系统属性java - Dserver.port=8000 -jar bootsample.jar
    七、属性变量说明
    1. 属性文件application.properties会根据Environment中数值进行过滤
  • 相关阅读:
    为PHP开发C语言扩展
    为PHP开发C++扩展
    PHP 7 vs HHVM性能对比
    Real-time storage area network
    JS书写优化
    JS书写优化
    数据库 10 大常见安全问题盘点~
    数据库 10 大常见安全问题盘点~
    数据库 10 大常见安全问题盘点~
    SQL Server 数据库定时自动备份
  • 原文地址:https://www.cnblogs.com/lean-blog/p/13572919.html
Copyright © 2020-2023  润新知