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

本文介绍Maven中的依赖管理和项目继承技巧,包括如何通过顶级POM文件统一管理子项目的依赖版本,确保所有子项目使用相同的依赖版本。

dependencyManagement只是插件管理,并不是真正的插件依赖,所以里面包含的插件在没有子项目使用的时候,并不会真正下载

1 .使用项目继承

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

 

  1. <parent
  2.  
  3. <groupId>org.apache.maven.proficio</groupId
  4.  
  5. <artifactId>proficio</artifactId
  6.  
  7. <version>1.0-SNAPSHOT</version
  8.  
  9. </parent

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

  1. <project
  2.  
  3. <dependencies 
  4.  
  5. <dependency 
  6.  
  7. <groupId>junit</groupId
  8.  
  9. <artifactId>junit</artifactId
  10.  
  11. <version>4.7</version
  12.  
  13. <scope>test</scope
  14.  
  15. </dependency 
  16.  
  17. </dependencies 
  18.  
  19. </project

 

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

 

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

  1. #mvn help:effective-pom 

 

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

 

2 .管理依赖

 

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

 

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

  1. <dependencyManagement
  2.  
  3.     <dependencies 
  4.  
  5.       <dependency 
  6.  
  7.         <groupId>com.devzuz.mvnbook.proficio</groupId
  8.  
  9.         <artifactId>proficio-model</artifactId
  10.  
  11.         <c>${project.version}</version
  12.  
  13.       </dependency 
  14.  
  15.       <dependency 
  16.  
  17.         <groupId>com.devzuz.mvnbook.proficio</groupId
  18.  
  19.         <artifactId>proficio-api</artifactId
  20.  
  21.         <version>${project.version}</version
  22.  
  23.       </dependency 
  24.  
  25.       <dependency 
  26.  
  27.         <groupId>com.devzuz.mvnbook.proficio</groupId
  28.  
  29.         <artifactId>proficio-core</artifactId
  30.  
  31.          <version>${project.version}</version
  32.  
  33.       </dependency 
  34.  
  35.       <dependency 
  36.  
  37.         <groupId>com.devzuz.mvnbook.proficio</groupId
  38.  
  39.         <artifactId>proficio-store-memory</artifactId
  40.  
  41.         <version>${project.version}</version
  42.  
  43.       </dependency 
  44.  
  45.       <dependency 
  46.  
  47.         <groupId>com.devzuz.mvnbook.proficio</groupId
  48.  
  49.         <artifactId>proficio-store-xstream</artifactId
  50.  
  51.         <version>${project.version}</version
  52.  
  53.       </dependency 
  54.  
  55.       <dependency 
  56.  
  57.         <groupId>org.codehaus.plexus</groupId
  58.  
  59.         <artifactId>plexus-container-default</artifactId
  60.  
  61.         <version>1.0-alpha-9</version
  62.  
  63.       </dependency 
  64.  
  65.     </dependencies 
  66.  
  67.   </dependencyManagement

 

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

 

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

 

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

 

检查 Proficio api 模块的 pom:

  1. <project
  2.  
  3.   <parent
  4.  
  5.     <groupId>com.devzuz.mvnbook.proficio</groupId
  6.  
  7.     <artifactId>proficio</artifactId
  8.  
  9.     <version>1.0-SNAPSHOT</version
  10.  
  11.   </parent
  12.  
  13.   <modelVersion>4.0.0</modelVersion
  14.  
  15.   <artifactId>proficio-api</artifactId
  16.  
  17.   <packaging>jar</packaging
  18.  
  19.   <name>Proficio API</name
  20.  
  21.   <dependencies 
  22.  
  23.     <dependency 
  24.  
  25.       <groupId>com.devzuz.mvnbook.proficio</groupId
  26.  
  27.        <artifactId>proficio-model</artifactId
  28.  
  29.     </dependency 
  30.  
  31.   </dependencies 
  32.  
  33. </project

 

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

 

只有当 外层的dependencies 元素中没有指明版本信息时, dependencyManagement 中的 dependencies 元素才起作用。

 

注意:一个是项目依赖,一个是多模块maven项目时候的依赖管理控制的。

<?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
<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/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.jlwl</groupId> <artifactId>jspm691px</artifactId> <packaging>war</packaging> <version>1.0-SNAPSHOT</version> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring.version>5.0.0.RELEASE</spring.version> <junit.version>4.12</junit.version> <druid.version>1.1.0</druid.version> <fastjson.version>1.2.8</fastjson.version> <mybaitsplus.version>2.3</mybaitsplus.version> <mysql.version>5.1.38</mysql.version> <log4j.version>1.2.17</log4j.version> <slf4j.version>1.7.19</slf4j.version> <aspectjweaver.version>1.8.8</aspectjweaver.version> <fileupload.version>1.3.1</fileupload.version> <jstl.version>1.2</jstl.version> </properties> <dependencies> <!-- JUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>${junit.version}</version> <scope>test</scope> </dependency> <!-- Spring --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-aop</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- Spring MVC --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>${spring.version}</version> <type>jar</type> <scope>compile</scope> </dependency> <!-- AOP --> <dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version>${aspectjweaver.version}</version> </dependency> <!-- FileUpload --> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>${fileupload.version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>${jstl.version}</version> </dependency> <!-- Mybatis-Plus --> <dependency> <groupId>com.baomidou</groupId> <artifactId>mybatis-plus</artifactId> <version>${mybaitsplus.version}</version> </dependency> <!-- Mysql --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>${mysql.version}</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>sqljdbc4</artifactId> <scope>4.0</scope> <version>4.0</version> </dependency> <dependency> <groupId>com.microsoft.sqlserver</groupId> <artifactId>mssql-jdbc</artifactId> <version>6.2.0.jre8</version> <scope>runtime</scope> </dependency> <!-- Druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>${druid.version}</version> </dependency> <!-- FastJson --> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>${fastjson.version}</version> </dependency> <!-- Log --> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>${log4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-log4j12</artifactId> <version>${slf4j.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.0</version> </dependency> <dependency> <groupId>javax.validation</groupId> <artifactId>validation-api</artifactId> <version>2.0.1.Final</version> </dependency> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.5</version> </dependency> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.0.12</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-core --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-core</artifactId> <version>8.5.93</version> </dependency> <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils --> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.0</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.10.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.10.1</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <!-- https://mvnrepository.com/artifact/javax.servlet.jsp/jsp-api --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.1</version> <scope>provided</scope> </dependency> <!-- 百度人工智能 --> <dependency> <groupId>com.baidu.aip</groupId> <artifactId>java-sdk</artifactId> <version>4.4.1</version> </dependency> <!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <version>9.0.27</version> <scope>provided</scope> </dependency> </dependencies> <build> <!-- 项目访问名称 --> <finalName>jspm691px</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <configuration> <source>1.7</source> <target>1.7</target> </configuration> </plugin> </plugins> </build> </project>
最新发布
05-21
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值