原文网址:maven--插件的管理(pluginManagement)_IT利刃出鞘的博客-CSDN博客
简介
说明
本文介绍maven如何使用pluginManagement来管理插件(build标签中的plugins标签)。
概述
Maven 使用 dependencyManagement 对依赖进行管理,见:这里,与之类似地,Maven 中还提供了一个名为 pluginManagement 的元素,它可以帮助用户管理 Maven 插件。
pluginManagement 元素与 dependencyManagement 元素的原理十分相似,在 pluginManagement 元素中可以声明插件及插件配置,但不会发生实际的插件调用行为,只有在 POM 中配置了真正的 plugin 元素,且其 groupId 和 artifactId 与 pluginManagement 元素中配置的插件匹配时,pluginManagement 元素的配置才会影响到实际的插件行为。
示例
假如存在两个项目,项目A为公共项目,项目B将其作为父项目(项目B指定parent为项目A),其关系通过pom文件的关系确定。
项目A的pom.xml
关键部分
<build><pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.4.13</version></plugin></plugins></pluginManagement>
</build>
整个XML
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.13</version><relativePath/></parent><groupId>com.knife.example</groupId><artifactId>common-parent</artifactId><version>0.0.1-SNAPSHOT</version><packaging>pom</packaging><name>common-parent</name><description>Demo project for Spring Boot</description><dependencies></dependencies><build><pluginManagement><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.4.13</version></plugin></plugins></pluginManagement></build></project>
项目B的pom.xml
关键部分
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins>
</build>
整个pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.knife.example</groupId><artifactId>common-parent</artifactId><version>0.0.1-SNAPSHOT</version><relativePath/></parent><groupId>com.knife.example</groupId><artifactId>order</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>order</name><description>Demo project for Spring Boot</description><dependencies></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
可以看到,子pom文件中,省去了版本、配置细节等信息,只需要指定groupId和artifactId,其他信息均从父pom文件继承。
当然,如果子pom文件想定制自己的特定内容,可以另行设置,并会覆盖从父pom文件继承到的内容。