<?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"> <dependencyManagement> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-grpclb</artifactId> <version>1.17.1</version> </dependency> </dependencyManagement> <dependencies> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-grpclb</artifactId> </dependency> </dependencies> </project>甚至,我们可以在 <dependencyManagement> 中对依赖进行配置——例如排除某些依赖,这些后续在 <dependencies> 使用的时候,也可以不必再次指定。
<build> <pluginManagement> <plugins> <plugin> <groupId>org.jacoco</groupId> <artifactId>jacoco-maven-plugin</artifactId> <version>0.8.5</version> </plugin> </plugins> </pluginManagement> </build>同样类似于<dependencyManagement>,我们在 <pluginManagement> 下对 plugin 进行的配置,在后续使用的时候也不必再次进行设置。
<?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"> <properties> <grpc-version>1.17.1</grpc-version> </properties> <dependencyManagement> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-netty</artifactId> <version>${grpc-version}</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>${grpc-version}</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-grpclb</artifactId> <version>${grpc-version}</version> </dependency> </dependencyManagement> </project>这对于统一管理 maven 依赖的版本号非常有用,而且在下面提到的 pom 继承中,还可以在子 pom 中重写版本号。
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.8</version> <relativePath/> </parent>而在 dependencies 中,我们可以看到所有的依赖均没有指定版本号:
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>这些依赖的版本号,其实都在 spring-boot-starter-parent 的 parent spring-boot-dependencies 中指定了:
war
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.7.8</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>关于 scope,有以下值:
import:只使用在 dependencyManagement 中,表示从其他的 pom 中导入 dependency 的配置
D:\idea\project-name | pom.xml # parent | +---module-1 | | pom.xml | +---src | | .... | | +---module-2 | | pom.xml | +---src | | .... | |根目录下的 pom 文件,我们设置 <packaging>pom</packaging> ,并指定 parent,例如 spring-boot-starter-parent,以及在 dependencyManagement 中指定我们项目中使用的依赖版本、在 pluginManagement 中指定我们项目中使用的插件配置。