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:/

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM