:maven依赖管理

原文地址:http://chenzhou123520.iteye.com/blog/1582171

Maven提供dependencyManagement 元素既能让子模块继承到父模块的依赖配置,又能保证自模块依赖的使用灵活性。(有选择的继承)
可在accout-parent 中加入:
Xml代码:
<project xmlnsxmlns="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.juvenxu.mvnbook.account</groupId>  
    <artifactId>account-parent</artifactId>  
    <version>1.0.0-SNAPSHOT</version>  
    <packaging>pom</packaging>  
    <name>Account Parent</name>  
    <properties>  
        <springframework.version>2.5.6</springframework.version>  
        <junit.version>4.7</junit.version>  
    </properties>  
    < dependencyManagement >  
        <dependencies>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-core</artifactId>  
                <version>${springframework.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-beans</artifactId>  
                <version>${springframework.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context</artifactId>  
                <version>${springframework.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>org.springframework</groupId>  
                <artifactId>spring-context-support</artifactId>  
                <version>${springframework.version}</version>  
            </dependency>  
            <dependency>  
                <groupId>junit</groupId>  
                <artifactId>junit</artifactId>  
                <version>${junit.version}</version>  
                <scope>test</scope>  
            </dependency>  
        </dependencies>  
    </ dependencyManagement >  
</project>  


子模块继承代码如下:

Xml代码  
<project xmlnsxmlns="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>  
      
    < parent >  
        <groupId>com.juvenxu.mvnbook.account</groupId>  
        <artifactId> account-parent </artifactId>  
        <version>1.0.0-SNAPSHOT</version>  
    </ parent >  
      
    <artifactId>account-email</artifactId>  
    <name>Account Email</name>  
  
    <properties>  
        <javax.mail.version>1.4.1</javax.mail.version>  
        <greenmail.version>1.3.1b</greenmail.version>  
    </properties>  
  
    <dependencies>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-core</artifactId>                               
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-beans</artifactId>                                            
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context</artifactId>                                         
        </dependency>  
        <dependency>  
            <groupId>org.springframework</groupId>  
            <artifactId>spring-context-support</artifactId>                                         
        </dependency>  
        <dependency>  
            <groupId>junit</groupId>  
            <artifactId>junit</artifactId>  
        </dependency>  
        <dependency>  
            <groupId>javax.mail</groupId>  
            <artifactId>mail</artifactId>  
            <version>${javax.mail.version}</version>  
        </dependency>                     
        <dependency>  
            <groupId>com.icegreen</groupId>  
            <artifactId>greenmail</artifactId>  
            <version>${greenmail.version}</version>  
            <scope>test</scope>  
        </dependency>  
    </dependencies>  
  
    <build>  
    </build>  
  
</project>  


使用继承管理依赖 (dependencyManagement)带来的好处
1、代码量减少,不用多次重复声明 version等元素。
2、version受父管理,父变则子变,便于修改。
3、dependencyManagement一旦定义,可以复用。


例如想要在另外一个模块中使用如父模块中一样的 dependencyManagement的设置。除了复制,还可以导入。如下:
<dependencyManagement>  
    <dependencies>  
        <dependency>  
            <groupId>com.juvenxu.mvnbook.account</groupId>  
            <artifactId> account-parent </artifactId>  
            <version>1.0-SNAPSHOT</version>  
            <type>pom</type>  
            <scope>import</scope>  
        </dependency>  
    </dependencies>  
</dependencyManagement>  

你可能感兴趣的:(java,maven)