Maven中<dependencies>节点和<dependencyManagement>节点的区别

本文详细解释了如何使用Maven项目继承和依赖管理功能来解决在Maven工程中遇到的问题,包括如何在多模块项目中统一依赖版本、避免重复配置依赖,以及如何通过命令查看生效的POM文件。

以前一直没有在意,今天建立maven工程的时候在<dependencyManagement>节点下加入了junit依赖,结果在dependency Graph中没有发现junit的依赖关系,怎么回事?没有加入项目依赖?遂google之

 

得解释:

1 .使用项目继承

利用项目继承可以将结构信息,部署信息,共同的依赖信息放置在单一的位置。在每个工程的 pom 中:

[...]

<parent>

<groupId>org.apache.maven.proficio</groupId>

<artifactId>proficio</artifactId>

<version>1.0-SNAPSHOT</version>

</parent>

[...]

这使得项目的 pom 可以继承顶层 pom 中的定义,检查顶层 pom 的 dependencies 部分:

<project>

[...]

<dependencies >

<dependency >

<groupId>junit</groupId>

<artifactId>junit</artifactId>

<version>3.8.1</version>

<scope>test</scope>

</dependency >

</dependencies >

[...]

</project>

在各个子模块的 pom 中没有对 Junit 依赖的定义,但是从顶层 pom 中继承了依赖的定义。

为了看清楚可以在一个子模块 pom 所在目录下,执行命令

#mvn help:effective-pom

可以看到最终起效果的 pom ,这在找错时很有效。

2 .管理依赖

在 pom 中指明 dependency management 元素的方式 maven 结合项目继承来管理依赖。在多模块应用中,可能多个子项目会有共同的依赖。此时为了正确运行,必须让所有的子项目使用依赖项的同一版本。必须确保应用的各个项目的依赖项和版本一致,才能保证测试的和发布的是相同的成果。因此,应在顶层的 pom 中定义共同的依赖关系。

在 Proficio 应用的顶层 pom 中的 dependency management 段如下:

  <dependencyManagement>

    <dependencies >

      <dependency >

        <groupId>com.devzuz.mvnbook.proficio</groupId>

        <artifactId>proficio-model</artifactId>

        <c>${project.version}</version>

      </dependency >

      <dependency >

        <groupId>com.devzuz.mvnbook.proficio</groupId>

        <artifactId>proficio-api</artifactId>

        <version>${project.version}</version>

      </dependency >

      <dependency >

        <groupId>com.devzuz.mvnbook.proficio</groupId>

        <artifactId>proficio-core</artifactId>

         <version>${project.version}</version>

      </dependency >

      <dependency >

        <groupId>com.devzuz.mvnbook.proficio</groupId>

        <artifactId>proficio-store-memory</artifactId>

        <version>${project.version}</version>

      </dependency >

      <dependency >

        <groupId>com.devzuz.mvnbook.proficio</groupId>

        <artifactId>proficio-store-xstream</artifactId>

        <version>${project.version}</version>

      </dependency >

      <dependency >

        <groupId>org.codehaus.plexus</groupId>

        <artifactId>plexus-container-default</artifactId>

        <version>1.0-alpha-9</version>

      </dependency >

    </dependencies >

  </dependencyManagement>

注意 ${project.version} 变量指的是应用的 version 。

顶层 pom 中的 dependencies 与 dependencyManagement 中的 dependencies 元素有一个重要的区别:

dependencyManagement 中的 dependencies 元素只表明依赖项版本的优先选择,并不影响项目的依赖项;而dependencies 元素则影响项目的依赖项。

检查 Proficio api 模块的 pom:

<project>

  <parent>

    <groupId>com.devzuz.mvnbook.proficio</groupId>

    <artifactId>proficio</artifactId>

    <version>1.0-SNAPSHOT</version>

  </parent>

  <modelVersion>4.0.0</modelVersion>

  <artifactId>proficio-api</artifactId>

  <packaging>jar</packaging>

  <name>Proficio API</name>

  <dependencies >

    <dependency >

      <groupId>com.devzuz.mvnbook.proficio</groupId>

       <artifactId>proficio-model</artifactId>

    </dependency >

  </dependencies >

</project>

其中没有指明依赖项的版本信息,在顶层 pom 中的 dependencyManagement 中表明其优选的版本是${project.version} 即 1.0-SNAPSHOT 。为使其模块 pom 完整,其版本信息会注入其中。只有当 dependencies 元素中没有指明版本信息时, dependencyManagement 中的 dependencies 元素才起作用。

 

 

 

 

呵呵  一个是项目依赖,一个是多模块maven项目时候的依赖管理控制的.

blog的pom文件如下 <?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>3.2.5</version> </parent> <groupId>com.glaze.blog</groupId> <artifactId>blog</artifactId> <version>${revision}</version> <packaging>pom</packaging> <name>blog</name> <description>blog</description> <modules> <module>blog-dao</module> <module>blog-service</module> <module>blog-api</module> </modules> <properties> <java.version>17</java.version> <blog.version>0.1.0.SNAPSHOT</blog.version> <revision>${blog.version}</revision> <project_id>blog</project_id> </properties> <dependencyManagement> <dependencies> <dependency> <groupId>com.glaze.blog</groupId> <artifactId>blog-dao</artifactId> <version>${revision}</version> </dependency> <dependency> <groupId>com.glaze.blog</groupId> <artifactId>blog-api</artifactId> <version>${revision}</version> </dependency> <dependency> <groupId>com.glaze.blog</groupId> <artifactId>blog-service</artifactId> <version>${revision}</version> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-websocket</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-mail</artifactId> </dependency> <!-- 前后端分离 权限验证 --> <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>4.4.0</version> </dependency> <dependency> <groupId>io.jsonwebtoken</groupId> <artifactId>jjwt</artifactId> <version>0.12.3</version> </dependency> <!-- 数据库相关 开始.... --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-boot-starter</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.mybatis</groupId> <artifactId>mybatis-spring</artifactId> <version>3.0.3</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid-spring-boot-starter</artifactId> <version>1.2.20</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper-spring-boot-starter</artifactId> <version>2.0.0</version> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-generator</artifactId> <version>3.5.4</version> </dependency> <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.32</version> </dependency> <!-- 数据库相关 结束.... --> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.32</version> <optional>true</optional> </dependency> <!-- 数据转换 开始.... --> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct</artifactId> <version>1.5.5.Final</version> </dependency> <dependency> <groupId>org.mapstruct</groupId> <artifactId>mapstruct-processor</artifactId> <version>1.5.5.Final</version> </dependency> <!-- 数据转换 结束.... --> <!-- 二维码 .... --> <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.3.3</version> </dependency> <!-- 工具类 开始.... --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.14.0</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.8.25</version> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>2.0.42</version> </dependency> <!-- 工具类 结束.... --> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId> <version>2.2.0</version> </dependency> <dependency> <groupId>org.springdoc</groupId> <artifactId>springdoc-openapi-starter-webmvc-api</artifactId> <version>2.2.0</version> </dependency> </dependencies> </project>
05-23
<?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> <groupId>com.yjxxt</groupId> <artifactId>demo-02</artifactId> <version>0.0.1-SNAPSHOT</version> <name>demo-02</name> <description>demo-02</description> <properties> <java.version>17</java.version> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <spring-boot.version>3.0.2</spring-boot.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus-spring-boot3-starter</artifactId> <version>3.5.10.1</version> </dependency> <dependency> <groupId>com.mysql</groupId> <artifactId>mysql-connector-j</artifactId> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${spring-boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>17</source> <target>17</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>com.yjxxt.demo02.Demo02Application</mainClass> <skip>true</skip> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>详细解释一下这个pom文件加上注释给我
03-13
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值