SpringBoot Maven插件在Maven中提供了对SpringBoot的支持,允许打包可执行jar或war并直接运行应用程序。

在~/.m2/repository/org/springframework/boot/spring-boot-dependencies/2.2.5.RELEASE/spring-boot-dependencies-2.2.5.RELEASE.pom文件列出了spring boot 提供了的jar包依赖。SpringBoot Maven插件的配置也在其中,如果没有,就要将下面的的配置添加到你的pom.xml文件中:

.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.5.RELEASE</version>
</plugin>
...
</plugins>
</pluginManagement>
</build>

现在我们来看看Springboot maven插件提供的一些有用的命令:

命令

作用

spring-boot:run

运行Spring Boot应用程序

spring-boot:repackage

重新打包已有的jar/war,便可以使用java -jar从命令行执行它们

spring-boot:startspring-boot:stop

启动和停止Spring Boot应用程序,与run目标相反,它不会阻止并允许其他目标在应用程序上运行。 此目标通常用于集成测试场景,其中应用程序在测试套件之前启动并在之后停止。

spring-boot:build-info

根据当前MavenProject的内容生成build-info.properties文件,可供执行器使用。

1.spring-boot:run

~/Desktop/MyProject/springboot$ mvn spring-boot:run

2.spring-boot:repackage

在spring boot里,可以直接把应用打包成为一个jar/war,然后java -jar <name>.jar命令运行启动jar/war,不需要另外配置一个Web Server。

(1)首先,要打jar包,还是war包,需要先在pom.xml里指定:

<project ...>
...
<packaging>jar</packaging>
...
</project>

(2)为了能够顺利重新打包你的应用程序,使用java -jar运行,需要添加下面的配置到pom.xml:

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.2.5.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

(3)执行重新打包

~/Desktop/MyProject/springboot$ mvn package spring-boot:repackage

如果遇到异常:repackage failed: Source file must be provided,请参考​​《repackage failed: Source file must be provided异常》​

3.spring-boot:build-info

根据当前MavenProject的内容生成build-info.properties文件。

~/Desktop/MyProject/springboot$ mvn spring-boot:build-info

结果在target/classes目录里生成META-INF/build-info.properties:

/Desktop/MyProject/springboot$ cd target
~/Desktop/MyProject/springboot/target$ tree
.
classes
com
wong
App.class
First.class
META-INF
build-info.properties
...
~/Desktop/MyProject/springboot/target$ cat classes/META-INF/build-info.properties
build.artifact=springboot
build.group=com.wong
build.name=springboot
build.time=2020-03-22T03\:35\:24.179Z
build.version=1.0.0

4.spring-boot:start 和 spring-boot:stop

~/Desktop/MyProject/springboot$ mvn package spring-boot:start