zoukankan      html  css  js  c++  java
  • SpringBoot项目多环境配置

    SpringBoot 项目多环境配置

     

    方法一:使用spring-boot-maven-plugin插件

       优点:简单

       缺点:把所有的libs都打包进去,整个jar包太大。上传到服务器非常慢。

      

    <plugins>
           <plugin>
                 <groupId>org.springframework.boot</groupId>
                 <artifactId>spring-boot-maven-plugin</artifactId>
                 <version>${springboot.version}</version>
                 <configuration>
                     <mainClass>${mainClass}</mainClass>
                     <layout>jar</layout>
                 </configuration>
                 <executions>
                     <execution>
                         <goals>
                            <goal>repackage</goal>
                         </goals>
                     </execution>
                 </executions>
            </plugin>
    </plugins>

    方法二:使用maven-dependency-plugin 和 maven-jar-plugin

      优点:

      1.把libs和myApp.jar 分开了,部署的时候只部署myApp.jar即可

      2.打包后的样式效果和eclipse的export导出可执行jar打出的文件名称和jar包是一样的

       myApp.jar

       myApp_lib

      

    <build>
            <finalName>${build.jar.name}</finalName>
            <resources>
                <resource>
                    <directory>src/main/resources</directory>
                    <!--Filtering 是 maven 的 resource 插件 提供的功能,作用是用环境变量、pom文件里定义的属性和指定配置文件里的属性替换属性(*.properties)文件里的占位符(${jdbc.url}) -->
                    <filtering>true</filtering>
                    <includes>
                        <include>*.yml</include>
                        <include>*.properties</include>
                        <include>mapper/**/*.xml</include>
                        <include>static/**</include>
                        <include>templates/**</include>
                        <include>*.xml</include>
                    </includes>
                </resource>
    
                <resource>
                    <directory>src/main/java</directory>
                    <includes>
                        <include>**/*.yml</include>
                        <include>**/*.properties</include>
                        <include>**/*.xml</include>
                    </includes>
                    <filtering>false</filtering>
                </resource>
    
            </resources>
    
            <plugins>
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <configuration>
                        <source>1.8</source>
                        <target>1.8</target>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok.version}</version>
                            </path>
                            <path>
                                <groupId>org.mapstruct</groupId>
                                <artifactId>mapstruct-processor</artifactId>
                                <version>${org.mapstruct.version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
                <!--  maven-dependency-plugin 复制项目的依赖包到指定目录 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-dependency-plugin</artifactId>
                    <executions>
                        <execution>
                            <phase>prepare-package</phase>
                            <goals>
                                <goal>copy-dependencies</goal>
                            </goals>
                            <configuration>
                                <outputDirectory>target/${build.jar.name}_lib</outputDirectory>
                                <overWriteReleases>false</overWriteReleases>
                                <overWriteSnapshots>false</overWriteSnapshots>
                                <overWriteIfNewer>true</overWriteIfNewer>
                                <includeScope>compile</includeScope>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
                <!--  maven-jar-plugin jar包的文件 -->
                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-jar-plugin</artifactId>
                    <configuration>
                        <archive>
                            <manifest>
                                <!-- 项目启动类 -->
                                <mainClass>study.app.SpringApplication</mainClass>
                                <!-- 依赖的jar的目录前缀,这个名称是为了和eclipse打包统一 -->
                                <classpathPrefix>${build.jar.name}_lib</classpathPrefix>
                                <addClasspath>true</addClasspath>
                            </manifest>
                            <!-- 增加当前目录,在META-INF/MANIFEST.MF中的Class-Path前面会多一个点.表示当前目录
                               比如:Class-Path: . prevention_lib/spring-boot-starter-2.0.4.RELEASE.jar
                              增加这个是为了解决以下这种配置方式,jar包外的application.properties无效的问题。
                                 -config/application.properties
                                 -myApplication.jar
                            -->
                            <manifestEntries>
                                <Class-Path>.</Class-Path>
                            </manifestEntries>
                        </archive>
    
                    </configuration>
                </plugin>
    
            </plugins>
        </build>
        

    说明:

       1. 生产的最终目录是这样的:

           -myApplication_lib

           -config/application.properties

           -myApplication.jar

          (1)与环境相关的配置文件都先放在服务器的config目下(与环境无关的公共配置仍然在jar中),而不需要根据profile依赖打包来上传。

         因为配置是不会经常变的,所以没必要每次打包时还要考虑环境。

          (2)myApplication_lib 目录下是jar包

        2.maven-jar-plugin 插件打包后,其实是不能识别myApplication.jar外的config/application.properties

        解决方法一:启动时通过spring.config.additional-location指定,优先级最高。

            java -jar myproject.jar --spring.config.additional-location=classpath:/default.properties,classpath:/override.properties

          解决方法二:maven-jar-plugin 增加配置

    <!-- 增加当前目录,在META-INF/MANIFEST.MF中的Class-Path前面会多一个点.表示当前目录
          比如:Class-Path: . prevention_lib/spring-boot-starter-2.0.4.RELEASE.jar
          增加这个是为了解决以下这种配置方式,jar包外的config/application.properties无效的问题 -->
    <manifestEntries>
         <Class-Path>.</Class-Path>
    </manifestEntries>

      3.Spring Boot加载配置文件顺序?

      https://docs.spring.io/spring-boot/docs/2.0.4.RELEASE/reference/html/boot-features-external-config.html#boot-features-external-config-profile-specific-properties

      SpringBoot配置文件加载优先级(数字小的优先:springBoot的处理是已存在就不加载):

    1. Devtools global settings properties on your home directory (~/.spring-boot-devtools.properties when devtools is active).
    2. @TestPropertySource annotations on your tests.
    3. @SpringBootTest#properties annotation attribute on your tests.
    4. Command line arguments.
    5. Properties from SPRING_APPLICATION_JSON (inline JSON embedded in an environment variable or system property).
    6. ServletConfig init parameters.
    7. ServletContext init parameters.
    8. JNDI attributes from java:comp/env.
    9. Java System properties (System.getProperties()).
    10. OS environment variables.
    11. RandomValuePropertySource that has properties only in random.*.
    12. Profile-specific application properties outside of your packaged jar (application-{profile}.properties and YAML variants).
    13. Profile-specific application properties packaged inside your jar (application-{profile}.properties and YAML variants).
    14. Application properties outside of your packaged jar (application.properties and YAML variants).
    15. Application properties packaged inside your jar (application.properties and YAML variants).
    16. @PropertySource annotations on your @Configuration classes.
    17. Default properties (specified by setting SpringApplication.setDefaultProperties).

      搜索配置文件时的顺序:

    1. file:./custom-config/    表示java启动时spring.config.additional-location指定才存在
    2. classpath:custom-config/  表示java启动时spring.config.additional-location指定才存在
    3. file:./config/
    4. file:./
    5. classpath:/config/
    6. classpath:/
  • 相关阅读:
    Android中Activity和Fragment之间的通信
    用Android布局和简单控件实现的一个常见的界面
    【Linux】linux常用基本命令 [轉載]
    本地项目上传到gitlab
    spring boot 配置redis
    linux mysql的安装
    linux 安装 java环境变量
    mvc 跨域
    JSON 的几种简单格式和转换
    地区三级联动和全盘扫描索引的建立
  • 原文地址:https://www.cnblogs.com/caoshouling/p/14010657.html
Copyright © 2011-2022 走看看