maven中dependencyManagement标签的简单使用(import scope依赖方式)

《maven应用实战》中描述的比较到位:

这里有个比较特别的元素,即dependencyManagement元素。根据前面的简介可以知道它是依赖管理元素,也就是说,用来管理依赖的。因为在实际项目中它有特殊意义,而且能够被继承。
一个Maven项目要直接引用某个依赖,都是在dependencies中使用dependency描述要引用依赖的坐标信息来完成的。这样就达到了一个要什么,就直接写什么的效果,决定权都在是否用dependency指定了引用构件的坐标。但是在实际项目管理过程中可以有个全局的管理。也就是说,把整个项目要引用的依赖,事先分析整理好,形成一个全局的集合。当某个Maven模块需要具体引用某依赖的时候,直接在集合中指定若干个。这样就可以实现整个项目依赖的全局管理,不至于零碎地分布在每个Maven模块中。
基于这样的考虑,就可以在一个公共的pom.xml中使用dependencyManagement元素,将所有的依赖都声明管理好,如以下代码所示。


 在项目中如果要使用前面在SpringPOM中用dependencyManagement管理的spring-core构件的话,只需继承SpringPOM,然后在用户自己的dependencies中指明spring-core就行,代码如下:

注意粗体部分,dependency中是没有指定version的,而且还不用配置scope,因为这些信息已经在SpringPOM中配置好了。这里只要通过groupId和artifactId指明是在dependencyManagement中配置的哪个,其他就自动明确了。虽然使用这种依赖管理机制不能减少太多的pom配置,不过还是建议用户使用这种方式。其原因是:在父pom中使用dependencyManagement声明依赖统一管理了项目中使用到的依赖种类、版本,每个子项目就不会出现额外的多余依赖,特别是没有优化的依赖和同一个构件的不同版本了。
当然了,在子项目中,除了可以用复制、继承的方式,将定义在父项目中的dependency-Management中管理的构件信息合并到当前Maven工程中来以外,也可以用依赖作用访问import,将它们合并过来,样例代码如下:

具体实现过程如下:

1.配置版本管理pom.xml

<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/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.test</groupId>
    <artifactId>test-parent</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>pom</packaging>
    <!-- 集中定义依赖版本号 -->
    <properties>
        <junit.version>4.12</junit.version>
        <spring.version>4.1.3.RELEASE</spring.version>
        <mybatis.version>3.2.8</mybatis.version>
        <mybatis.spring.version>1.2.2</mybatis.spring.version>
        <mybatis.paginator.version>1.2.15</mybatis.paginator.version>
        <mysql.version>5.1.32</mysql.version>
        <slf4j.version>1.6.4</slf4j.version>
        <jackson.version>2.4.2</jackson.version>
        <druid.version>1.0.9</druid.version>
    </properties>
    <dependencyManagement>
        <dependencies>
            <!-- 连接池 -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <version>${druid.version}</version>
            </dependency>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
                <version>${spring.version}</version>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
                <version>${spring.version}</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <!-- 资源文件拷贝插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-resources-plugin</artifactId>
                <version>2.7</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <!-- java编译插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.2</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
        </plugins>
        <pluginManagement>
            <plugins>
                <!-- 配置Tomcat插件 -->
                <plugin>
                    <groupId>org.apache.tomcat.maven</groupId>
                    <artifactId>tomcat7-maven-plugin</artifactId>
                    <version>2.2</version>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

2.在实现文件中增加:

<dependencyManagement><!--如果是多模块的,可以配置在parent级pom.xml中-->
	<dependencies>
		<dependency>
			<groupId>com.test.sample</groupId>
			<artifactid>base-parent1</artifactId>
			<version>1.0.0-SNAPSHOT</version>
			<type>pom</type>
			<scope>import</scope>
		</dependency>
	</dependencies>
</dependencyManagement>
<!--配置依赖时不需要配置版本号,由dependencyManagement统一管理,便于维护-->

 <!--如果代码中没有配置依赖,则不会拉取jar-->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>druid</artifactId>
                <!--<version>1.0.8</version>-->
<!--如果配置了版本号,就不会使用dependencyManagement定义的版本号-->
            </dependency>
            <!-- Spring -->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-context</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-webmvc</artifactId>
            </dependency>

        dependencyManagement 和 dependencies的区别,如果在父类中定义了dependencies,子类继承的时候回自动产生依赖,而dependencyManagement 中的东西就像是依赖的声明,子模块想要继承还需要在自己的pom文件中进行添加。

注意:

1、需要maven2.9以上版本。

2、将dependency分类,每一类建立单独的pom文件

3、在需要使用到这些依赖的子model中,使用dependencyManagement管理依赖,并import scope依赖

3、scope=import只能用在dependencyManagement里面,且仅用于type=pom的dependency

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值