maven dependencyManagement的作用

从Maven的继承开始说起:

假设有两个子模块sub-1和sub-2,其pom文件分别如下所示:

<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>cn.codecrazy.demo</groupId>
  <artifactId>demo-sub1</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.31</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
<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>cn.codecrazy.demo</groupId>
  <artifactId>demo-sub2</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

可以看到sub1和sub2中都引入了junit、spring-context和spring-beans依赖。并且sub1中还另外引入了fastjson依赖。我们可以利用maven的继承机制,将这些依赖放到parent父模块中,并在子模块中引入节点。

在sub1和sub2的上层目录中新增一个parent模块,其pom文件如下所示:

<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>cn.codecrazy.demo</groupId>
  <artifactId>demo-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
      <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
      <version>4.3.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
      <version>1.2.31</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

注意节点为pom方式,之后的sub1和sub2的pom文件可简化如下所示:

<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>

  <parent>
    <groupId>cn.codecrazy.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>demo-sub1</artifactId>

</project>
 <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>

  <parent>
    <groupId>cn.codecrazy.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>demo-sub2</artifactId>

</project>

可以看到sub1和sub2模块的pom文件配置大大简化了,由于配置了parent节点,子模块的依赖从父模块得到了继承,即使在子模块中不配置节点,子模块也引入了它的依赖。

但是上述配置存在一个问题,sub2中并不需要fastjson依赖,但由于继承,也同样从父模块中继承了fastjson依赖。换句话说,以后继承自该parent模块的其他子模块也许并不需要parent模块中声明的所有依赖,但是就目前这种配置方式,父模块中的这些依赖都会被无条件的继承,这显然是不合理的,这就需要用到节点。

节点中配置的依赖并不会真正的引入依赖,但是该节点却能够被子模块继承,要在子模块中真正引入依赖,需要将依赖配置在节点下,而这种方式配置的依赖可以省略节点,使用的即是在节点中配置的相应。采用了节点的父模块pom文件如下:

<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>cn.codecrazy.demo</groupId>
  <artifactId>demo-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.31</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

sub1和sub2模块pom文件相应如下所示:

<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>

  <parent>
    <groupId>cn.codecrazy.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>demo-sub1</artifactId>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>fastjson</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>
<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>

  <parent>
    <groupId>cn.codecrazy.demo</groupId>
    <artifactId>demo-parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <relativePath>../pom.xml</relativePath>
  </parent>

  <artifactId>demo-sub2</artifactId>

  <dependencies>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework</groupId>
      <artifactId>spring-beans</artifactId>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

可以看到在sub2的pom文件中没有引入fastjson依赖,这样sub2就不会真正的引入fastjson依赖。

节点的另一个好处是,使得多个子模块中引入的相同依赖版本号能够保持一致。

如何引入另外的dependencyManagement?

假设在某个模块demo-other中也有节点管理着一批依赖,现在想在demo-parent模块中将那些依赖管理全部导入,把demo-other模块中的依赖全部复制到demo-parent模块的节点下是可行但是非常不优雅的做法。

比较优雅的做法是用到import依赖范围,即import。demo-parent的pom文件如下所示:

<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>cn.codecrazy.demo</groupId>
  <artifactId>demo-parent</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>pom</packaging>
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>cn.codecrazy.demo</groupId>
        <artifactId>demo-other</artifactId>
        <version>1.0-SNAPSHOT</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.3.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.3.4.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.31</version>
      </dependency>
      <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
</project>

注意多出来的部分:

<dependency>
    <groupId>cn.codecrazy.demo</groupId>
    <artifactId>demo-other</artifactId>
    <version>1.0-SNAPSHOT</version>
    <type>pom</type>
    <scope>import</scope>
</dependency>
Book Description Manage your Java and JEE project dependencies with ease with this hands-on guide to Maven Overview Improve your productivity by efficiently managing dependencies. Learn how to detect and fix dependency conflicts Learn how to share transitive relations and to visualize your dependencies In Detail Managing dependencies in a multi-module project is difficult. In a multi-module project, libraries need to share transitive relations with each other. Maven eliminates this need by reading the project files of dependencies to figure out their inter-relations and other related information. Gaining an understanding of project dependencies will allow you to fully utilize Maven and use it to your advantage. Aiming to give you a clear understanding of Maven’s functionality, this book focuses on specific case studies that shed light on highly useful Maven features which are often disregarded. The content of this book will help you to replace homebrew processes with more automated solutions. This practical guide focuses on the variety of problems and issues which occur during the conception and development phase, with the aim of making dependency management as effortless and painless as possible. Throughout the course of this book, you will learn how to migrate from non-Maven projects to Maven, learn Maven best practices, and how to simplify the management of multiple projects. The book emphasizes the importance of projects as well as identifying and fixing potential conflicts before they become issues. The later sections of the book introduce you to the methods that you can use to increase your team’s productivity. This book is the perfect guide to help make you into a proud software craftsman. What you will learn from this book Learn how to use profiles, POM, parent POM, and modules Increase build-speed and decrease archive size Set, rationalize, and exclude transitive dependencies Optimize your POM and its dependencies Migrate projects to Maven including projects with exotic dependencies Approach An easy-to-follow, tutorial-based guide with chapters progressing from basic to advanced dependency management. Who this book is written for If you are working with Java or Java EE projects and you want to take advantage of Maven dependency management, then this book is ideal for you. This book is also particularly useful if you are a developer or an architect. You should be well versed with Maven and its basic functionalities if you wish to get the most out of this book. Table of Contents Chapter 1: Basic Dependency Management Chapter 2: Dependency Mechanism and Scopes Chapter 3: Dependency Designation (advanced) Chapter 4: Migration of Dependencies to Apache Maven Chapter 5: Tools within Your IDE Chapter 6: Release and Distribute Appendix: Useful Public Repositories Book Details Title: Apache Maven Dependency Management Author: Jonathan Lalou Length: 158 pages Edition: 1 Language: English Publisher: Packt Publishing Publication Date: 2013-10-25 ISBN-10: 1783283017 ISBN-13: 9781783283019
### MavendependencyManagement 的用法和配置 #### 使用场景 当项目模块众多时,使用 Maven 管理项目变得尤为便捷。这不仅有助于管理构建、文档、报告、依赖关系等方面的工作,还能确保所有子项目使用的依赖项及其版本保持一致,从而保障测试环境与生产环境中的一致性[^1]。 #### 配置方式 在顶层 POM 文件中定义 `dependencyManagement` 元素用于集中管理和声明依赖的版本号。这种方式允许开发者在一个地方设定好所需 jar 包的具体版本信息,使得各子项目只需简单引用这些依赖而不必重复指定其版本号。具体来说: - **父级 POM**: 定义全局性的依赖管理规则。 ```xml <project> ... <dependencyManagement> <dependencies> <!-- 示例:Junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- 更多依赖... --> </dependencies> </dependencyManagement> ... </project> ``` - **子级 POMs**: 继承自父级并利用已定义好的依赖设置 子项目的 pom.xml 可以像下面这样简洁地引入所需的依赖,无需再次指明版本号: ```xml <project> ... <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <!-- 版本由父POM中的dependencyManagement控制 --> </dependency> <!-- 添加其他本地特有的依赖 --> </dependencies> ... </project> ``` 这种机制有效减少了跨多个子模块间可能出现的版本不匹配问题,并简化了整个项目的维护工作量[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值