此版本仍在开发中,尚未被视为稳定版本。对于最新的稳定版本,请使用 Spring Boot 3.4.0! |
使用 Maven 运行应用程序
该插件包括一个 run 目标,可用于从命令行启动应用程序,如以下示例所示:
$ mvn spring-boot:run
可以使用arguments
参数,请参阅使用应用程序参数了解更多详细信息。
应用程序在分叉进程中执行,在命令行上设置属性不会影响应用程序。
如果需要指定一些 JVM 参数(用于调试目的),可以使用jvmArguments
参数,请参阅 调试应用程序 以了解更多详细信息。
此外,还显式支持系统属性和环境变量。
由于启用配置文件很常见,因此有专用的profiles
属性,该属性为-Dspring-boot.run.jvmArguments="-Dspring.profiles.active=dev"
,请参阅指定活动配置文件。
Spring Bootdevtools
是一个模块,用于改善使用 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
goal、run
goal 以这样一种方式构建 Classpath,即插件配置中排除的任何依赖项也会从 Classpath 中排除。
有关更多详细信息,请参阅专用示例。
有时,运行应用程序的测试变体很有用。
例如,如果您想在开发时使用 Testcontainers 或使用一些测试存根。
使用test-run
目标,具有许多与run
为此目的。
spring-boot:run
org.springframework.boot:spring-boot-maven-plugin:3.3.7-SNAPSHOT
就地运行应用程序。
可选参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
||
|
|
|
|
||
|
|
|
|
参数详情
addResources
将 maven 资源直接添加到 Classpath 中,这允许对资源进行实时就地编辑。从target/classes
为了防止它们出现两次,如果ClassLoader.getResources()
被调用。请考虑添加spring-boot-devtools
添加到您的项目中,因为它提供了此功能以及更多功能。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
additionalClasspathElements
应添加到 Classpath 中的其他 Classpath 元素。元素可以是包含类和资源的目录,也可以是 jar 文件。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
classesDirectory
包含应该用于运行应用程序的类和资源文件的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
commandlineArguments
命令行中应传递给应用程序的参数。使用空格分隔多个参数,并确保在引号之间将多个值括起来。指定后,优先于#arguments
.
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
directories
包含应添加到 Classpath 的类或资源的其他目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
environmentVariables
应与用于运行应用程序的分叉进程关联的环境变量列表。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
excludes
要排除的项目定义的集合。这Exclude
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
includes
要包含的项目定义的集合。这Include
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
jvmArguments
JVM 参数,这些参数应该与用于运行应用程序的分叉进程相关联。在命令行上,确保在引号之间将多个值括起来。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
mainClass
主类的名称。如果未指定,则将使用找到的第一个包含 'main' 方法的编译类。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
optimizedLaunch
是否应优化 JVM 的启动。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
profiles
要激活的 Spring 轮廓。指定 'spring.profiles.active' 参数的便捷快捷方式。在命令行中,使用逗号分隔多个配置文件。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
systemPropertyVariables
要传递给进程的 JVM 系统属性的列表。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
spring-boot:test-run
org.springframework.boot:spring-boot-maven-plugin:3.3.7-SNAPSHOT
使用测试运行时 Classpath 就地运行应用程序。将用于启动应用程序的主类按如下方式确定:配置的主类(如果有)。然后在 test classes 目录中找到主类(如果有)。然后在 classes 目录中找到主类(如果有)。
可选参数
名字 | 类型 | 违约 |
---|---|---|
|
|
|
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
|
|
|
||
|
|
|
|
||
|
参数详情
addResources
将 maven 资源直接添加到 Classpath 中,这允许对资源进行实时就地编辑。从target/classes
为了防止它们出现两次,如果ClassLoader.getResources()
被调用。请考虑添加spring-boot-devtools
添加到您的项目中,因为它提供了此功能以及更多功能。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
additionalClasspathElements
应添加到 Classpath 中的其他 Classpath 元素。元素可以是包含类和资源的目录,也可以是 jar 文件。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
classesDirectory
包含应该用于运行应用程序的类和资源文件的目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
commandlineArguments
命令行中应传递给应用程序的参数。使用空格分隔多个参数,并确保在引号之间将多个值括起来。指定后,优先于#arguments
.
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
directories
包含应添加到 Classpath 的类或资源的其他目录。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
environmentVariables
应与用于运行应用程序的分叉进程关联的环境变量列表。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
excludeGroupIds
要排除的 groupId 名称的逗号分隔列表(完全匹配)。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
excludes
要排除的项目定义的集合。这Exclude
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
includes
要包含的项目定义的集合。这Include
元素定义 mandatorygroupId
和artifactId
组件和可选的classifier
元件。当配置为属性时,值应使用冒号分隔的组件进行逗号分隔:groupId:artifactId,groupId:artifactId:classifier
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
jvmArguments
JVM 参数,这些参数应该与用于运行应用程序的分叉进程相关联。在命令行上,确保在引号之间将多个值括起来。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
mainClass
主类的名称。如果未指定,则将使用找到的第一个包含 'main' 方法的编译类。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
optimizedLaunch
是否应优化 JVM 的启动。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
profiles
要激活的 Spring 轮廓。指定 'spring.profiles.active' 参数的便捷快捷方式。在命令行中,使用逗号分隔多个配置文件。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
systemPropertyVariables
要传递给进程的 JVM 系统属性的列表。
名字 |
|
---|---|
类型 |
|
默认值 |
|
用户属性 |
|
因为 |
|
例子
调试应用程序
这run
和test-run
goals 在分叉的进程中运行您的应用程序。
如果需要调试它,则应添加必要的 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/
>) 时,system 属性将设置为空 String 作为值。
Maven 会修剪 pom 中指定的值,因此无法通过以下机制指定需要以空格开头或结尾的 System 属性:考虑使用jvmArguments
相反。
任何 String 类型的 Maven 变量都可以作为系统属性传递。
任何传递任何其他 Maven 变量类型(例如List
或URL
variable) 将导致变量表达式按字面传递 (unevaluated)。
这jvmArguments
parameter 优先于使用上述机制定义的系统属性。
在以下示例中,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/
>),env 变量设置为空 String 作为值。
Maven 会修剪 pom 中指定的值,因此无法指定需要以空格开头或结尾的 env 变量。
任何 String 类型的 Maven 变量都可以作为系统属性传递。
任何传递任何其他 Maven 变量类型(例如List
或URL
variable) 将导致变量表达式按字面传递 (unevaluated)。
以这种方式定义的环境变量优先于现有值。
使用应用程序参数
可以使用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