使用Maven运行你的应用程序
该插件包含一个run目标,可用于从命令行启动你的应用程序,如下例所示:
$ mvn spring-boot:run
可以通过`arguments`参数指定应用程序参数,详见 使用应用程序参数。
应用程序在一个分叉进程中执行,在命令行上设置属性不会影响应用程序。 如果需要指定一些JVM参数(如用于调试),可以使用`jvmArguments`参数,详见 调试应用程序。 同时也明确支持 系统属性 和 环境变量。
由于启用profile非常常见,特设了 profiles
属性,可作为 -Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
的快捷方式,详见 指定激活的profiles。
Spring Boot devtools
模块可提升开发时体验。
要启用它,只需将以下依赖添加到你的项目中:
<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.5.0
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.5.0
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
。
任何String类型的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中指定的值,因此无法指定以空格开头或结尾的环境变量。
任何String类型的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
可通过`profiles`参数指定应用程序要使用的激活profiles。
如下配置启用`local`和`dev` profiles:
<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>
也可在命令行指定要启用的profiles,务必用逗号分隔,如下例所示:
$ mvn spring-boot:run -Dspring-boot.run.profiles=local,dev