* springboot项目依赖spring-boot-dependencies,为什么还要指定spring-boot-maven-plugin的版本?
查看spring-boot-dependencies.pom 文件里边明确声明了依赖
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>2.3.4.RELEASE</version> </plugin>
为什么在项目里还要再配置一遍这个依赖?
可能他是想显示的指定版本号
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
插件中的maven目标
如图,插件提供了6个maven goal
spring-boot-maven-plugin
的帮助信息。使用命令行mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name>
可展示goal的参数描述信息。打包:repackage
将spring-boot-maven-plugin
引入pom,执行 mvn package
命令,即可打jar包(插件默认打jar包),target文件夹里的 *.jar
即为可执行jar包。
打包主要使用的是repackage goal,它是spring-boot-starter-parent
为插件设置的默认goal。这个goal绑定在maven的 package生命周期上,完整命令为mvn package spring-boot:repackage
。在 mvn package
执行打包之后,repackage 再次打包生成可执行的 jar包或war包。
默认情况下,repackage生成包的名称与 mvn package
生成的原始包名称相同,而原始包被重命名为 *.origin
。若想修改生成jar包的名称,请参考spring-boot-maven-plugin:打依赖包 中的方法。
repackage 命令生成的包,默认会包含项目引入的所有依赖,包括scope为provied的依赖,若除去provided依赖,请参考spring-boot-maven-plugin:打包时排除provided依赖。
若项目引入了spring-boot-devtools
,默认spring-boot-devtools
会被打在包里,若想排除,应设置 repackage 的 excludeDevtools参数为true。在打war包时,还应将spring-boot-devtools
的optinal
设置为true
或将scope
设置为provided
。
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<id>repackage</id>
<configuration>
<excludeDevtools>true</excludeDevtools>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
repackage 会在Manifest文件中写入Main-Class and Start-Class属性。当默认值不能使程序正常运行时,可以通过插件配置。Manifest文件位于的META-INF文件夹中。
打可执行jar包时,示例如下:
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: beauty
Start-Class: com.demo.beauty.BeautyApplication
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 2.2.1.RELEASE
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_251
Main-Class: org.springframework.boot.loader.JarLauncher
对应的spring-boot-maven-plugin
配置如下:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<mainClass>com.demo.beauty.BeautyApplication</mainClass>
</configuration>
</plugin>
</plugins>
</build>
可以看出,打可执行jar包时,spring-boot-maven-plugin
的mainClass
参数对应的是 Manifest 文件中的Start-Class属性,即项目的启动类。
Manifest 文件中的Main-Class属性由插件的 layout 决定。layout属性值默认为jar/war。layout种类有
WAR,即通常的可执行war。Main-Class : org.springframework.boot.loader.warLauncher。为避免将war包部署在容器中运行时可能的冲突问题,provided类型的依赖都被放置在可执行war包的WEB-INF/lib-provided文件夹中,包括直接运行war需要的内置容器。
ZIP,亦可作DIR,类似于JAR。Main-Class : org.springframework.boot.loader.PropertiesLauncher
NONE,打包所有依赖库和项目资源,但是不打包任何启动器。可以看到在layout为NONE时,打出的包中的org文件夹没有了,Manifest 文件中没有Start-Class属性,Main-Class属性值为项目的启动类。
Manifest-Version: 1.0
Archiver-Version: Plexus Archiver
Built-By: beauty
Spring-Boot-Classes: BOOT-INF/classes/
Spring-Boot-Lib: BOOT-INF/lib/
Spring-Boot-Version: 2.2.1.RELEASE
Created-By: Apache Maven 3.6.3
Build-Jdk: 1.8.0_251
Main-Class: com.demo.beauty.BeautyApplication
运行:run
run goal 可运行springboot项目,其默认将应用运行于 a forked process,通常方式设置的命令行参数不会对 run goal 启动的进程产生影响。
直接使用run goal,不能使程序进入调试模式,原因如上。调试,或设置其他JVM 参数,应在插件中配置 jvmArguments
。调试程序的配置可参考spring-boot-maven-plugin:debug调试程序
设置系统属性和环境变量的方法: spring-boot-maven-plugin:添加系统属性和环境变量
设置active profiles的方法:spring-boot-maven-plugin:设置active profiles
当将fork
属性为置为false时,run goal将直接通过 Maven JVM 运行项目(非官方建议做法),而不使用 a forked process。此时通过spring-boot-maven-plugin
插件设置的的JVM参数(jvmArguments
), 系统属性(systemPropertyVariables
),环境变量(environmentVariables
)和代理 (agents
)将不生效。
插件支持资源热更新,通过将addResources
置为true来启用热更
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
启用热更后,运行时, src/main/resources 文件夹被加入类路径, target/classes 文件夹下的副本被删除。
构建项目的资源筛选不作用于热更!
Spring Boot 1.3以后,官方推出了spring-boot-devtools
工具,这个工具在热更支持上体验更好,所以spring-boot-maven-plugin
的热更功能默认是关闭的。
与 repackage
goal 一致,run goal也将所有的依赖都放在类路径中,包括scope为provided的依赖
若想在运行时包含scope为test的依赖,可以将useTestClasspath
设置为true。注意,这只影响run goal 不影响repackage goal,即打包时不会包含scope为test的依赖
https://docs.spring.io/spring-boot/docs/current/maven-plugin/reference/htmlsingle/ 这里说了这么一句话:
If you do not want to use the spring-boot-starter-parent
, you can still keep the benefit of the dependency management (but not the plugin management) by using an import
scoped dependency, as follows:
关键是括号里的这句话 (but not the plugin management)所以需要在项目里明确定义spring-boot-maven-plugin的版本
<dependencyManagement>
<dependencies>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.5.0</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
您的问题已经有小伙伴解答了,请点击【采纳】按钮,采纳帮您提供解决思路的答案,给回答的人一些鼓励哦~~
ps:开通问答VIP,享受5次/月 有问必答服务,了解详情↓↓↓
【电脑端】戳>>> https://vip.csdn.net/askvip?utm_source=1146287632
【APP 】 戳>>> https://mall.csdn.net/item/52471?utm_source=1146287632