Maven依赖管理

Maven提供dependencyManagement 元素既能让子模块继承到父模块的依赖配置,又能保证自模块依赖的使用灵活性。(有选择的继承)

可在accout-parent 中加入:

 

Xml代码   收藏代码
  1. <project xmlnsxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>com.juvenxu.mvnbook.account</groupId>  
  5.     <artifactId>account-parent</artifactId>  
  6.     <version>1.0.0-SNAPSHOT</version>  
  7.     <packaging>pom</packaging>  
  8.     <name>Account Parent</name>  
  9.     <properties>  
  10.         <springframework.version>2.5.6</springframework.version>  
  11.         <junit.version>4.7</junit.version>  
  12.     </properties>  
  13.     < dependencyManagement >  
  14.         <dependencies>  
  15.             <dependency>  
  16.                 <groupId>org.springframework</groupId>  
  17.                 <artifactId>spring-core</artifactId>  
  18.                 <version>${springframework.version}</version>  
  19.             </dependency>  
  20.             <dependency>  
  21.                 <groupId>org.springframework</groupId>  
  22.                 <artifactId>spring-beans</artifactId>  
  23.                 <version>${springframework.version}</version>  
  24.             </dependency>  
  25.             <dependency>  
  26.                 <groupId>org.springframework</groupId>  
  27.                 <artifactId>spring-context</artifactId>  
  28.                 <version>${springframework.version}</version>  
  29.             </dependency>  
  30.             <dependency>  
  31.                 <groupId>org.springframework</groupId>  
  32.                 <artifactId>spring-context-support</artifactId>  
  33.                 <version>${springframework.version}</version>  
  34.             </dependency>  
  35.             <dependency>  
  36.                 <groupId>junit</groupId>  
  37.                 <artifactId>junit</artifactId>  
  38.                 <version>${junit.version}</version>  
  39.                 <scope>test</scope>  
  40.             </dependency>  
  41.         </dependencies>  
  42.     </ dependencyManagement >  
  43. </project>  

 子模块继承代码如下:

 

Xml代码   收藏代码
  1. <project xmlnsxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.       
  5.     < parent >  
  6.         <groupId>com.juvenxu.mvnbook.account</groupId>  
  7.         <artifactId> account-parent </artifactId>  
  8.         <version>1.0.0-SNAPSHOT</version>  
  9.     </ parent >  
  10.       
  11.     <artifactId>account-email</artifactId>  
  12.     <name>Account Email</name>  
  13.   
  14.     <properties>  
  15.         <javax.mail.version>1.4.1</javax.mail.version>  
  16.         <greenmail.version>1.3.1b</greenmail.version>  
  17.     </properties>  
  18.   
  19.     <dependencies>  
  20.         <dependency>  
  21.             <groupId>org.springframework</groupId>  
  22.             <artifactId>spring-core</artifactId>                               
  23.         </dependency>  
  24.         <dependency>  
  25.             <groupId>org.springframework</groupId>  
  26.             <artifactId>spring-beans</artifactId>                                            
  27.         </dependency>  
  28.         <dependency>  
  29.             <groupId>org.springframework</groupId>  
  30.             <artifactId>spring-context</artifactId>                                         
  31.         </dependency>  
  32.         <dependency>  
  33.             <groupId>org.springframework</groupId>  
  34.             <artifactId>spring-context-support</artifactId>                                         
  35.         </dependency>  
  36.         <dependency>  
  37.             <groupId>junit</groupId>  
  38.             <artifactId>junit</artifactId>  
  39.         </dependency>  
  40.         <dependency>  
  41.             <groupId>javax.mail</groupId>  
  42.             <artifactId>mail</artifactId>  
  43.             <version>${javax.mail.version}</version>  
  44.         </dependency>                     
  45.         <dependency>  
  46.             <groupId>com.icegreen</groupId>  
  47.             <artifactId>greenmail</artifactId>  
  48.             <version>${greenmail.version}</version>  
  49.             <scope>test</scope>  
  50.         </dependency>  
  51.     </dependencies>  
  52.   
  53.     <build>  
  54.     </build>  
  55.   
  56. </project>  
   使用继承管理依赖 (dependencyManagement)带来的好处

1、代码量减少,不用多次重复声明 version等元素。

2version受父管理,父变则子变,便于修改。

3dependencyManagement一旦定义,可以复用。

例如想要在另外一个模块中使用如父模块中一样的  dependencyManagement 的设置。除了复制,还可以导入。如下:
Xml代码   收藏代码
  1. <dependencyManagement>  
  2.     <dependencies>  
  3.         <dependency>  
  4.             <groupId>com.juvenxu.mvnbook.account</groupId>  
  5.             <artifactId> account-parent </artifactId>  
  6.             <version>1.0-SNAPSHOT</version>  
  7.             <type>pom</type>  
  8.             <scope>import</scope>  
  9.         </dependency>  
  10.     </dependencies>  
  11. </dependencyManagement>  
 

你可能感兴趣的:(java,maven,项目管理,依赖)