POM文件的Scope行爲總結

今天在公司弄spark機器學習時,發現spark的mllib包下載下來了,但到自己的scala文件中一直引入不進去,後來一點一點排查這問題,發現在pom文件中引入spark-mllib包的時候,多加了一個scope runtime屬性,導致包引用不進去,於是便上網查詢了scope的這幾種行爲,總結如下,希望以後再也不要踩這個坑了:

官網的解釋如下:

  • compile - this is the default scope, used if none is specified. Compile dependencies are available in all classpaths. Furthermore, those dependencies are propagated to dependent projects.
  • provided - this is much like compile, but indicates you expect the JDK or a container to provide it at runtime. It is only available on the compilation and test classpath, and is not transitive.
  • runtime - this scope indicates that the dependency is not required for compilation, but is for execution. It is in the runtime and test classpaths, but not the compile classpath.
  • test - this scope indicates that the dependency is not required for normal use of the application, and is only available for the test compilation and execution phases. It is not transitive.
  • system - this scope is similar to provided except that you have to provide the JAR which contains it explicitly. The artifact is always available and is not looked up in a repository.

compile:默認的scope。任何定義在compile scope下的依賴將會在所有的class paths下可用。maven工程會將其打包到最終的artifact中。如果你構建一個WAR類型的artifact,那麼在compile scope下引用的JAR文件將會被集成到WAR文件內。

provided:這個scope假定對應的依賴會由運行這個應用的JDK或者容器來提供。最好的例子就是servlet API。任何在provided scope下定義的依賴在構建時的類路徑裏是可用的,但是不會被打包到最終的artifact中。如果是一個WAR的文件,servlet API在構建時的類路徑裏是可用的,但是並不會被打包到WAR文件中。

runtime:在runtime scope下定義的依賴只會在運行期可用,而在構建期的類路徑下不可用。這些依賴將會被打包到最終的artifact中。比如你有一個基於web的應用需要在運行時訪問MySQL數據庫。你的代碼沒有任何MySQL數據庫驅動的硬依賴。你的代碼僅僅是基於JDBC API來編寫,在構建期並不需要MySQL數據庫驅動。然而,在運行期,就需要相應的驅動來操作MySQL數據庫了。因此,這個驅動應該被打包到最終的artifact中。

test:只用於測試變異的依賴(比如JUnit),execution必須定義在test scope下。這些依賴不會被打包到最終的artifact中。

system:於provided scope很像。唯一的區別在於,在system scope中,你需要告訴Maven如何去找到這個依賴。如果你要引用的依賴在Maven倉庫中不存在時,就可以用這個scope。不推薦使用system依賴。

import:從其它的pom文件中導入依賴設置。

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