1. 程式人生 > >maven中dependencyManagement標籤的簡單使用(import scope依賴方式)

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