1. 程式人生 > >【日常工作】maven構建多模組專案依賴問題——dependencyManagement、dependencies

【日常工作】maven構建多模組專案依賴問題——dependencyManagement、dependencies

環境:
MacBook Pro 15
jdk8
IntelliJ IDEA

引子
對於maven多模組專案,自然會想到每個模組都有自己的pom檔案,每個模組都可以在自己的pom中定義自己需要的dependency;很容易想到,多個pom檔案中的dependency難免會有重複的(兩個模組都依賴同一個dependency很常見),而且每個dependency都有version需要管理,也可能出現衝突不一致等配置問題。
父子模組間單純的繼承關係也有問題,父模組中如果配置很多dependency,繼承到不同的子模組中可能匯冗餘(因為不同模組可能分開部署,冗餘無用的jar,怎麼能忍!!!)。
如何充分利用模組之間的關係,而又清晰、方便管理不同模組中的dependency?

為解決引子中的問題:maven引入了幾個標籤(設計開發框架的人都是人精)
1. parent:模組間根據這個標籤進行繼承關係。
2. dependency:管理依賴jar,父模組(parent)中dependency預設,繼承到子模組中。
3. dependencyManagement:統一管理dependency中的version,而且預設不繼承;但是管理子模組中相同dependency的version(即子模組,配置相同的dependency不用顯示配置version)。

研究問題1-4:maven構建多模組專案,模組間的依賴關係;

  1. 子模組可以用parent繼承父模組,dependencies中的是每個dependency也自動繼承?
  2. 父模組使用dependencyManagement管理dependencies中的dependency的version,即當子模組中引用重複(父模組已經引入)的dependency時,可以不用帶version控制?方便版本管理?。
  3. dependencyManagement中管理的dependencies中的dependency是否也可以自動繼承?
  4. 繼承是否有傳遞性?即子模組Sa繼承自父模組P,孫子模組Sb繼承自子模組Sa,則Sb是否會自動繼承P中的dependency?dependencyManagement管理的version又是啥情況?

搞明白上邊的幾個問題,先理解一個2個標籤下dependency的狀態:
1. dependencyManagement標籤:沒有特別標註(specified)情況下,子模組預設繼承的dependency的資訊(information)是父模組dependencyManagement下管理的,包括版本-version和值-values。
2. dependencies標籤:管理依賴(dependency)的列表(list)。
參考

官網-maven.apache.org-3.0.3-maven-model

Element Type Description
dependencyManagement DependencyManagement Default dependency information for projects that inherit from this one. The dependencies in this section are not immediately resolved. Instead, when a POM derived from this one declares a dependency described by a matching groupId and artifactId, the version and other values from this section are used for that dependency if they were not already specified.
ependencies/dependency* List(Dependency) (Many) This element describes all of the dependencies associated with a project. These dependencies are used to construct a classpath for your project during the build process. They are automatically downloaded from the repositories defined in this project. See the dependency mechanism for more information.

栗子說明:
問題1答案:父模組中dependencies內的dependency可以自動繼承到子模組中;但是dependencyManagement標籤下的dependencies中的dependency不會自動繼承到子模組,子模組需要顯示引用dependency的groupId和artifactId,version則可選(建議放在父模組中管理)。

問題2答案:是的,父模組dependencyManagement主要管理dependencies中的dependency的資訊(包括version和values)。

問題3答案:不是,父模組dependencyManagement管理的dependencies中的dependency不會再子模組預設繼承,子模組繼承,需要顯示定義dependencies中的具體dependency的groupId和artifactId(version可選,建議在父模組管理)。
這樣父模組既可以統一管理dependency的版本,避免jar包衝突;子模組也可以自由選擇,不會冗餘載入子模組用不到的jar。

參考:

相關推薦

日常工作maven構建模組專案依賴問題——dependencyManagementdependencies

環境: MacBook Pro 15 jdk8 IntelliJ IDEA 引子 對於maven多模組專案,自然會想到每個模組都有自己的pom檔案,每個模組都可以在自己的pom中定義自己需要的dependency;很容易想到,多個pom

Maven構建模組專案

在平時的Javaweb專案開發中為了便於後期的維護,我們一般會進行分層開發,最常見的就是分為domain(域模型層)、dao(資料庫訪問層)、service(業務邏輯層)、web(表現層),這樣分層之後,各個層之間的職責會比較明確,後期維護起來也相對比較容易,今天我們就是使用

Maven學習總結(4)-使用Maven構建模組專案

我用的IDE是eclipse,以下是多模組專案構建步驟。 一、新建Maven Project 1.New-Maven Project-Next 2.操作如下 3.如下填寫,然後Finish 說明:maven中jar、war、pom的區別

Maven學習總結——IDEA中使用Maven構建模組專案

  在平時的Javaweb專案開發中為了便於後期的維護,我們一般會進行分層開發,最常見的就是分為domain(域模型層)、dao(資料庫訪問層)、service(業務邏輯層)、web(表現層),這樣分層之後,各個層之間的職責會比較明確,後期維護起來也相對比較容易,今天我們就是使用Maven來構建以上的各個層

使用maven構建模組專案

①首先建立一個springboot專案 ②再new一個新的模組(新模組依然是springboot專案,同理可以繼續建立需要的新模組) 目錄結構如下圖所示 ③構建模組的依賴關係,模組之間的依賴如下圖所示 在父模組的pom檔案中新增專案的所有依賴,子模組可以繼

使用maven構建模組專案(二)

1. 使用dependencyManagement管理依賴 dependencyManagement的作用:Maven使用dependencyManagement元素來提供一種管理依賴版本號的方法。 (1)helloweb-parent——>pom

使用maven構建模組專案(一)

1. 建立helloweb專案的骨架結構 (1)建立一個maven專案helloweb,刪除target資料夾,如下圖所示 (2)在helloweb目錄下,建立以下4個資料夾 a. helloweb-parent b. helloweb-en

maven構建模組專案的兩種方式

maven構建多模組父子專案有兩種方式,一種通過Eclipse工具去建立,另一種在可以在控制檯通過maven指令建立專案。下面舉例一步步構建下面的JavaWeb專案 project-parent             |----pom.xml             |-

Maven構建模組Springboot專案

1 建立空專案 (groupId=com.boomsecret artifactId=boomsecret-java) mvn archetype:generate -DgroupId=com.boomsecret -DartifactId=boomsecret-java -Darc

maven 單獨構建模組專案中的單個模組

最近在公司的一個專案裡用到了maven的多模組,由於專案比較大,每次修改後構建整個工程耗時太久,需要單獨構建某個模組,mvn有支援的選項: -pl, --projects Build specified reactor projects instead

Maven反應堆 模組專案指定構建某個模組

在預設情況下,Maven會根據多模組配置構建所有的模組,Maven還會根據模組間的依賴關係自動計算構建順序,以確保被依賴的模組會先得以構建。值得一提的是,在這種情形下,Maven會將父模組看成是其子模組的依賴 Maven提供了很多命令列選項讓我們自定義反應堆,

Maven構建模組Java工程

概述 專案開發時,通常會將專案分為多個模組進行開發,本文討論如何用Maven構建多模組的Java工程。 軟體環境 Java:1.6.0_26 Maven:3.1.1 OS:WindowXP SP3 專案模組說明 示例工程分為多個模組,分別是: research_mav

Springboot+Gradle+Docker構建模組專案

文章目錄 背景 引入子模組時exclude失效 Gradle Docker plugin推薦 Docker打包SpringBoot多模組專案 gradle構建的jar包找不到main入口 背景 原有Springboot基

Spring+Spring MVC+Mybatis+Maven搭建模組專案

maven多模組專案SpringMVC簡單例項:劃分多模組,也就是方便多人開發,自己開發自己的那塊沒有多大沖突。 專案結構 整個專案目錄是這樣的: GitHub地址 :https://github.com/thecattle/maven_model —-

idea使用maven搭建模組專案+整合spring security安全框架

我的專案結構: 1.使用idea的maven的quickstart構建父專案 接下來輸入:groupId填com.imooc.security        ArtifactId填imooc-security-parent 刪除它自建的src目錄,並將pom

Spring+Spring MVC+Mybatis+Maven搭建模組專案(二)

基於第一篇文章《Spring+Spring MVC+Mybatis+Maven搭建多模組專案(一)》的基礎上,寫一個完整的示例,從頁面到Dao層的整個過程 1、先在bug.model模組下建立com.bug.model.user包,在包中建立UserVO物件

Spring+Spring MVC+Mybatis+Maven搭建模組專案(一)

最近在研究Spring MVC和Maven,工作中也是使用Spring MVC、Mybatis及Maven整合,出於好奇,自己也搭建了一個Spring+Spring MVC+Mybatis+Maven的多模組框架,先介紹一下我的工程結構 bug.root:根模

IDEA中使用springBoot+gradle構建模組專案

最近專案中用到了springboot+gradle來構建多模組專案,寫這篇文章純當作筆記吧,同時也分享給大家。 gradle和maven是目前很便捷的兩款專案構建工具,都有各自的優勢。據官方統計,gradle比maven構建專案時快100倍以上。廢話不多說,下面介紹一下如何

理解 maven 模組專案依賴關係

語言功底差,直接上程式碼。然後再解釋1。父pom<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:

Spring Boot2企業版快速開發平臺ALBase(2): 使用Maven建立模組專案

系統模組劃分 Maven多模組專案,適用於一些比較大的專案,通過合理的模組拆分,實現程式碼的複用,便於維護和管理。尤其是一些開源框架,也是採用多模組的方式,提供外掛整合,使用者可以根據需要配置指定的模組。   專案結構如下:     albase   (父