maven使用systemPath方式加載本地jar的實(shí)現(xiàn)
1、背景
在對(duì)接第三方廠商時(shí),會(huì)提供對(duì)應(yīng)jar,maven公開倉庫上沒有發(fā)布,提供的處理方法。
- 上傳的公司私服。
- systemPath方式加載本地jar。
本文要講的就是使用systemPath方式加載本地jar。
2、使用
示例如下
2.1 下載第三方j(luò)ar
jar-local-1.0.0.jar
jar中提供了MyStringUtils.generateUUID方法。
2.2 項(xiàng)目引入
2.2.1 引入依賴
<dependency> <groupId>com.ybw</groupId> <artifactId>jar-local</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/jar-local-1.0.0.jar</systemPath> </dependency>
- groupId:此名稱可自定義,com.ybw為自定義的名稱;
- artifactId:此名稱可自定義,jar-local為自定義的名稱,不過這個(gè)最好與加載的jar(jar-local-1.0.0.jar)名稱保持一致。
- version:此版本號(hào)可自定義,不過好與加載的jar保持一致。
- scope:使用system。
- systemPath:本地jar文件你放在系統(tǒng)文件的目錄,${project.basedir}表示項(xiàng)目根目錄。lib和src為同級(jí)目錄。
2.2.2 插件配置
spring boot項(xiàng)目
<build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <source>1.8</source> <target>1.8</target> <encoding>UTF-8</encoding> </configuration> </plugin> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>${spring-boot.version}</version> <configuration> <mainClass>com.ybw.jar.load.local.JarLocalApplication</mainClass> <includeSystemScope>true</includeSystemScope> </configuration> <executions> <execution> <id>repackage</id> <goals> <goal>repackage</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
<includeSystemScope>true</includeSystemScope>作用:
代表maven打包時(shí)會(huì)將外部引入的jar包(比如在根目錄下或resource文件下新加外部jar包)打包到項(xiàng)目jar,在服務(wù)器上項(xiàng)目才能運(yùn)行,不加此配置,本地可以運(yùn)行,因?yàn)楸镜乜梢栽賚ib下找到外部包,但是服務(wù)器上jar中是沒有的。
2.2.3 使用本地jar方法
已可以正常使用MyStringUtils.generateUUID()
package com.ybw.jar.load.local.utils; import com.ybw.jar.local.utils.MyStringUtils; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; /** * 依賴測試 * * @author ybw * @version V1.0 * @className MyStringUtilsTest * @date 2022/4/24 **/ @Slf4j public class MyStringUtilsTest { @Test public void print() { log.info("{}", MyStringUtils.generateUUID()); } }
3、擴(kuò)展(scope屬性)
依賴范圍控制哪些依賴在哪些classpath 中可用,哪些依賴包含在一個(gè)應(yīng)用中。
- compile (編譯)
compile是默認(rèn)的范圍;如果沒有提供一個(gè)范圍,那該依賴的范圍就是編譯范圍。編譯范圍依賴在所有的classpath中可用,同時(shí)它們也會(huì)被打包。
- provided (已提供)
provided 依賴只有在當(dāng)JDK 或者一個(gè)容器已提供該依賴之后才使用。例如, 如果你開發(fā)了一個(gè)web 應(yīng)用,你可能在編譯 classpath 中需要可用的Servlet API 來編譯一個(gè)servlet,但是你不會(huì)想要在打包好的WAR 中包含這個(gè)Servlet API;這個(gè)Servlet API JAR 由你的應(yīng)用服務(wù)器或者servlet 容器提供。已提供范圍的依賴在編譯classpath (不是運(yùn)行時(shí))可用。它們不是傳遞性的,也不會(huì)被打包。
- runtime (運(yùn)行時(shí))
runtime 依賴在運(yùn)行和測試系統(tǒng)的時(shí)候需要,但在編譯的時(shí)候不需要。比如,你可能在編譯的時(shí)候只需要JDBC API JAR,而只有在運(yùn)行的時(shí)候才需要JDBC驅(qū)動(dòng)實(shí)現(xiàn)。
- test (測試)
test范圍依賴 在一般的編譯和運(yùn)行時(shí)都不需要,它們只有在測試編譯和測試運(yùn)行階段可用。
- system (系統(tǒng))
system范圍依賴與provided類似,但是你必須顯式的提供一個(gè)對(duì)于本地系統(tǒng)中JAR文件的路徑。這么做是為了允許基于本地對(duì)象編譯,而這些對(duì)象是系統(tǒng)類庫的一部分。這樣的構(gòu)建應(yīng)該是一直可用的,Maven 也不會(huì)在倉庫中去尋找它。如果你將一個(gè)依賴范圍設(shè)置成系統(tǒng)范圍,你必須同時(shí)提供一個(gè)systemPath元素。注意該范圍是不推薦使用的(建議盡量去從公共或定制的 Maven 倉庫中引用依賴)。
- import(導(dǎo)入)
import 只能在pom文件的<dependencyManagement>中使用,從而引入其他的pom文件中引入依賴,如:在Spring boot 項(xiàng)目的POM文件中,我們可以通過在POM文件中繼承 Spring-boot-starter-parent來引用Srping boot默認(rèn)依賴的jar包,如下:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.6.7</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.6.7</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
通過上面方式,就可以獲取spring-boot-dependencies.2.6.7.pom文件中dependencyManagement配置的jar包依賴。如果要繼承多個(gè),可以在dependencyManagement中添加,如:
<dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.6.7</version> <type>pom</type> <scope>import</scope> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>${spring-cloud.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
到此這篇關(guān)于maven使用systemPath方式加載本地jar的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)maven 加載本地jar內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
詳解Java中while和do-while循環(huán)、break的使用
本文介紹了循環(huán)結(jié)構(gòu)語句while和do-while循環(huán)、break的使用,while循環(huán)語句通過流程圖和語法語句結(jié)合一個(gè)求1~10的整數(shù)和的例子來幫助大家理解while循環(huán)的用法,感興趣的朋友跟隨小編來看看吧2020-11-11SpringBoot項(xiàng)目啟動(dòng)時(shí)增加自定義Banner的簡單方法
最近看到springboot可以自定義啟動(dòng)時(shí)的banner,然后自己試了一下,下面這篇文章主要給大家介紹了SpringBoot項(xiàng)目啟動(dòng)時(shí)增加自定義Banner的簡單方法,需要的朋友可以參考下2022-01-01java如何實(shí)現(xiàn)抽取json文件指定字段值
這篇文章主要介紹了java如何實(shí)現(xiàn)抽取json文件指定字段值,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2022-06-06java實(shí)現(xiàn)多人聊天工具(socket+多線程)
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)多人聊天工具,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08