DependencyManagement怎么用?
日期: 2020-06-25 分类: 跨站数据测试 331次阅读
DependencyManagement怎么用?
当我们在使用maven的聚合工程的时候, 通常可以在父类统一管理项目所需要的依赖,这样可以保证我们项目中使用的依赖的版本是一致的。
其中父项目管理依赖通常有两种方式,一种是在dependencies
中声明项目所需要的依赖,这样当子项目继承父项目的时候,他就会继承父项目dependencies中声明的所有依赖
父pom:
<packaging>pom</packaging>
<properties>
<fastjson-version>1.2.70</fastjson-version>
</properties>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
</dependencies>
另外一种是在父项目中使用dependencyManagement
来管理项目中的依赖,然后子项目继承父项目的时候,按需引入,当需要用什么依赖的时候,需要显示的写上,并且不带上版本号,这样才表示使用的是在父项目中的依赖.不写上的,则无法引入。
父pom:
<properties>
<fastjson-version>1.2.70</fastjson-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
</dependencies>
</dependencyManagement>
使用dependencyManagement创建springboot父工程
当在dependencyManagement中引入一个packaging=pom的工程时,需要指定type=pom,scope=import,然后子工程就可以按需引入了
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
<modules>
<module>son1</module>
</modules>
<packaging>pom</packaging>
<properties>
<fastjson-version>1.2.70</fastjson-version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson-version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.1.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
平时我们创建Springboot项目,直接就继承一个spring-boot-starter-parent, parent中他其实是继承spring-boot-dependencies,然后在spring-boot-dependencies中有一个
<!--我们创建的项目继承的父亲 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<!--spring-boot-starter-parent继承的父亲 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.1.5.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
<!--spring-boot-dependencies工程 -->
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-amqp</artifactId>
<version>${activemq.version}</version>
</dependency>
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-blueprint</artifactId>
<version>${activemq.version}</version>
</dependency>
</dependencies>
<dependencyManagement>
除特别声明,本站所有文章均为原创,如需转载请以超级链接形式注明出处:SmartCat's Blog
标签:maven maven
精华推荐