springboot多環境下maven打包


前言:

最近在項目中使用springboot時發現,采用在pom中定義不同的profile,並且maven打包時

采用-P參數並不能替換我application.properties文件中指定占位符的問題。

配置文件布局:

在application.properties中定義整個項目中不同環境下共通的配置屬性,並采用springboot針對配置文件的特性 - spring.profiles.active=dev或者test

來自動加載合並application-dev/test.properties中特有的配置的功能,開發,測試,生產環境配置文件命名方面需要遵守springboot的約束即可。

Maven pom 配置:

 

備注:實際上使用springboot,上述的<resources>標簽,以及<outputDirectory>都是不需要的,其都已經定義過了。在這里寫出來只是為了講述maven打包的原理。

執行打包命令:

    進入項目pom文件所在目錄,執行

    mvn clean package -P test

    命令解釋:

            1:執行后會根據pom中定義的profiles標簽尋找對應的profile id為test的,將其properties下的子標簽獲取到,拿到key = boot.profile,value = test。放入map中

            2:根據 build中定義的resources標簽,開始打包對應的資源文件,由於指定了filtering為true,故會將上一步中得到的map中的key拿去替換resource中指定的資源文件中的占位符${key},為value值。

遇到的問題:

 

執行后查看classes文件下的資源文件,發現並沒有替換掉

原因:

    發現最終是因為springboot導致的,查看其pom繼承,

進入后發現,springboot默認指定資源文件中的占位符為@@,並不是maven默認的 ${}

 

解決方案:

    1:將application.properties中的占位符由${key} -> @key@ 

    2:覆蓋springboot的默認規則

 
 1     <build>
 2         <finalName>${project.artifactId}</finalName>
 3         <resources>
 4             <resource>
 5                 <filtering>true</filtering>
 6                 <directory>${basedir}/src/main/resources</directory>
 7                 <includes>
 8                     <include>**/application*.yml</include>
 9                     <include>**/application*.yaml</include>
10                     <include>**/*.properties</include>
11                 </includes>
12             </resource>
13             <resource>
14                 <directory>${basedir}/src/main/resources</directory>
15                 <excludes>
16                     <exclude>**/application*.yml</exclude>
17                     <exclude>**/application*.yaml</exclude>
18                     <exclude>**/*.properties</exclude>
19                 </excludes>
20             </resource>
21         </resources>
22         <plugins>
23             <plugin>
24                 <groupId>org.springframework.boot</groupId>
25                 <artifactId>spring-boot-maven-plugin</artifactId>
26             </plugin>
27             <plugin>
28                 <artifactId>maven-resources-plugin</artifactId>
29                 <configuration>
30                     <encoding>utf-8</encoding>
31                     <useDefaultDelimiters>true</useDefaultDelimiters>
32                 </configuration>
33             </plugin>
34         </plugins>
35     </build>
  其中,增加maven-resources-plugin就已經可以使用${}了(此時@@依舊存在),resources只是修改影響文件范圍
  filtering設置為true很關鍵,不然引用不到profiles中的內容。

    3:測試了再自己的pom中不增加resource配置,只是增加

也是可以的哦

建議:

    既然已經使用了springboot,就不要再用回之前的寫法了,平白增加配置。

 

附:pom的profile導入.properties

方法1:

 1   <profiles>
 2     <!--開發環境-->
 3     <profile>
 4       <id>dev</id>
 5       <activation>
 6         <activeByDefault>true</activeByDefault>
 7       </activation>
 8       <properties>
 9         <spring.profiles.active>dev</spring.profiles.active>
10       </properties>
11       <build>
12         <filters>
13           <filter>${basedir}/src/main/resources/application-dev.properties</filter>
14         </filters>
15       </build>
16     </profile>
17     <!--預發環境-->
18     <profile>
19       <id>pre</id>
20       <activation>
21         <activeByDefault>true</activeByDefault>
22       </activation>
23       <properties>
24         <spring.profiles.active>pre</spring.profiles.active>
25       </properties>
26       <build>
27         <filters>
28           <filter>${basedir}/src/main/resources/application-pre.properties</filter>
29         </filters>
30       </build>
31     </profile>
32   </profiles>

方法2:

 

 1   <profiles>
 2     <!--開發環境-->
 3     <profile>
 4       <id>dev</id>
 5       <activation>
 6         <activeByDefault>true</activeByDefault>
 7       </activation>
 8       <properties>
 9         <spring.profiles.active>dev</spring.profiles.active>
10       </properties>
11     </profile>
12     <!--預發環境-->
13     <profile>
14       <id>pre</id>
15       <activation>
16         <activeByDefault>true</activeByDefault>
17       </activation>
18       <properties>
19         <spring.profiles.active>pre</spring.profiles.active>
20       </properties>
21     </profile>
22   </profiles>
23 
24   <build>
25     <filters>
26          <filter> 
27 ${basedir}/src/main/resources/application-${spring.profiles.active}.properties 
28          </filter>
29     </filters>
30   </build>

 

 


免責聲明!

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



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