使用 Maven 執行你的應用

該外掛包含一個 run 目標,可用於從命令列啟動你的應用,如下例所示

$ mvn spring-boot:run

應用引數可以使用 arguments 引數指定,更多詳情請參閱 使用應用引數

應用在單獨的程序中執行,在命令列上設定屬性不會影響應用。如果你需要指定一些 JVM 引數(例如用於除錯),可以使用 jvmArguments 引數,更多詳情請參閱 除錯應用。還明確支援系統屬性環境變數

由於啟用 profile 非常常見,因此有一個專門的 profiles 屬性,它提供了 -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev" 的快捷方式,更多詳情請參閱 指定活動 profile

Spring Boot devtools 是一個模組,用於改善開發 Spring Boot 應用時的開發時體驗。要啟用它,只需將以下依賴新增到你的專案

<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-devtools</artifactId>
		<optional>true</optional>
	</dependency>
</dependencies>

devtools 執行時,它會在你重新編譯應用時檢測到變化並自動重新整理。這不僅適用於資源,也適用於程式碼。它還提供了一個 LiveReload 伺服器,以便在任何東西發生變化時自動觸發瀏覽器重新整理。

還可以配置 Devtools,使其僅在靜態資源發生變化時重新整理瀏覽器(並忽略程式碼中的任何變化)。只需在你的專案中包含以下屬性

spring.devtools.remote.restart.enabled=false

devtools 之前,外掛預設支援資源的即時重新整理,現在為了支援上面描述的解決方案,該功能已被停用。你可以透過配置你的專案隨時恢復它

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<addResources>true</addResources>
				</configuration>
			</plugin>
		</plugins>
	</build>

啟用 addResources 後,當你執行應用時,任何 src/main/resources 目錄都將新增到應用 classpath 中,並且 classes 輸出中找到的任何重複檔案將被移除。這允許資源的即時重新整理,在開發 web 應用時非常有用。例如,你可以處理 HTML、CSS 或 JavaScript 檔案,並立即看到你的更改而無需重新編譯應用。這也是一種有用的方式,可以讓你的前端開發人員無需下載和安裝 Java IDE 即可工作。

使用此功能的一個副作用是構建時對資源的過濾將不起作用。

為了與 repackage 目標保持一致,run 目標會以這樣一種方式構建 classpath,即外掛配置中排除的任何依賴也會從 classpath 中排除。更多詳情請參閱專用示例

有時執行應用的測試變體很有用。例如,如果你想在開發時使用 Testcontainers 或利用一些測試樁。為此,請使用 test-run 目標,它具有與 run 相同的許多特性和配置選項。

spring-boot:run

org.springframework.boot:spring-boot-maven-plugin:3.4.5

就地執行應用。

必需引數

名稱 型別 預設值

classesDirectory

File

${project.build.outputDirectory}

可選引數

名稱 型別 預設值

addResources

boolean

false

additionalClasspathElements

String[]

agents

File[]

arguments

String[]

commandlineArguments

String

environmentVariables

Map

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

mainClass

String

noverify

boolean

optimizedLaunch

boolean

true

profiles

String[]

skip

boolean

false

systemPropertyVariables

Map

useTestClasspath

Boolean

false

workingDirectory

File

引數詳情

addResources

將 Maven 資源直接新增到 classpath 中,這允許資源的即時就地編輯。從 target/classes 中移除重複資源,以防止在呼叫 ClassLoader.getResources() 時它們出現兩次。請考慮將 spring-boot-devtools 新增到你的專案,因為它提供了此功能以及更多功能。

名稱

addResources

型別

boolean

預設值

false

使用者屬性

spring-boot.run.addResources

始於

1.0.0

additionalClasspathElements

應新增到 classpath 中的額外 classpath 元素。一個元素可以是包含類和資源的目錄,也可以是 jar 檔案。

名稱

additionalClasspathElements

型別

java.lang.String[]

預設值

使用者屬性

spring-boot.run.additional-classpath-elements

始於

3.2.0

agents

Agent jar 檔案路徑。

名稱

agents

型別

java.io.File[]

預設值

使用者屬性

spring-boot.run.agents

始於

2.2.0

arguments

應傳遞給應用的引數。

名稱

arguments

型別

java.lang.String[]

預設值

使用者屬性

始於

1.0.0

classesDirectory

包含用於執行應用的類檔案和資原始檔的目錄。

名稱

classesDirectory

型別

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

始於

1.0.0

commandlineArguments

應傳遞給應用的命令列引數。使用空格分隔多個引數,並確保將多個值用引號括起來。指定時,優先於 #arguments

名稱

commandlineArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.run.arguments

始於

2.2.3

environmentVariables

應與用於執行應用的單獨程序關聯的環境變數列表。

名稱

environmentVariables

型別

java.util.Map

預設值

使用者屬性

始於

2.1.0

excludeGroupIds

要排除的 groupId 名稱的逗號分隔列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

始於

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了必需的 groupIdartifactId 元件以及一個可選的 classifier 元件。配置為屬性時,值應使用冒號分隔的元件用逗號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

始於

1.1.0

includes

要包含的 artifact 定義集合。Include 元素定義了必需的 groupIdartifactId 元件以及一個可選的 classifier 元件。配置為屬性時,值應使用冒號分隔的元件用逗號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

始於

1.2.0

jvmArguments

應與用於執行應用的單獨程序關聯的 JVM 引數。在命令列上,確保將多個值用引號括起來。

名稱

jvmArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.run.jvmArguments

始於

1.1.0

mainClass

主類的名稱。如果未指定,將使用找到的第一個包含 'main' 方法的已編譯類。

名稱

mainClass

型別

java.lang.String

預設值

使用者屬性

spring-boot.run.main-class

始於

1.0.0

noverify

指示 agent 需要 -noverify 的標誌。

名稱

noverify

型別

boolean

預設值

使用者屬性

spring-boot.run.noverify

始於

1.0.0

optimizedLaunch

是否應最佳化 JVM 的啟動。

名稱

optimizedLaunch

型別

boolean

預設值

true

使用者屬性

spring-boot.run.optimizedLaunch

始於

2.2.0

profiles

要啟用的 Spring profile。指定 'spring.profiles.active' 引數的便捷快捷方式。在命令列上使用逗號分隔多個 profile。

名稱

profiles

型別

java.lang.String[]

預設值

使用者屬性

spring-boot.run.profiles

始於

1.3.0

skip

跳過執行。

名稱

skip

型別

boolean

預設值

false

使用者屬性

spring-boot.run.skip

始於

1.3.2

systemPropertyVariables

要傳遞給程序的 JVM 系統屬性列表。

名稱

systemPropertyVariables

型別

java.util.Map

預設值

使用者屬性

始於

2.1.0

useTestClasspath

執行時是否包含測試 classpath 的標誌。

名稱

useTestClasspath

型別

java.lang.Boolean

預設值

false

使用者屬性

spring-boot.run.useTestClasspath

始於

1.3.0

workingDirectory

應用使用的當前工作目錄。如果未指定,將使用 basedir。

名稱

workingDirectory

型別

java.io.File

預設值

使用者屬性

spring-boot.run.workingDirectory

始於

1.5.0

spring-boot:test-run

org.springframework.boot:spring-boot-maven-plugin:3.4.5

使用測試執行時 classpath 就地執行應用。用於啟動應用的主類確定順序如下:如果配置了主類,則使用配置的主類。然後,如果在測試類目錄中找到主類,則使用該主類。然後,如果在類目錄中找到主類,則使用該主類。

必需引數

名稱 型別 預設值

classesDirectory

File

${project.build.outputDirectory}

testClassesDirectory

File

${project.build.testOutputDirectory}

可選引數

名稱 型別 預設值

addResources

boolean

false

additionalClasspathElements

String[]

agents

File[]

arguments

String[]

commandlineArguments

String

environmentVariables

Map

excludeGroupIds

String

excludes

List

includes

List

jvmArguments

String

mainClass

String

noverify

boolean

optimizedLaunch

boolean

true

profiles

String[]

skip

boolean

false

systemPropertyVariables

Map

workingDirectory

File

引數詳情

addResources

將 Maven 資源直接新增到 classpath 中,這允許資源的即時就地編輯。從 target/classes 中移除重複資源,以防止在呼叫 ClassLoader.getResources() 時它們出現兩次。請考慮將 spring-boot-devtools 新增到你的專案,因為它提供了此功能以及更多功能。

名稱

addResources

型別

boolean

預設值

false

使用者屬性

spring-boot.run.addResources

始於

1.0.0

additionalClasspathElements

應新增到 classpath 中的額外 classpath 元素。一個元素可以是包含類和資源的目錄,也可以是 jar 檔案。

名稱

additionalClasspathElements

型別

java.lang.String[]

預設值

使用者屬性

spring-boot.run.additional-classpath-elements

始於

3.2.0

agents

Agent jar 檔案路徑。

名稱

agents

型別

java.io.File[]

預設值

使用者屬性

spring-boot.run.agents

始於

2.2.0

arguments

應傳遞給應用的引數。

名稱

arguments

型別

java.lang.String[]

預設值

使用者屬性

始於

1.0.0

classesDirectory

包含用於執行應用的類檔案和資原始檔的目錄。

名稱

classesDirectory

型別

java.io.File

預設值

${project.build.outputDirectory}

使用者屬性

始於

1.0.0

commandlineArguments

應傳遞給應用的命令列引數。使用空格分隔多個引數,並確保將多個值用引號括起來。指定時,優先於 #arguments

名稱

commandlineArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.run.arguments

始於

2.2.3

environmentVariables

應與用於執行應用的單獨程序關聯的環境變數列表。

名稱

environmentVariables

型別

java.util.Map

預設值

使用者屬性

始於

2.1.0

excludeGroupIds

要排除的 groupId 名稱的逗號分隔列表(精確匹配)。

名稱

excludeGroupIds

型別

java.lang.String

預設值

使用者屬性

spring-boot.excludeGroupIds

始於

1.1.0

excludes

要排除的 artifact 定義集合。Exclude 元素定義了必需的 groupIdartifactId 元件以及一個可選的 classifier 元件。配置為屬性時,值應使用冒號分隔的元件用逗號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

excludes

型別

java.util.List

預設值

使用者屬性

spring-boot.excludes

始於

1.1.0

includes

要包含的 artifact 定義集合。Include 元素定義了必需的 groupIdartifactId 元件以及一個可選的 classifier 元件。配置為屬性時,值應使用冒號分隔的元件用逗號分隔:groupId:artifactId,groupId:artifactId:classifier

名稱

includes

型別

java.util.List

預設值

使用者屬性

spring-boot.includes

始於

1.2.0

jvmArguments

應與用於執行應用的單獨程序關聯的 JVM 引數。在命令列上,確保將多個值用引號括起來。

名稱

jvmArguments

型別

java.lang.String

預設值

使用者屬性

spring-boot.run.jvmArguments

始於

1.1.0

mainClass

主類的名稱。如果未指定,將使用找到的第一個包含 'main' 方法的已編譯類。

名稱

mainClass

型別

java.lang.String

預設值

使用者屬性

spring-boot.run.main-class

始於

1.0.0

noverify

指示 agent 需要 -noverify 的標誌。

名稱

noverify

型別

boolean

預設值

使用者屬性

spring-boot.run.noverify

始於

1.0.0

optimizedLaunch

是否應最佳化 JVM 的啟動。

名稱

optimizedLaunch

型別

boolean

預設值

true

使用者屬性

spring-boot.test-run.optimizedLaunch

始於

profiles

要啟用的 Spring profile。指定 'spring.profiles.active' 引數的便捷快捷方式。在命令列上使用逗號分隔多個 profile。

名稱

profiles

型別

java.lang.String[]

預設值

使用者屬性

spring-boot.run.profiles

始於

1.3.0

skip

跳過執行。

名稱

skip

型別

boolean

預設值

false

使用者屬性

spring-boot.run.skip

始於

1.3.2

systemPropertyVariables

要傳遞給程序的 JVM 系統屬性列表。

名稱

systemPropertyVariables

型別

java.util.Map

預設值

使用者屬性

始於

2.1.0

testClassesDirectory

包含用於執行應用的測試類檔案和資原始檔的目錄。

名稱

testClassesDirectory

型別

java.io.File

預設值

${project.build.testOutputDirectory}

使用者屬性

始於

workingDirectory

應用使用的當前工作目錄。如果未指定,將使用 basedir。

名稱

workingDirectory

型別

java.io.File

預設值

使用者屬性

spring-boot.run.workingDirectory

始於

1.5.0

示例

除錯應用

runtest-run 目標在單獨的程序中執行你的應用。如果你需要除錯它,應新增必要的 JVM 引數以啟用遠端除錯。以下配置將暫停程序,直到偵錯程式連線到埠 5005

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<jvmArguments>
						-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005
					</jvmArguments>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

這些引數也可以在命令列上指定

$ mvn spring-boot:run -Dspring-boot.run.jvmArguments=-agentlib:jdwp=transport=dt_socket,server=y,suspend=y,address=*:5005

使用系統屬性

可以使用 systemPropertyVariables 屬性指定系統屬性。以下示例將 property1 設定為 test,將 property2 設定為 42

<project>
	<build>
		<properties>
			<my.value>42</my.value>
		</properties>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<systemPropertyVariables>
						<property1>test</property1>
						<property2>${my.value}</property2>
					</systemPropertyVariables>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

如果值為空或未定義(即 <my-property/>),則系統屬性將設定為空字串作為值。Maven 會修剪 pom 中指定的值,因此無法透過此機制指定以空格開頭或結尾的系統屬性:請考慮使用 jvmArguments

任何 String 型別的 Maven 變數都可以作為系統屬性傳遞。任何嘗試傳遞任何其他 Maven 變數型別(例如 ListURL 變數)的操作將導致變量表達式被按字面意義(未評估)傳遞。

jvmArguments 引數優先於使用上述機制定義的系統屬性。在以下示例中,property1 的值被 overridden

$ mvn spring-boot:run -Dspring-boot.run.jvmArguments="-Dproperty1=overridden"

使用環境變數

可以使用 environmentVariables 屬性指定環境變數。以下示例設定了 'ENV1'、'ENV2'、'ENV3'、'ENV4' 環境變數

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<environmentVariables>
						<ENV1>5000</ENV1>
						<ENV2>Some Text</ENV2>
						<ENV3/>
						<ENV4></ENV4>
					</environmentVariables>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

如果值為空或未定義(即 <MY_ENV/>),則環境變數將設定為空字串作為值。Maven 會修剪 pom 中指定的值,因此無法指定以空格開頭或結尾的環境變數。

任何 String 型別的 Maven 變數都可以作為系統屬性傳遞。任何嘗試傳遞任何其他 Maven 變數型別(例如 ListURL 變數)的操作將導致變量表達式被按字面意義(未評估)傳遞。

以這種方式定義的環境變數優先於現有值。

使用應用引數

可以使用 arguments 屬性指定應用引數。以下示例設定了兩個引數:property1property2=42

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<arguments>
						<argument>property1</argument>
						<argument>property2=${my.value}</argument>
					</arguments>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

在命令列上,引數與 jvmArguments 一樣用空格分隔。如果引數包含空格,請確保用引號將其括起來。在以下示例中,有兩個可用引數:property1property2=Hello World

$ mvn spring-boot:run -Dspring-boot.run.arguments="property1 'property2=Hello World'"

指定活動 Profile

可以使用 profiles 引數指定特定應用要使用的活動 profile。

以下配置啟用了 localdev profile

<project>
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<configuration>
					<profiles>
						<profile>local</profile>
						<profile>dev</profile>
					</profiles>
				</configuration>
			</plugin>
		</plugins>
	</build>
</project>

要啟用的 profile 也可以在命令列上指定,請確保使用逗號分隔它們,如下例所示

$ mvn spring-boot:run -Dspring-boot.run.profiles=local,dev