maven之依賴範圍

依賴範圍scope與classpath的關係

maven的依賴範圍決定了不同的classpath。不同的依賴範圍決定了該依賴是否出現的對應的classpath中。classpath分編譯classpath,測試classpath,運行時classpath。

scope 編譯classpath 測試classpath 運行classpath 典型例子
compile yes yes yes spring-core
test no yes no junit
provided yes yes no servlet-api
runtime no yes yes jdbc驅動
system yes yes no 本地類文件

---------------------------------------------------華麗的分割線------------------------------------------------------
scope裏面的test要拉出來單獨說一下。
我一開始對test的是有誤解的,我認爲編譯classpath和測試classpath都會依賴使用該scope的包。
但實際情況不是這樣的,我們使用mvn compile進行編譯,maven默認只會去掃src/main/下面的源碼。也就是說src/test/下面的源碼不會去編譯,我還專門去試了下,故意寫錯一個測試類,mvn compile還是可以編譯通過的。這也解釋了,爲啥scope的test的依賴包,不會出現在編譯classpath下面。
還看過一個帖子,有tx把測試類和生產類寫在一個包下面,mvn compile去編譯,怎麼編都編不過去,原因就是junit的scope設置爲test,這樣在編譯classpath下面不會出現junit,這樣測試類自然就編不去。

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