scope为provided
以下面dependency為例
1 <dependency> 2 <groupId>javax.servlet</groupId> 3 <artifactId>javax.servlet-api</artifactId> 4 <version>3.1.0</version> 5 <scope>provided</scope> 6 </dependency>
當(dāng)子工程中引入某個(gè)依賴時(shí),可見(jiàn)其設(shè)置<scope>provided</scope>,那么后續(xù)依賴該工程的所有項(xiàng)目會(huì)可能出現(xiàn)找不到這個(gè)依賴,原因是:
1.provided是沒(méi)有傳遞性的。即,如果你依賴的某個(gè)jar包,它的某個(gè)jar的范圍是provided,那么該jar不會(huì)在你的工程中依靠jar依賴傳遞加入到你的工程中。
2.provided具有繼承性,上面的情況,如果需要統(tǒng)一配置一個(gè)組織的通用的provided依賴,可以使用parent,然后在所有工程中繼承
比如:我現(xiàn)在有個(gè)工程結(jié)構(gòu)圖如下:
假設(shè)GranParent中的pom.xml是個(gè)超級(jí)pom.xml文件,
Parent繼承GranParent
1 <modelVersion>4.0.0</modelVersion> 2 <groupId>com.alice</groupId> 3 <artifactId>Parent</artifactId> 4 <version>1.0.0-SNAPSHOT</version> 5 6 <parent> 7 <groupId>com.alice</groupId> 8 <artifactId>GrandParent</artifactId> 9 <version>1.0.0</version> 10 </parent> 11 12 <modules> 13 <module>childA</module> 14 <module>childB</module> 15 <module>childB</module> 16 </modules> 17 <packaging>pom</packaging>
childA的pom.xml
1 <parent> 2 <groupId>com.alice</groupId> 3 <artifactId>Parent</artifactId> 4 <version>1.0.0-SNAPSHOT</version> 5 </parent> 6 <artifactId>childA</artifactId> 7 8 <name>childA</name> 9 <packaging>jar</packaging> 10 11 <dependencies> 12 <dependency> 13 <groupId>javax.servlet</groupId> 14 <artifactId>javax.servlet-api</artifactId> 15 <version>3.1.0</version> 16 <scope>provided</scope> 17 </dependency> 18 <dependencies>
childB的pom.xml,其中依賴childA
1 <modelVersion>4.0.0</modelVersion> 2 <parent> 3 <groupId>com.alice</groupId> 4 <artifactId>Parent</artifactId> 5 <version>1.0.0-SNAPSHOT</version> 6 </parent> 7 <artifactId>childB</artifactId> 8 9 <name>childB</name> 10 <packaging>jar</packaging> 11 12 <dependencies> 13 <dependency> 14 <groupId>com.alice</groupId> 15 <artifactId>childA</artifactId> 16 <version>1.0.0-SNAPSHOT</version> 17 </dependency> 18 </dependencies>
childC的pom.xml,其中依賴childB
1 <modelVersion>4.0.0</modelVersion> 2 <parent> 3 <groupId>com.alice</groupId> 4 <artifactId>Parent</artifactId> 5 <version>1.0.0-SNAPSHOT</version> 6 </parent> 7 <artifactId>childC</artifactId> 8 9 <name>childC</name> 10 <packaging>jar</packaging> 11 12 <dependencies> 13 <dependency> 14 <groupId>com.alice</groupId> 15 <artifactId>childB</artifactId> 16 <version>1.0.0-SNAPSHOT</version> 17 </dependency> 18 </dependencies>
從上面的pom文件可知,
在childA中,javax.servlet的scope為provided
首先解釋下為啥設(shè)置為provided:
因?yàn)槭莟omcat中也有servlet-api包,如果默認(rèn)為compile,即項(xiàng)目在編譯,測(cè)試,運(yùn)行階段都需要這個(gè)artifact對(duì)應(yīng)的jar包在classpath中,那么在使用tomcat運(yùn)行時(shí)就會(huì)發(fā)生了沖突,由于provided只影響編譯和測(cè)試階段,在編譯測(cè)試階段,我們需要這個(gè)artifact對(duì)應(yīng)的jar包在classpath中,而在運(yùn)行階段,假定目標(biāo)的容器(比如我們這里的tomcat容器)已經(jīng)提供了這個(gè)jar包,所以無(wú)需我們這個(gè)artifact對(duì)應(yīng)的jar包了,那么在實(shí)際發(fā)布時(shí)會(huì)默認(rèn)使用第三方web服務(wù)器中提供的jar包,而不會(huì)使用本jar包。
由于childB直接依賴childA,所以childB在測(cè)試編譯階段可引入servlet-api包,程序可以正常跑通,
但是childC依賴B,而childC也是需要childA中的servlet-api包,但是由于provided沒(méi)有傳遞性,scope在不指明情況下默認(rèn)為copmpile,所以childC和childA是間接依賴關(guān)系,所以childC無(wú)法引用servlet-api包,導(dǎo)致運(yùn)行報(bào)錯(cuò),此時(shí)需要單獨(dú)在childC中加servlet-api依賴包,
1 <modelVersion>4.0.0</modelVersion> 2 <parent> 3 <groupId>com.alice</groupId> 4 <artifactId>Parent</artifactId> 5 <version>1.0.0-SNAPSHOT</version> 6 </parent> 7 <artifactId>childC</artifactId> 8 9 <name>childC</name> 10 <packaging>jar</packaging> 11 12 <dependencies> 13 <dependency> 14 <groupId>com.alice</groupId> 15 <artifactId>childB</artifactId> 16 <version>1.0.0-SNAPSHOT</version> 17 </dependency> 18 <dependency> 19 <groupId>javax.servlet</groupId> 20 <artifactId>javax.servlet-api</artifactId> 21 <version>3.1.0</version> 22 <scope>test</scope> 23 </dependency> 24 </dependencies>
什么是傳遞性依賴(https://blog.csdn.net/lewky_liu/article/details/78145211)
有時(shí)候我們?cè)趐om.xml文件中引入的依賴,其本身就需要依賴于其他的依賴,這時(shí)候我們不需要去考慮這些依賴,Maven會(huì)解析各個(gè)直接依賴的pom,將那些必要的間接依賴,以傳遞性依賴的形式引入到當(dāng)前的項(xiàng)目中。
通過(guò)傳遞性依賴,我們可以在pom.xml文件中少寫(xiě)不少的依賴配置
傳遞性依賴的依賴范圍
假如當(dāng)前項(xiàng)目為A,A依賴于B,B依賴于C。此時(shí)稱A對(duì)于B是第一直接依賴,B對(duì)于C是第二直接依賴,而A對(duì)于C是傳遞性依賴。只要知道B在A項(xiàng)目中的scope,就可以知道C在A中的scope。其依賴范圍如下:
表格的第一列是B在A中的依賴范圍,第一行是C在B中的依賴范圍,交叉的格子是C在A中的依賴范圍
當(dāng)C在B中的scope為test時(shí),A不依賴C,C直接被丟棄
當(dāng)C在B中的scope為provided時(shí),只有當(dāng)B在A中的scope也是provided時(shí),A才會(huì)依賴C,這時(shí)候C在A的scope是provided
當(dāng)C在B中的scope為compile或runtime時(shí),A依賴C,此時(shí)C在A中的scope繼承自B在A的scope。注意,如果C的scope是runtime,B的scope是compile,此時(shí)C在A的scope是runtime,而不是compile
參考https://blog.csdn.net/u013704227/article/details/46460913
https://blog.csdn.net/lewky_liu/article/details/78145211
總結(jié)
以上是生活随笔為你收集整理的scope为provided的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 老猿学5G扫盲贴:中国移动网络侧CHF的
- 下一篇: Cooperation、Collabor