背景:最近在学习maven的多模块构建过程中看到DependencyManagement选项,对这个选项的使用做个记录!

区别与联系

这里介绍一个在父项目中的根结点中声明dependencyManagement和dependencies的区别

dependencyManagement使用

dependencyManagement只会影响现有依赖的配置,但不会引入依赖。

例如我们可以在父模块中配置如下:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactid>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactid>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
</dependencyManagement>

这段配置不会给任何子模块引入依赖,但如果某个子模块需要使用JUnit和Log4j的时候,我们就可以简化依赖配置成这样:

 <dependency>
<groupId>junit</groupId>
<artifactid>junit</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactid>log4j</artifactId>
</dependency>

现在只需要groupId和artifactId,其它元素如version和scope都能通过继承父POM的dependencyManagement得到,如果有依赖配置了exclusions,那节省的代码就更加可观。但重点不在这,重点在于现在能够保证所有模块使用的JUnit和Log4j依赖配置是一致的。而且子模块仍然可以按需引入依赖,如果我不配置dependency,父模块中dependencyManagement下的spring-aop依赖不会对我产生任何影响。

也许你已经意识到了,在多模块Maven项目中,dependencyManagement几乎是必不可少的,因为只有它是才能够有效地帮我们维护依赖一致性。

dependencies

相对于dependencyManagement,所有声明在dependencies里的依赖都会自动引入,并默认被所有的子项目继承。

classifier

如果你要发布同样的代码,但是由于技术原因需要生成两个单独的构件,你就要使用一个分类器(classifier)。例如,如果你想要构建两个单独的构件成JAR,一个使用Java 1.4 编译器,另一个使用Java 6 编译器,你就可以使用分类器
来生成两个单独的JAR构件,它们有同样的groupId:artifactId:version组合。如果你的项目使用本地扩展类库,你可以使用分类器为每一个目标平台生成一个构件。分类器常用于打包构件的源码,JavaDoc 或者二进制集合。

dependencyManagement精进

我们知道Maven的继承和Java的继承一样,是无法实现多重继承的,如果10个、20个甚至更多模块继承自同一个模块,那么按照我们之前的做法,这个父模块的dependencyManagement会包含大量的依赖。

如果你想把这些依赖分类以更清晰的管理,那就不可能了,import scope依赖能解决这个问题。你可以把dependencyManagement放到单独的专门用来管理依赖的POM中,然后在需要使用依赖的模块中通过import scope依赖,就可以引入dependencyManagement。

例如可以写这样一个用于依赖管理的POM:

<project>
<modelVersion>4.0.0</modelVersion>
<groupId>com.juvenxu.sample</groupId>
<artifactId>sample-dependency-infrastructure</artifactId>
<packaging>pom</packaging>
<version>1.0-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactid>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactid>log4j</artifactId>
<version>1.2.16</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

然后我就可以通过非继承的方式来引入这段依赖管理配置:

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.juvenxu.sample</groupId>
<artifactid>sample-dependency-infrastructure</artifactId>
<version>1.0-SNAPSHOT</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <dependency>
<groupId>junit</groupId>
<artifactid>junit</artifactId>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactid>log4j</artifactId>
</dependency>

这样,父模块的POM就会非常干净,由专门的packaging为pom的POM来管理依赖,也契合的面向对象设计中的单一职责原则。此外,我们还能够创建多个这样的依赖管理POM,以更细化的方式管理依赖。

这种做法与面向对象设计中使用组合而非继承也有点相似的味道。

plugins和pluginmanagement的区别

pluginmanagement标签一般用在父pom中,子元素可以包含plugins插件,比如

<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
<version>2.1</version>
<configuration>
<attach>true</attach>
</configuration>
<executions>
<execution>
<phase>compile</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</pluginManagement>

然后,在子pom文件中就可以这样使用:

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-source-plugin</artifactId>
</plugin>
</plugins>

省去了版本、配置等信息,只需指定groupId和artifactId即可。

但是在父pom中,如果使用这个标签来包裹plugins插件,当在此项目根目录运行对应的mvn命令时,如果在子pom中没有直接像上面再次引用这个plugin,那么不会触发这个plugin插件,只有在子pom中再次引用了之后,才会在对应的子项目路径下触发这个plugin.

plugins和pluginManagement标签都需要在build标签中。

关于插件pluginManagement,Maven并没有提供与import scope依赖类似的方式管理,那我们只能借助继承关系,不过好在一般来说插件配置的数量远没有依赖配置那么多,因此这也不是一个问题。

(转)Maven中的DependencyManagement和pluginmanagement的更多相关文章

  1. 【maven】Maven中的dependencyManagement

    dependencyManagement使用简介 Maven中的dependencyManagement元素提供了一种管理依赖版本号的方式.在dependencyManagement元素中声明所依赖的 ...

  2. Maven中的DependencyManagement和Dependencies

    Maven 使用dependencyManagement 元素来提供了一种管理依赖版本号的方式.通常会在一个组织或者项目的最顶层的父POM 中看到dependencyManagement 元素.使用p ...

  3. Maven中的dependencyManagement 意义

    1.在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 2.pom.xml文件中,jar的版本判断的两种途径 1:如果dependenci ...

  4. 034 Maven中的dependencyManagement和dependencies区别

    这个标签使用过,但是具体的描述还是没有说明过.在这里,专门查了一下,写了这篇文章. 1.定义 在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管 ...

  5. Maven 梳理 - Maven中的dependencyManagement 意义

    1.在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 2.pom.xml文件中,jar的版本判断的两种途径 1:如果dependenci ...

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

    以前一直没有在意,今天建立maven工程的时候在<dependencyManagement>节点下加入了junit依赖,结果在dependency Graph中没有发现junit的依赖关系 ...

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

    dependencyManagement只是插件管理,并不是真正的插件依赖,所以里面包含的插件在没有子项目使用的时候,并不会真正下载 1 .使用项目继承 利用项目继承可以将结构信息,部署信息,共同的依 ...

  8. Maven 中的dependencies与dependencyManagement的区别

    1.dependencyManagement 在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器 在pom.xml文件中,jar的版本判断的 ...

  9. Maven中dependencyManagement使用

    在Maven中dependencyManagement的作用其实相当于一个对所依赖jar包进行版本管理的管理器. 在dependencyManagement下申明的dependencies,Maven ...

随机推荐

  1. Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C-Bracket Subsequence

    #include<iostream> #include<stdio.h> #include<string.h> #include<algorithm> ...

  2. 2016-03-22 OneZero团队 Daily Scrum Meeting

    会议时间: 2016-03-22 9:33-9:57am 会议内容: 一.在原有Sprint Backlog基础上,我们加了亮点(摇一摇功能:随机选取一条记录在界面显示,以提醒主页君回忆) 需求分析图 ...

  3. RedisDump安装报错

    环境:win10 首先安装 Ruby 安装好后,使用命令行 gem install redis-dump 在安装过程中始终报错,意思是无法使用make命令 然后安装make 参考教程: http:// ...

  4. Maven入门系列(一):Eclipse中使用Maven

    Maven下载和安装 在使用Maven之前首先先要下载Mavne的免安装包,下载地址:http://maven.apache.org/download.cgi 想看源码的可以下载src版本,使用的下载 ...

  5. sprint最后冲刺-out to out

    摘要:团队合作.实现四则APP,上传代码到github. 1.之前我们队一直无法把代码上传到github.直到今天.找到了一种可以协助代码上github的软件msysgit. 经过:(一行行看) 我们 ...

  6. The Contest CodeForces - 813A (思维)

    Pasha is participating in a contest on one well-known website. This time he wants to win the contest ...

  7. Maven -Maven配置tomcat插件 两种

    Maven Tomcat插件现在主要有两个版本,tomcat-maven-plugin和tomcat7-maven-plugin,使用方式基本相同. tomcat-maven-plugin 插件官网: ...

  8. 开源的CAS实现SSO

    https://www.ibm.com/developerworks/cn/opensource/os-cn-cas/index.html ISC是基于CAS定制的,使用的高级的代理模式. https ...

  9. Java 线程内 递归 Bug 一例

    一个线程的run方法里使用递归方法,出了Bug. private boolean ispass(String creationId){ List<Map> maps =creationSe ...

  10. linux_目录基本操作

    ls命令 ls命令用来显示目标列表,在Linux中是使用率较高的命令.ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件. 语法 $ ls [选项] [目录] 选项 说明 -a 显示所有档案 ...