SpringBoot - spring-boot-maven-plugin introduction

brief description

This is SpringBoot's MAVEN plug-in, which is mainly used for packaging. It is usually used to package projects into JAR or WAR files (to generate FAT packages).
The packages generated by this plugin are executable JARs.

Spring Boot Maven Plugin Documentation:https://docs.spring.io/spring-boot/docs/2.4.2/maven-plugin/reference/htmlsingle/

What is the role of the spring-boot-maven-plugin plugin in the SpringBoot project? : https://blog.csdn.net/goodjava2007/article/details/122205769

effect

(1) The JAR or WAR file packaged with this plug-in contains all dependencies and resource files, and can be started and run directly on the command line or on the WEB server. If this plug-in is not used, all dependencies of the packaged project are manually configured. project and resource files, would be cumbersome, complex, and error-prone. That is to say, SPRING BOOT uses the spring-boot-maven-plugin plug-in to package all the JARs needed to start and run the application together, logically the JAR has the conditions to run independently.
(2) The plug-in can specify the class to be executed. If it is not specified, it can automatically detect the main function in the project and start the SPRING BOOT container.

Function

The plug-in has 7 GOALs, and the commonly used 5 goals are as follows:
(1) spring-boot: run, run the SPRING BOOT application;
(2) spring-boot: repackage, the default value of the plug-in, is to execute the mvn package again Package, make it executable, and add the suffix .origin to the package generated for the first time;
(3) spring-boot:start, manage the life cycle of the SPRING BOOT application, this goal is usually used in integration testing solutions, in In this scheme, the application starts before the test suite, and then stops after the test suite;
(4) spring-boot:stop, manages the life cycle of the SPRING BOOT application, and stops the Spring application that has been started by the start target. Usually called after the test suite is completed;
(5) spring-boot:build-info, which generates construction information that the executor can use.
insert image description here

how to use

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	// 用于指定生成JAR还是WAR文件。
    <packaging>jar</packaging>
    
    // 该插件默认是在PACKAGE阶段执行插件的 repackage 目标。
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.4.2</version>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

insert image description here

use analysis

Since the repackage target is bound to the package phase of the default life cycle by default, when running the mvn package, the repackage target will be executed according to the above configuration.

how to pack

# 使用如下指令在命令行中进行打包:
mvn clean package

(1) The plug-in can be understood as a secondary encapsulation of the mvn package command. On the basis of it, the MAVEN command (target) adapted to the SPRING BOOT project is added. The purpose is to perform secondary modifications to the original package produced by MAVEN. Such as: repackaging into a new, executable JAR file, etc.
(2) If it is a SPRING BOOT project (and the spring-boot-starter-parent package is used), by default, when executing mvn package, the regular operation of the package will be performed first, and then the repackage operation of spring-boot will be performed.

Error analysis

(1) Instructions
# 使用如下指令在命令行中执行:
mvn clean package spring-boot:repackage
(2) error
[INFO] Reactor Summary for servicex 2.5.1:
[INFO]
[INFO] servicex ........................................... SKIPPED
[INFO] servicex-common .................................... SKIPPED
[INFO] servicex-common-excel .............................. SKIPPED
[INFO] servicex-common-core ............................... SKIPPED
[INFO] servicex-api ....................................... SKIPPED
[INFO] servicex-api-system ................................ SKIPPED
[INFO] servicex-common-redis .............................. SKIPPED
[INFO] servicex-common-security ........................... SKIPPED
[INFO] servicex-auth ...................................... SKIPPED
[INFO] servicex-gateway ................................... SKIPPED
[INFO] servicex-visual .................................... SKIPPED
[INFO] servicex-visual-monitor ............................ SKIPPED
[INFO] servicex-common-datasource ......................... SKIPPED
[INFO] servicex-common-datascope .......................... SKIPPED
[INFO] servicex-common-log ................................ SKIPPED
[INFO] servicex-common-swagger ............................ SKIPPED
[INFO] servicex-modules ................................... SKIPPED
[INFO] servicex-modules-system ............................ SKIPPED
[INFO] servicex-modules-gen ............................... SKIPPED
[INFO] servicex-modules-quartz ............................ SKIPPED
[INFO] servicex-modules-file .............................. SKIPPED
[INFO] servicex-common-word ............................... SKIPPED
[INFO] servicex-mall ...................................... SKIPPED
[INFO] servicex-yqx ....................................... SKIPPED
[INFO] servicex-test ...................................... SKIPPED
[INFO] servicex-test-config ............................... SKIPPED
[INFO] servicex-test-datasource ........................... SKIPPED
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  2.433 s
[INFO] Finished at: 2023-06-14T10:05:55+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] No plugin found for prefix 'spring-boot' in the current project and in the plugin groups [org.apache.maven.plugins, org.codehaus.mojo] available from the repositories [local (D:\99-M2\repository), aliyun (http://maven.aliyun.com/nexus/content/groups/public/)] -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/NoPluginFoundForPrefixException
(3) Reasons
# 由于pom.xml文件里缺少了如下的parent依赖,那么就需要在项目中手动添加如下依赖。
<parent>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-parent</artifactId>
   <version>2.4.2</version>
</parent>

(1) The parent project of the project depends on spring-boot-starter-parent, and this error will not occur in this case;
(2) The parent project of the project is not spring-boot-starter-parent, but a certain level of parent project The parent project is spring-boot-starter-parent, as above.
(3) The parent project of the project is not spring-boot-starter-parent, and there is no parent project of a certain level of parent project is spring-boot-starter-parent, then you need to check whether the project contains spring-boot-maven- plugin plugin (if it is not included in this project, it needs to be added manually).

(4) Doubt

(1) mvn clean package spring-boot:repackage, can this command be executed on the command line?
Answer: You can use this command to perform packaging in a single project. However, in a multi-layer project or even a multi-module project, it is generally recommended to use: mvn clean package directly for packaging.

Guess you like

Origin blog.csdn.net/goodjava2007/article/details/126518683