maven的依賴、繼承和聚合

一、maven的依賴

1.依賴的傳遞性

A依賴B,B依賴C,那麼A依賴C

好處:可以傳遞,不必在每個模塊工程中都重複聲明,在"最下面"的工程中依賴一次即可

注意:非compile範圍的依賴不能傳遞,所以在各個工程模塊中,如果有需要就得重複聲明依賴

2.依賴的排除

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
    <exclusions>
        <exclusion>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-core</artifactId>
        </exclusion>
    </exclusions>
</dependency>

3.依賴的原則

解決模塊之間jar包衝突問題

  • 路徑最短者優先
  • 路徑相同時先聲明者優先

4.統一管理版本號

1) 使用properties標籤內使用自定義標籤統一聲明版本號

<properties>
    <junit-version>4.12</junit-version>
</properties>

2) 在需要統一版本的位置,使用${自定義標籤名}引用聲明的版本號

<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>${junit-version}</version>
    <scope>test</scope>
</dependency>

二、maven的繼承

目的:統一管理各個模塊工程中依賴的版本

例如:將junit依賴版本統一提取到父工程中,在子工程中聲明junit依賴時不指定版本,以父工程中統一設定爲準,同時也便於修改

步驟:

  1. 創建一個maven工程作爲父工程,注意:打包的方式pom
  2. 在子工程中聲明父工程的引用
  3. 在子工程的座標中與父工程座標中重複的內容刪除
  4. 在父工程中統一junit的依賴
  5. 在子工程中刪除junit依賴的版本號部分

注意: 配置繼承後,執行安裝命令時要先安裝父工程

父工程pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 配置依賴的管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

子工程pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>

    <!-- 子工程中聲明父工程 -->
    <parent>
        <groupId>org.example</groupId>
        <artifactId>parent</artifactId>
        <version>1.0-SNAPSHOT</version>
        <!-- 以當前文件爲基準的父工程pom.xml文件的相對路徑 -->
        <relativePath>../parent/pom.xml</relativePath>
    </parent>

    <artifactId>child</artifactId>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

</project>

三、maven的聚合

作用: 一鍵安裝各個模塊工程
配置方式: 在一個"總的聚合工程"中配置參與聚合的模塊
使用方式: 在聚合工程的pom.xml上 maven install

父工程pom文件

<?xml version="1.0" encoding="UTF-8"?>
<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>org.example</groupId>
    <artifactId>parent</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>pom</packaging>

    <!-- 配置聚合 -->
    <modules>
        <!-- 指定各個子工程相對路徑 -->
        <module>../child</module>
    </modules>

    <!-- 配置依賴的管理 -->
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
            </dependency>
        </dependencies>
    </dependencyManagement>

</project>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章