[精讚] [會員登入]
216

【Maven】[parent pom]如何使用他人所提供的 parent pom.xml

有時候我們必須使用對方已經放在 Private Maven Repository 上的 pom.xml,那要怎麼使用呢?

分享此文連結 //n.sfs.tw/16129

分享連結 【Maven】[parent pom]如何使用他人所提供的 parent pom.xml@小編過路君子
(文章歡迎轉載,務必尊重版權註明連結來源)
2023-03-25 20:12:54 最後編修
2023-03-25 18:11:18 By 過路君子
 

大家好,這裡是收到一顆日本青森縣產的蘋果的小編過路君子

聽說青森的蘋果很好吃,送給小編蘋果的那個人一直說很好吃很好吃。

 

 

很多時候我們必須跟其他人一起合作撰寫 Java 程式,這時候對方可能就會提供以下的 Maven 資訊:

<groupId>this.is.the.parent.pom</groupId>
<artifactId>dependency-info</artifactId>
<version>3.7.5</version>

 

這時候我們就不能像以前只有自己寫的時候一樣,pom.xml 內想寫什麼就寫什麼。

必須依照對方所提供的 pom.xml 來做設定,特別是 <version> 這個屬性,因為在一個專案內,所使用的套件如果版本不一致可能會對整個專案帶來一些阻力。

所以我們第一步就是將對方的 pom.xml 引用進我們原來的 pom.xml。

<!-- own pom.xml file -->

<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>that.is.our.self.pom</groupId>
	<artifactId>myCode</artifactId>
	<version>1.0.3</version>

	<packaging>jar</packaging>

	<!-- Put here that other side pom.xml dependency information -->
	<parent>
		<groupId>this.is.the.parent.pom</groupId>
		<artifactId>dependency-info</artifactId>
		<version>3.7.5</version>
	</parent>

	<!-- We must add other side repository url, otherwise we can't get the parent pom.xml -->
	<repositories>
		<repository>
			<id>maven-public</id>
			<url>http://127.0.0.1/repository/maven-public/</url>
			<releases>
				<enabled>true</enabled>
			</releases>
			<snapshots>
				<enabled>true</enabled>
			</snapshots>
		</repository>
	</repositories>
</project>

 

這時候的 dependencies 寫法就有兩種,第一種是不用寫,第二種是要寫,但是不提供版本資訊。

至於要採用第一種還是第二種的寫法,還是看 parent pom.xml 如何撰寫,所以我們分別來看一下兩種的差異:

 

第一種:不用寫

這時候的 parent pom.xml 內的  <dependencies> 資訊並沒有任何的特別,例如:

<!-- parent pom.xml -->

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.9.2</version>
    </dependency>
</dependencies>

 

那這時只需要我們像上方的 pom.xml 一樣,以<parent></parent>的方式將對方的 pom.xml 引入到我們的 pom.xml 內,在打包的時候就會自動一起打包進去。

如果使用這種方式就代表寫 parent pom.xml 的人認為每個使用到此 parent pom.xml 的工程師都會用到 dependencies 裡面所有的包,所以就算今天我們的程式內並未使用裡面的包,依舊會被一起打包進去我們的 .jar 或 .war 內。

而且我們還沒辦法阻止,所以容易造成打包的時間過長或是過於冗贅的問題。

 

第二種:要寫,但是不提供版本資訊

這時候的 parent pom.xml 內就跟我們平常在寫 pom.xml 長得不一樣了,如下:

<!-- parent pom.xml -->

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.2</version>
        </dependency>
    </dependencies>
</dependencyManagement>

沒錯,這個多出來的<dependencyManagement></dependencyManagement>是只會用在這個場合內的,可以理解成定義在此區塊內的所有 dependency 資訊僅僅只是定義而已。

在打包的時候 Maven 並不會主動將定義在此區塊內的 jar 檔打包進 jar 或 war 檔內,所以還是會出現找不到依賴包的錯誤。

這時候我們就必須手動在我們自己的 pom.xml 內寫入依賴資訊,但不用提供依賴包版本,Maven 會自動抓取 parent pom.xml 內所定義的版本來使用。

<!-- own pom.xml file -->

<dependencies>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
    </dependency>
</dependencies>

那如果我們依舊在這之內使用了<version></version>寫入了版本資訊,那我們的 Maven 在打包的時候依舊會使用我們所寫的版本,那......不就失去了 parent pom.xml 的作用了?

 

那我們要確認提供此 parent pom.xml 的人是否有定義我們要用的依賴包資訊也很簡單,首先我們同樣先將依賴資訊加入我們的 pom.xml,但先不加入 version 資訊。

如果成功打包那就代表對方的 parent pom.xml 有定義我們所需的依賴包資訊,反之如果出現以下訊息,則代表對方並未定義我們所需使用依賴包資訊:

[ERROR] [ERROR] Some problems were encountered while processing the POMs:
[ERROR] 'dependencies.dependency.version' for com.github.bumptech.glide:glide:jar is missing. @ line 42, column 15
 @
[ERROR] The build could not read 1 project -> [Help 1]
[ERROR]
[ERROR]   The project that.is.our.self.pom:myCode:1.0.3 (/home/user/myCode/pom.xml) has 1 error
[ERROR]   'dependencies.dependency.version' for com.github.bumptech.glide:glide:jar is missing. @ line 42, column 15
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/ProjectBuildingException

這時候再將版本資訊添加進去即可,那至於要不要通知 parent pom.xml 的管理員加入你所使用的依賴包選項則就依照各位的狀況來斟酌了。

 

順帶一提,plugins 也有對應的 tag<pluginManagement></pluginManagement>可以使用,其用法跟上面的 dependencyManagement 一模一樣,只是一個放在 dependencies 之前,而另一個放在 plugins 之前。

代表的意義完全一樣,用來定義所使用的 plugins 版本資訊。

 

 

 

後記

其實後來發現 Maven 對於 parent pom.xml 所繼承的資訊其實還滿寬鬆的,連 distributionManagement 或是 repositories 所定義的資訊都可以繼承下來。

換句話說,如果有個依賴包在 Maven 預設的資料庫內沒有的話,其實是可以寫道 parent pom.xml 內,這樣所有使用到的工程師的 pom.xml 就會自到我們所設定的遠端資料庫去下載。

END

你可能感興趣的文章

【第二天 上午】[研究方法] 108年青年學者養成營 今天一整天就兩場的講座,從早聽到晚,真的是非常充實的一天,但卻也是非常的疲累呀

【第五天】﹝成果發表﹞暨畢業典禮記 如果用一句話來囊括這一次的青年學者養成營的活動,那應該就是 —— 五日青年學者養成營,成功!

【活動心得】108年青年學者養成營 要讓一個念頭、一串想法蹦出來都必須要些什麼契子?而一個行動、一次活動結束後又會有哪些想法、收穫呢?

【圖書資源利用】(找一篇論文)臺灣碩博士知識加值系統 一個收錄我國博碩士論文書目、摘要和已授權電子全文的網站,就算是要找任何的資料都很方便的網站,怎麼能不介紹?

【第三天 上午】[上篇]圖書資源利用及檢索技巧 一個上午、一個講師、一百二十三張簡報,至少十五種不同的網站,究竟能蒐羅幾千、幾萬筆資料呢?

【第四天】[實務課程] 108年青年學者養成營 在歷經三天的知識轟炸之後,今天終於要正式上戰場了?!不過…整個過程似乎都充滿了讓人意想不到的趣味?!

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

高捷少女:購票大作戰③  歐巴桑露出懷疑的表情。「怎麼了,您跟夏尼爾小姐不是朋友嗎?只要打電話確認就好了,不是嗎?」「是……是這樣沒錯……可是……」小穹支支吾吾地說。我的確是潔西塔的朋友,但

【歌評】蓮台野夜行 - 魔術師梅莉(魔術師メリー)  對於同一首歌每一個人都有不同的見解,看看別人對於這一首歌的看法,說不定就可以聽出這首歌想要表達的事情!

艾米莉亞和高捷戀旅② 七點三十五分了。 「快到粉絲團!快!」小穹慌張地說,婕兒、耐耐也湊向手機。雖然她們沒有報名,不過也希望亦晨參賽,不免著急起來。 「亦晨?妳的報名序號是多少?」艾米快速滑動手機。合格的參賽者姓名和序號都

【數學】徐氏數學簡明講義(三) 第二章 直線與園 P2.1-17 Q28 28.若X、Y∈R,試求之最小值___ 解: 配方 畫圖 做對稱點 求其直線長度 解

【中翻英歌詞】(二創歌)Poison Body ~ Forsaken Doll // ill-befated: fabricated, decorated; celebrated, venerated //致命、捏造、妝點、頌讚、景