官方文檔鏈接地址:http://docs.spring.io/spring-boot/docs/1.3.0.BUILD-SNAPSHOT/maven-plugin/index.html
第一部分
Spring Boot Maven Plugin提供了Spring Boot的Maven支持,允許你打包可執(zhí)行文件和war文件,并且就地運(yùn)行。
第一章 目標(biāo)概述
Spring Boot Plugin有如下目標(biāo):
1.spring-boot:run 運(yùn)行Spring Boot應(yīng)用
2.spring-boot:repackage 重新打包jar/war包為可執(zhí)行包
3.spring-boot:start 和spring-boot:stop 管理Spring Boot 應(yīng)用的生命周期(即集成測試)
用法
關(guān)于如何使用Spring Boot Plugin的通用介紹可以在 第二章 章節(jié)中找到。一些更具體的用例在如下的例子中介紹。
用例
為了更好的理解Spring Boot的某些用法,可以參加如下章節(jié)例子。
1.自定義分類重打包
2.排除依賴
3.調(diào)試應(yīng)用
4.隨機(jī)端口集成測試
5.指定使用的(active)文件
第二章 目標(biāo)
2.1插件文檔
插件可使用目標(biāo)如下:
| 目標(biāo) | 詳情
|
| spring-boot:help | 展示spring-boot-maven-plugin的幫助信息,使用 mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name>展示參數(shù)詳情 |
| spring-boot:repackage | 重新打包存在的jar或者war包從而使他們可以在命令行使用jar -jar來執(zhí)行,使用layout=NONE也可以簡單的打包有嵌套依賴的jar(沒有主類,所以無法執(zhí)行)
|
| spring-boot:run | 運(yùn)行一個(gè)可執(zhí)行的應(yīng)用 |
| spring-boot:start | 啟動(dòng)Spring應(yīng)用程序。和run目標(biāo)不同,該目標(biāo)不會(huì)阻塞,并且允許其他目標(biāo)來操作應(yīng)用程序。這個(gè)目標(biāo)通常是在應(yīng)用程序集成測試套件開始之前和停止之后的繼承測試腳本中使用
|
| spring-boot:stop | 停止使用start目標(biāo)啟動(dòng)的spring應(yīng)用程序,通常在測試套件完成后被調(diào)用。 |
2.2系統(tǒng)要求
Maven:2.0+
JDK:1.6+
Memory:沒有最小限制
磁盤空間:沒有最小限制
2.3用法
需要在項(xiàng)目插件配置中指定版本
在parent pom文件中配置插件管理
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</plugin>
</plugins>
</pluginManagement>
在parent pom文件或者在module的pom文件中配置插件如下:
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
</plugin>
</plugins>
第三章:用法
插件提供了多個(gè)目標(biāo)來與SpringBoot應(yīng)用程序工作。
repackage:創(chuàng)建自動(dòng)執(zhí)行的jar包或者war包。它可以替代常規(guī)的構(gòu)件或者連接到構(gòu)建生命周期并有獨(dú)立的分級(jí)。
run:運(yùn)行SpringBoot應(yīng)用程序,同時(shí)可以使用多個(gè)選項(xiàng)將參數(shù)傳遞給應(yīng)用程序。
start和stop:集成spring boot應(yīng)用程序到集成測試階段,從而使應(yīng)用程序在集成測試程序之前啟動(dòng)
3.1 重新打包應(yīng)用
為了重新打包應(yīng)用,只需要在pom文件中的plugin配置中如下:
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
上邊重新打包jar和war包的例子是在Maven的package命令生命周期內(nèi)構(gòu)建的。包括所有的scope為provided的依賴。如果某些依賴需要被排除掉,可以使用exclude選項(xiàng)。具體參考 排除一個(gè)依賴 章節(jié)。
重新打包之后,默認(rèn)情況下原始的(沒有配置exectuable)構(gòu)件(artifact)被重命名為.original,并且使用自定義的分級(jí)可以保持原始構(gòu)件。
該構(gòu)件重寫了manifest,特別是它管理了之類和啟動(dòng)類條目。所以默認(rèn)情況下不起作用你就必須配置這些條目(不是在jar插件中)。Main-Class實(shí)際上由boot插件的layout屬性控制。
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); background-color: rgb(245, 245, 245);"><plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<mainClass>${start-class}</mainClass>
<layout>ZIP</layout>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin></pre>
3.2 運(yùn)行應(yīng)用程序
插件包含了一個(gè)run目標(biāo),該目標(biāo)能夠從命令行執(zhí)行應(yīng)用程序: mvn spring-boot:run
默認(rèn)情況下,應(yīng)用從Maven的JVM運(yùn)行。如果需要在分支中運(yùn)行,則指定fork選項(xiàng)。如果指定了jvmArguments或者agent參數(shù),分支進(jìn)程也會(huì)執(zhí)行。
如果需要制定某些JVM參數(shù)(如為了debug),可以使用jvmArguments參數(shù),更多細(xì)節(jié)參考 調(diào)試應(yīng)用 一章。方便起見,為了啟用總則(profiles),可以使用特定(profiles)屬性來處理,參考 指定使用的配置文件 一章。
Spring Boot 1.3已經(jīng)推出了devtools,它是提升使用Spring Boot應(yīng)用開發(fā)時(shí)經(jīng)驗(yàn)的一個(gè)模塊。啟用該模塊,僅僅在項(xiàng)目中添加如下配置即可:
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); background-color: rgb(245, 245, 245);"><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<optional>true</optional>
</dependency>
</dependencies></pre>
目前最新是2.0.0.BUILD-SNAPSHOT了。
當(dāng)devtools運(yùn)行時(shí),會(huì)在重新編譯應(yīng)用時(shí)進(jìn)行檢測變化并且自動(dòng)刷新。這不僅包括資源文件,也包括代碼。它也提供了一個(gè)激活的可以重加在的服務(wù)器,所以不管任何改變都會(huì)自動(dòng)出發(fā)瀏覽器刷新。
devtools也可配置成僅僅靜態(tài)資源改變時(shí)刷新瀏覽器(也就是忽略代碼的改變),僅僅增加如下配置:
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; border: 1px solid rgba(0, 0, 0, 0.14902); background-color: rgb(245, 245, 245);">spring.devtools.remote.restart.enabled=false</pre>
在devtools之前,該插件已經(jīng)默認(rèn)支持資源的及時(shí)刷新(hot refreshing),為了支持devtools功能,該插件功能已經(jīng)被禁用。但是可以隨時(shí)恢復(fù)該功能,恢復(fù)功能配置如下;
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
...
</plugins>
...
</build></pre>
當(dāng)啟用addResources配置時(shí),任意src/main/resources文件夾在應(yīng)用運(yùn)行時(shí)將被添加到應(yīng)用的類路徑,同時(shí)任意target/class中發(fā)現(xiàn)重復(fù)的資源將被移除。這將在發(fā)布web應(yīng)用時(shí)使資源及時(shí)刷新非常有用。例如,當(dāng)使用HTML,CSS和JavaScript文件時(shí),不用重新編譯應(yīng)用就可以立馬看到變化。這對前端開發(fā)人員不用下載安裝Java IDE就可以工作也是一種非常有用的方式。
需要注意的是,該特性的副作用是在構(gòu)建時(shí)資源過濾不起作用
為了與repackage目標(biāo)保持一致,run目標(biāo)在構(gòu)件類路徑下文件時(shí)將排除在配置依賴時(shí)排除的依賴配置。更詳細(xì)的的請參考 排除一個(gè)依賴 章節(jié)。
有時(shí)候在運(yùn)行應(yīng)用時(shí)包含測試依賴也是非常有用的。例如,在測試模式下使用根目錄類運(yùn)行應(yīng)用。如果希望這樣做,可以設(shè)置useTestClasspath參數(shù)的值為true。注意:盡在運(yùn)行應(yīng)用時(shí)生效:重新打包目標(biāo)將不會(huì)增加測試依賴到結(jié)果jar和war包中。
3.3 使用集成測試
雖然可以很容易從測試(測試套件)本身啟動(dòng)Spring Boot程序,但可能需要在構(gòu)建自身來處理。為了確信圍繞集成測試的Spring Boot應(yīng)用的生命周期被合適的管理,可以使用start和stop目標(biāo)。如下配置:
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><build>
...
<plugins>
...
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
</execution>
<execution>
<id>post-integration-test</id>
<goals>
<goal>stop</goal>
</goals>
</execution>
</executions>
</plugin>
...
</plugins>
...
</build></pre>
這樣的設(shè)置現(xiàn)在可以使用failsafe-plugin來運(yùn)行你的集成測試,正如你所期待的哪樣。
更多詳細(xì)細(xì)節(jié),參 考隨機(jī)端口的集成測試的。
第二部分
第四章 自定義分類重打包
默認(rèn)情況下,repackage目標(biāo)將使用可執(zhí)行的構(gòu)件來替代原始的構(gòu)件。如果希望保留原是構(gòu)件,并且也使用不同的分類來附屬保留可執(zhí)行的構(gòu)件,可以配置如下:
說明:如果不適用repackage目標(biāo),那么maven執(zhí)行package命令生成的jar包只有一個(gè),名稱為pom.xml里面配置的name(artifactId)-version.jar
如果加入了repackage配置,則maven打包生成的jar包會(huì)被重命名為name-version.jar.original,使用repackage重新打包生成的jar包名稱為name-version.jar,
下面的配置就是如果希望保留原始構(gòu)件生成的jar包名稱不變,同時(shí)也想保留repackage打包生成的jar包,可以自定義命名。
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project></pre>
如上配置,那么使用repackage重新生成的包的名稱就是name-version-exec.jar,就是在version后面追加了configuration節(jié)點(diǎn)中的classifier節(jié)點(diǎn)中的值,該值是自定義的。但是如果classifier節(jié)點(diǎn)中什么值都不寫,那么就和默認(rèn)的repackage配置一樣,即原始的構(gòu)件為name-version.jar.original,repackage打包的jar為name-version.jar
第五章 排除依賴
默認(rèn)情況下,repackage和run目標(biāo)都會(huì)包含所有provided scope的依賴?;赽oot的項(xiàng)目應(yīng)該考慮provided scope的依賴就像容器所需要的依賴包來使應(yīng)用可以運(yùn)行。
有三種方式可以排除運(yùn)行時(shí)被打包使用的依賴
1、通過指定groupId和artifactId來排除依賴(如果需要可以指定classifier,這是可選的)
2、通過指定artifactId,來排除所有匹配的依賴
3、通過指定groupId,來排除所有屬于該group的依賴
如下通過指定groupId和artifactId排除依賴
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<excludes>
<exclude>
<groupId>com.foo</groupId>
<artifactId>bar</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project></pre>
如上配置就會(huì)排除對com.foo:bar的jar包
如下通過指定artifactId,來排除artifactId與此匹配的所有依賴
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<excludeArtifactIds>my-lib,another-lib</excludeArtifactIds>
</configuration>
</plugin>
</plugins>
</build>
</project></pre>
如上配置就會(huì)排除所有artifactId為my-lib和another-lib的jar包
如下通過指定groupId,來排除groupId與此匹配的所有依賴
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<excludeGroupIds>com.foo</excludeGroupIds>
</configuration>
</plugin>
</plugins>
</build>
</project></pre>
如上配置則排除掉所有g(shù)roupId為com.foo的jar包
第六章 調(diào)試應(yīng)用
默認(rèn)情況下,run目標(biāo)和mvn命令是在同一個(gè)進(jìn)程中執(zhí)行的,除非jvm參數(shù)或者客戶端明確指定??梢酝ㄟ^使用fork屬性明確的開啟或者關(guān)閉是否在同一進(jìn)程中執(zhí)行。
如果需要fork這個(gè)進(jìn)程并且進(jìn)行調(diào)試,可以添加需要的JVM參數(shù)來開啟遠(yuǎn)程調(diào)試。如下配置為掛起進(jìn)程,直到有調(diào)試請求從5005端口進(jìn)入。
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<jvmArguments>
-Xdebug -Xrunjdwp:transport=dt_sorket,server=y,suspend=y,address=5005
</jvmArguments>
</configuration>
</plugin>
</plugins>
</build>
</project></pre>
需要注意的是,只要你指定了這些JVM參數(shù),這個(gè)進(jìn)程就會(huì)自動(dòng)被fork。這些jvm擦?xí)部梢栽诿钚兄兄付?,確認(rèn)書寫正確:
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);">mvn spring-boot:run -Drun.jvmArguments="-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=5005"</pre>
第七章 集成測試的隨機(jī)端口
Spring Boot集成測試的一個(gè)好特性是它能夠?yàn)閃eb應(yīng)用分配一個(gè)空閑端口。當(dāng)start目標(biāo)插件使用時(shí),Spring Boot應(yīng)用是被分離執(zhí)行的,這讓傳遞給集成測試程序本身實(shí)際的端口變得非常困難。
如下的配置展示如何使用build-help-plugin插件達(dá)到相同的特性。
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<executions>
<execution>
<id>reserve-tomcat-port</id>
<goals>
<goal>reserve-network-port</goal>
</goals>
<phase>process-resources</phase>
<configuration>
<portNames>
<portName>tomcat.http.port</portName>
</portNames>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<executions>
<execution>
<id>pre-integration-test</id>
<goals>
<goal>start</goal>
</goals>
<configuration>
<arguments>
<argument>--server.port={tomcat.http.port}</test.server.port>
</systemPropertyVariables>
</configuration>
</plugin>
</plugins>
</build></pre>
現(xiàn)在可以在任意的集成測試中查詢test.server.port系統(tǒng)屬性來給server創(chuàng)建一個(gè)合適的url。
第八章 指定使用的配置文件
一個(gè)特定應(yīng)用使用的配置文件可以通過profiles參數(shù)指定。如下配置啟動(dòng)了foo和bar兩個(gè)配置文件:
<pre style="font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px; text-decoration: none; padding: 9.5px; color: rgb(51, 51, 51); border-top-left-radius: 4px; border-top-right-radius: 4px; border-bottom-right-radius: 4px; border-bottom-left-radius: 4px; margin-top: 0px; margin-bottom: 10px; line-height: 20px; word-break: break-all; word-wrap: break-word; white-space: pre-wrap; background-color: rgb(245, 245, 245); border: 1px solid rgba(0, 0, 0, 0.14902);"><project>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.3.0.BUILD-SNAPSHOT</version>
<configuration>
<profiles>
<profile>foo</profile>
<profile>bar</profile>
</profiles>
</configuration>
</plugin>
</plugins>
</build>
</project></pre>
使用哪個(gè)配置文件也可以通過命令行參數(shù)配置,如果有多個(gè),需要使用都好將他們隔開:
mvn spring-boot:run -Drun.profiles=bar,foo