使用Maven中的scope總結(jié)
Maven中的scope總結(jié)
Maven中的scope主要有以下6種
接下來分別介紹下這幾種scope:
compile
不聲明scope元素的情況下的默認(rèn)值;compile表示被依賴包需要參與當(dāng)前項(xiàng)目的編譯,包括后續(xù)的測(cè)試,運(yùn)行周期也參與其中,是一個(gè)比較強(qiáng)的依賴;打包的時(shí)候通常需要包含進(jìn)去。
provided
provided 類型的scope只會(huì)在項(xiàng)目的編譯、測(cè)試階段起作用;可以認(rèn)為在目標(biāo)容器中已經(jīng)提供了這個(gè)依賴,無(wú)需在提供,但是在編寫代碼或者編譯時(shí)可能會(huì)用到這個(gè)依賴;依賴不會(huì)被打入到項(xiàng)目jar包中。
說到provided,這里就要說到<dependency>下的子標(biāo)簽<optional> ,如果一個(gè)依賴的<optional> 設(shè)置為true,則該依賴在打包的時(shí)候不會(huì)被打進(jìn)jar包,同時(shí)不會(huì)通過依賴傳遞傳遞到依賴該項(xiàng)目的工程;例如:x
依賴B,B由依賴于A(x->B->A),則A中設(shè)置<optional> 為true的依賴不會(huì)被傳遞到x中。
這兩者的區(qū)別在于:
1、<optional>為true 表示某個(gè)依賴可選,該依賴是否使用都不會(huì)影響服務(wù)運(yùn)行;
2、provided的<scope>在目標(biāo)容器中已經(jīng)提供了這個(gè)依賴,無(wú)需在提供
runtime
runtime與compile比較相似,區(qū)別在于runtime 跳過了編譯階段,打包的時(shí)候通常需要包含進(jìn)去。
test
在一般的編譯和運(yùn)行時(shí)都不需要,它們只有在測(cè)試編譯和測(cè)試運(yùn)行階段可用,不會(huì)被打包到項(xiàng)目jar包中,同時(shí)如果項(xiàng)目A依賴于項(xiàng)目B,項(xiàng)目B中的test作用域下的依賴不會(huì)被繼承。
system
表示使用本地系統(tǒng)路徑下的jar包,需要和一個(gè)systemPath一起使用,如下:
<!--引用-->
?? ??? ?<dependency>
?? ??? ??? ?<groupId>xxxx</groupId>
?? ??? ??? ?<artifactId>xxx</artifactId>
?? ??? ??? ?<systemPath>${basedir}/lib/xxxxx.jar</systemPath>
?? ??? ??? ?<scope>system</scope>
?? ??? ??? ?<version>1.4.12</version>
?? ??? ?</dependency>import
import 只能在pom文件的<dependencyManagement>中使用,從而引入其他的pom文件中引入依賴,如:在Spring boot 項(xiàng)目的POM文件中,我們可以通過在POM文件中繼承 Spring-boot-starter-parent來引
用Srping boot默認(rèn)依賴的jar包,如下:
<!-- Inherit defaults from Spring Boot --> <parent> ?? ?<groupId>org.springframework.boot</groupId> ?? ?<artifactId>spring-boot-starter-parent</artifactId> ?? ?<version>2.0.1.BUILD-SNAPSHOT</version> </parent>
但是,通過上面的parent繼承的方法,只能繼承一個(gè) spring-boot-start-parent。實(shí)際開發(fā)中,用戶很可能需要繼承自己公司的標(biāo)準(zhǔn)parent配置,這個(gè)時(shí)候可以使用 scope=import 來實(shí)現(xiàn)多繼承。代碼如下:
?? ? <dependencyManagement> ?? ? ? ? <dependencies> ?? ? ? ? ? ? <dependency> ?? ? ? ? ? ? ? ? <!-- Import dependency management from Spring Boot --> ?? ? ? ? ? ? ? ? <groupId>org.springframework.boot</groupId> ?? ? ? ? ? ? ? ? <artifactId>spring-boot-dependencies</artifactId> ?? ? ? ? ? ? ? ? <version>2.0.1.BUILD-SNAPSHOT</version> ?? ? ? ? ? ? ? ? <type>pom</type> ?? ? ? ? ? ? ? ? <scope>import</scope> ?? ? ? ? ? ?</dependency> ?? ? ? ?</dependencies> ?? ?</dependencyManagement>
通過上面方式,就可以獲取spring-boot-dependencies.2.0.1.BUILD-SNAPSHOT.pom文件中dependencyManagement配置的jar包依賴。如果要繼承多個(gè),可以在dependencyManagement中添加,如:
?? ? <dependencyManagement> ?? ? ? ? <dependencies> ?? ? ? ? ? ? <!-- Override Spring Data release train provided by Spring Boot --> ?? ? ? ? ? ? <dependency> ?? ? ? ? ? ? ? ? <groupId>org.springframework.data</groupId> ?? ? ? ? ? ? ? ? <artifactId>spring-data-releasetrain</artifactId> ?? ? ? ? ? ? ? ? <version>Fowler-SR2</version> ?? ? ? ? ? ? ? ? <type>pom</type> ?? ? ? ? ? ? ? ? <scope>import</scope> ?? ? ? ? ? ?</dependency> ?? ? ? ? ? ?<dependency> ?? ? ? ? ? ? ? ?<groupId>org.springframework.boot</groupId> ?? ? ? ? ? ? ? ?<artifactId>spring-boot-dependencies</artifactId> ?? ? ? ? ? ? ? ?<version>2.0.1.BUILD-SNAPSHOT</version> ?? ? ? ? ? ? ? ?<type>pom</type> ?? ? ? ? ? ? ? ?<scope>import</scope> ?? ? ? ? ? ?</dependency> ?? ? ? ?</dependencies> ?? ?</dependencyManagement>
Maven中<scope>參數(shù)</scope>配置
| 參數(shù)名稱 | 具體功能 |
| <scope>compile</scope> | 默認(rèn)值,表示當(dāng)前依賴包要參與當(dāng)前項(xiàng)目的編譯后續(xù)測(cè)試運(yùn)行時(shí)打包 |
| <scope>provided</scope> | 當(dāng)前包只在編譯和測(cè)試的時(shí)候使用,而不再后續(xù)的運(yùn)行和打包的時(shí)候不會(huì)打包進(jìn)來 |
| <scope>test</scope> | 表示當(dāng)前依賴包只參與測(cè)試工作 |
| <scope>runtime</scope> | 表示當(dāng)前依賴包只參與運(yùn)行周期,其他跳過 |
| <scope>system</scope> | 從參與度和provided一致,不過被依賴項(xiàng)不會(huì)從maven遠(yuǎn)程倉(cāng)庫(kù)下載,而是從本地的系統(tǒng)拿。需要systemPath屬性來定義路徑 |
解決maven項(xiàng)目中無(wú)法打包生成空文件夾的問題
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.1.1</version>
<executions>
<!-- Run shade goal on package phase -->
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<artifactSet>
<excludes>
<exclude>org.apache.flink:force-shading</exclude>
<exclude>com.google.code.findbugs:jsr305</exclude>
<exclude>org.slf4j:*</exclude>
<exclude>org.apache.logging.log4j:*</exclude>
</excludes>
</artifactSet>
<filters>
<filter>
<!-- Do not copy the signatures in the META-INF folder.
Otherwise, this might cause SecurityExceptions when using the JAR. -->
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.lkr.flink.StreamingJob</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)全局過濾器功能
本文主要介紹了springcloud整合gateway實(shí)現(xiàn)網(wǎng)關(guān)全局過濾器功能,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Spring-Cloud Eureka注冊(cè)中心實(shí)現(xiàn)高可用搭建
這篇文章主要介紹了Spring-Cloud Eureka注冊(cè)中心實(shí)現(xiàn)高可用搭建,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-04-04
SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)示例
本文介紹了SpringBoot+MyBatis-Plus實(shí)現(xiàn)分頁(yè)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-12-12
Java創(chuàng)建數(shù)組、賦值的四種方式詳解(聲明+創(chuàng)建+初始化?)
數(shù)組是一種數(shù)據(jù)結(jié)構(gòu),用來存儲(chǔ)同一類型值的集合一旦創(chuàng)建了數(shù)組,就不能再改變它的長(zhǎng)度,下面這篇文章主要給大家介紹了關(guān)于Java創(chuàng)建數(shù)組、賦值的四種方式(聲明+創(chuàng)建+初始化?)的相關(guān)資料,需要的朋友可以參考下2024-04-04
Java基于HttpClient實(shí)現(xiàn)RPC的示例
HttpClient可以實(shí)現(xiàn)使用Java代碼完成標(biāo)準(zhǔn)HTTP請(qǐng)求及響應(yīng)。本文主要介紹了Java基于HttpClient實(shí)現(xiàn)RPC,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
mybatis-plus之自動(dòng)映射字段(typeHandler)的注意點(diǎn)及說明
這篇文章主要介紹了mybatis-plus之自動(dòng)映射字段(typeHandler)的注意點(diǎn)及說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04
springboot vue組件開發(fā)實(shí)現(xiàn)接口斷言功能
這篇文章主要為大家介紹了springboot+vue組件開發(fā)實(shí)現(xiàn)接口斷言功能,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05
Mybatis-Plus實(shí)現(xiàn)自定義SQL具體方法
Mybatis-Plus是Mybatis的一個(gè)增強(qiáng)工具,它可以優(yōu)化我們的開發(fā)效率,這篇文章主要介紹了Mybatis-Plus實(shí)現(xiàn)自定義SQL,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08

