Maven-13依赖管理

http://blog.csdn.net/gudong2945/article/details/6733688

Maven-13依赖管理
分类: Maven 2011-08-30 20:27 406人阅读 评论(0) 收藏 举报
maven
Maven提供dependencyManagement元素既能让子模块继承到父模块的依赖配置,又能保证自模块依赖的使用灵活性。(有选择的继承)
可在accout-parent中加入:

[html] view plaincopy
<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/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> 

子模块继承代码如下:
[html] view plaincopy
<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/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的设置。除了复制,还可以导入。如下:
[html] view plaincopy
<dependencyManagement> 
    <dependencies> 
        <dependency> 
            <groupId>com.juvenxu.mvnbook.account</groupId> 
            <artifactId>account-parent</artifactId> 
            <version>1.0-SNAPSHOT</version> 
            <type><span style="color:#ff0000;">pom</span></type> 
            <scope><span style="color:#ff0000;">import</span></scope> 
        </dependency> 
    </dependencies> 
</dependencyManagement> 

猜你喜欢

转载自xingmeng85.iteye.com/blog/1991877