使用 Maven 运行你的应用程序
插件包含一个运行目标,可以用于从命令行启动你的应用程序,如下例所示:
$ mvn spring-boot:run
可以使用 arguments
参数指定应用程序参数,详情请参见 使用应用程序参数。
应用程序在一个分叉的进程中执行,在命令行上设置属性不会影响应用程序。
如果需要指定一些 JVM 参数(即用于调试目的),可以使用 jvmArguments
参数,详情请参见 调试应用程序。
还有对 系统属性 和 环境变量 的显式支持。
由于启用配置文件是很常见的,因此有一个专用的 profiles
属性,它提供了 -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
的快捷方式,详情请参见 指定活动配置文件。
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
目录将在运行应用程序时添加到应用程序类路径中,并且在类输出中找到的任何重复项将被删除。
这允许资源的热刷新,这在开发 Web 应用程序时非常有用。
例如,你可以处理 HTML、CSS 或 JavaScript 文件,并立即看到更改,而无需重新编译应用程序。
这也是一种有助于前端开发人员工作的方式,他们无需下载和安装 Java IDE。
注意:使用此功能的一个副作用是构建时资源的过滤将不起作用。
为了与 repackage
目标保持一致,run
目标以这样的方式构建类路径,即插件配置中排除的任何依赖项也会从类路径中排除。
更多详情,请参见 专用示例。
有时运行应用程序的测试变体是有用的。
例如,如果你想在开发时使用 Testcontainers 或使用一些测试存根。
使用 test-run
目标,它具有与 run
相同的许多功能和配置选项。
spring-boot:run
org.springframework.boot:spring-boot-maven-plugin:3.4.6
Run an application in place.
Optional parameters
Name | Type | Default |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
Parameter details
addResources
Add maven resources to the classpath directly, this allows live in-place editing of resources. Duplicate resources are removed from target/classes
to prevent them from appearing twice if ClassLoader.getResources()
is called. Please consider adding spring-boot-devtools
to your project instead as it provides this feature and many more.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
additionalClasspathElements
Additional classpath elements that should be added to the classpath. An element can be a directory with classes and resources or a jar file.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
agents
Path to agent jars.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
arguments
Arguments that should be passed to the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
classesDirectory
Directory containing the classes and resource files that should be used to run the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
commandlineArguments
Arguments from the command line that should be passed to the application. Use spaces to separate multiple arguments and make sure to wrap multiple values between quotes. When specified, takes precedence over #arguments
.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
environmentVariables
List of Environment variables that should be associated with the forked process used to run the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludeGroupIds
Comma separated list of groupId names to exclude (exact match).
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludes
Collection of artifact definitions to exclude. The Exclude
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
includes
Collection of artifact definitions to include. The Include
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
jvmArguments
JVM arguments that should be associated with the forked process used to run the application. On command line, make sure to wrap multiple values between quotes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
mainClass
The name of the main class. If not specified the first compiled class found that contains a 'main' method will be used.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
noverify
Flag to say that the agent requires -noverify.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
optimizedLaunch
Whether the JVM’s launch should be optimized.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
profiles
The spring profiles to activate. Convenience shortcut of specifying the 'spring.profiles.active' argument. On command line use commas to separate multiple profiles.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
skip
Skip the execution.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
systemPropertyVariables
List of JVM system properties to pass to the process.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
spring-boot:test-run
org.springframework.boot:spring-boot-maven-plugin:3.4.6
Run an application in place using the test runtime classpath. The main class that will be used to launch the application is determined as follows: The configured main class, if any. Then the main class found in the test classes directory, if any. Then the main class found in the classes directory, if any.
Required parameters
Name | Type | Default |
---|---|---|
|
|
|
|
|
Optional parameters
Name | Type | Default |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
||
|
|
|
|
||
|
Parameter details
addResources
Add maven resources to the classpath directly, this allows live in-place editing of resources. Duplicate resources are removed from target/classes
to prevent them from appearing twice if ClassLoader.getResources()
is called. Please consider adding spring-boot-devtools
to your project instead as it provides this feature and many more.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
additionalClasspathElements
Additional classpath elements that should be added to the classpath. An element can be a directory with classes and resources or a jar file.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
agents
Path to agent jars.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
arguments
Arguments that should be passed to the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
classesDirectory
Directory containing the classes and resource files that should be used to run the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
commandlineArguments
Arguments from the command line that should be passed to the application. Use spaces to separate multiple arguments and make sure to wrap multiple values between quotes. When specified, takes precedence over #arguments
.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
environmentVariables
List of Environment variables that should be associated with the forked process used to run the application.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludeGroupIds
Comma separated list of groupId names to exclude (exact match).
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
excludes
Collection of artifact definitions to exclude. The Exclude
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
includes
Collection of artifact definitions to include. The Include
element defines mandatory groupId
and artifactId
components and an optional classifier
component. When configured as a property, values should be comma-separated with colon-separated components: groupId:artifactId,groupId:artifactId:classifier
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
jvmArguments
JVM arguments that should be associated with the forked process used to run the application. On command line, make sure to wrap multiple values between quotes.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
mainClass
The name of the main class. If not specified the first compiled class found that contains a 'main' method will be used.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
noverify
Flag to say that the agent requires -noverify.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
optimizedLaunch
Whether the JVM’s launch should be optimized.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
profiles
The spring profiles to activate. Convenience shortcut of specifying the 'spring.profiles.active' argument. On command line use commas to separate multiple profiles.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
skip
Skip the execution.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
systemPropertyVariables
List of JVM system properties to pass to the process.
Name |
|
---|---|
Type |
|
Default value |
|
User property |
|
Since |
|
示例
调试应用程序
run
和 test-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
。
任何字符串类型的 Maven 变量都可以作为系统属性传递。
任何尝试传递其他 Maven 变量类型(例如 List
或 URL
变量)的尝试都将导致变量表达式按字面传递(未求值)。
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 中指定的值,因此无法指定需要以空格开头或结尾的环境变量。
任何字符串类型的 Maven 变量都可以作为系统属性传递。
任何尝试传递其他 Maven 变量类型(例如 List
或 URL
变量)的尝试都将导致变量表达式按字面传递(未求值)。
以这种方式定义的环境变量优先于现有值。
使用应用程序参数
可以使用 arguments
属性指定应用程序参数。
以下示例设置了两个参数:property1
和 property2=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
的分隔方式相同。
如果参数包含空格,请确保对其进行引用。
在以下示例中,有两个参数可用:property1
和 property2=Hello World
:
$ mvn spring-boot:run -Dspring-boot.run.arguments="property1 'property2=Hello World'"
指定活动配置文件
可以使用 profiles
参数指定要用于特定应用程序的活动配置文件。
以下配置启用了 local
和 dev
配置文件:
<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>
也可以在命令行上指定要启用的配置文件,请确保用逗号分隔它们,如下例所示:
$ mvn spring-boot:run -Dspring-boot.run.profiles=local,dev