maven 之dependencyManagement 和 pluginManagement

一个大中型Java项目中,一般由若干个module组成,各个module各司其职,担任整个工程中不同角色。大多数情况下,大多数module都会用到相同的jar包,或者插件。如果每个module中都引入自己喜欢的jar、插件,不仅冗余,而且太多冗余jar包,使得资源浪费。为了统一管理各个module中的library jar plugin,让所有子module都继承一个父pom.xml。有同学说,既然有了继承,我就直接在pom.xml   的 dependencies中声明module中需要的library,不就好了!想想如果有一些module中不需要这样的library,岂不是又要引进冗余jar。而dependencyManagement正好解决了这一问题,他在父pom.xml中,不会直接到repository中解析你定义的依赖,而是在子module中,如果你用到了dependencyManagement中声明的dependency包,这时你只需要声明包的groupid, artifactid,即可,因为dependencyManagement已经替你定义好了版本version,从而既实现了版本统一管理,又可以各取所需!

dependencyManagement用于在父项目中统一对子项目依赖管理,在pom的project节点中定义如下(举例):

[html] view plain copy
 
  1. <dependencyManagement>  
  2.  <dependencies>  
  3.   
  4.  <dependency>  
  5.             <groupId>org.slf4j</groupId>  
  6.             <artifactId>slf4j-api</artifactId>  
  7.             <version>1.7.12</version>  
  8.         </dependency>  
  9.   
  10.         <dependency>  
  11.             <groupId>ch.qos.logback</groupId>  
  12.             <artifactId>logback-core</artifactId>  
  13.             <version>1.1.3</version>  
  14.         </dependency>  
[html] view plain copy
 
  1. <span style="white-space:pre">    </span><dependency>  
  2.       <groupId>junit</groupId>  
  3.       <artifactId>junit</artifactId>  
  4.       <version>4.11</version>  
  5.       <scope>test</scope>  
  6.     </dependency>  
[html] view plain copy
 
  1.         <dependency>  
  2.             <groupId>ch.qos.logback</groupId>  
  3.             <artifactId>logback-classic</artifactId>  
  4.             <version>1.1.3</version>  
  5.         </dependency>  
  6.   
  7.   </dependencies>  
  8. </dependencyManagement>  


maven之 pluginManagement:

其实,它和dependencyManagement有相似功能,即:由父pom.xml定义该元素,可统一子项目中的插件。引用stackoverflow的问题帖子:

http://stackoverflow.com/questions/10483180/maven-what-is-pluginmanagement

用自己的理解翻译,ps:读者可自行翻译

在你的build中,pluginManagement仅仅是一种在所有module中分享相同插件配置的一种方式。

From Maven document:

pluginManagement: 它是一个被视作插件的一个元素。在很多module中,Plugin Management 以相同方式包含插件元素,而不是为某个特定项目构建(build)配置插件信息

它是用来配置从它继承的项目构建(build)。可是,这仅仅是配置在子module的plugins中确实引用到的插件。每个子module都有权利去覆盖PluginManagement 的定义。

原文地址:https://www.cnblogs.com/soundcode/p/6595246.html