[精讚] [會員登入]
285

【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年青年學者養成營二三事 今年是國家圖書館舉辦的第五屆的青年學者養成營,算是一個新興的小營隊,那...裡面到底都發生些什麼事情呢?小編帶你一塊直擊!

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[活動] 2017年4/1雲空幻想愚人節活動彩蛋&攻略 (紀念性質) 雲空幻想2017年的愚人節活動介紹同時也是本小編的第一篇網路文章(*^ω^)♪

高捷少女:布拉格體驗㊤ 「可……可以去歐洲玩?而且還有人出錢?太棒囉!」婕兒忍不住歡呼起來,把扳手拋呀拋的,旁邊的客人紛紛轉過來看。「好了啦!」耐耐忍不住害羞起來,輕輕敲了一下婕兒的頭

高捷少女:小穹與果仁巧克力㊦ 「如果妳跟一個女生同班三年,看過她午休流口水跟狼吞虎嚥地吃午餐,就算變成高捷代言人,也很難把她當女神的啦!」她說,小穹氣得搶走她義大利麵裡的蝦子,其他人笑得花枝招展。

高捷少女:地下城的探險少女① 婕兒心中一奇,便走上前看著仔細。那塊凹進去的地方中心大約三公分厚,越往邊緣就越淺,圓型直徑十五公分。婕兒拿出銅盤對比一下,發現兩者大小竟然一致,銅盤似乎能夠完整的嵌進去。     婕兒看著凹槽,心中

高捷少女:地下城的探險少女(終)  小穹眨眨眼睛,然後說了出來。「其實,從剛剛開始,我就在想了……是在看過這本日記之後。」她拿出日記。「我想……我們尋找寶藏的想法,是不是真的正確的?」「怎麼說呢?」耐耐好奇地問。「這個埋藏寶藏的人,在