MAVEN專案依賴外部JAR進行打包的兩種方式
阿新 • • 發佈:2022-05-13
前言
第三方jar包在開發工具中引入後編譯沒問題, 啟動除錯包括打包時會提示找不到jar包的錯誤.需要上傳到maven倉庫中,並在pom檔案內引入.
-
匯入jar包
-
點選Project Structure進行專案設定,在Modules中Dependencies標籤中點選+號 新增lib下的所有jar
這樣的話專案中就可以使用lib中依賴的jar了,但是如果要打包則會報錯,須進行相關配置。
一、使用開發工具引入
- 在pom.xml中的build標籤內容新增resources標籤,如下:
<resources> <resource> <directory>lib</directory> <targetPath>BOOT-INF/lib/</targetPath> <includes> <include>**/*.jar</include> </includes> </resource> </resources>
- 然後在plugin標籤的configuration標籤內加入compilerArguments標籤
<compilerArguments><!-- 打包本地jar包 --> <extdirs>${project.basedir}/lib</extdirs> </compilerArguments>
-
如圖
-
然後使用maven命令進行打包即可
mvn clean install -Dmaven.test.skip=true -Dcheckstyle.skip=true
示例:pandas 是基於NumPy 的一種工具,該工具是為了解決資料分析任務而建立的。
二、引入jar包路徑
- 在pom.xml中通過以下方式引入lib中的每個依賴
<dependency> <groupId>com.xxx.www</groupId> <artifactId>out-jar-1</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/commons-cxxxx.jar</systemPath> </dependency> <dependency> <groupId>com.xxx.www</groupId> <artifactId>out-jar-2</artifactId> <version>1.0.0</version> <scope>system</scope> <systemPath>${project.basedir}/lib/commons-httxxxx.jar</systemPath> </dependency>
-
其中groupId和artifactId可以隨便填,注意artifactId不要重複了
-
然後使用maven命令進行打包即可:
mvn clean install -Dmaven.test.skip=true -Dcheckstyle.skip=true
- 如果是SpringBoot專案還要加如下配置:
<plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <includeSystemScope>true</includeSystemScope> </configuration> </plugin>
- 主要是
<includeSystemScope>true</includeSystemScope>
三、jar包生成maven
- Maven 相關命令
安裝指定檔案到本地倉庫命令:mvn install:install-file
-DgroupId=<groupId> : 設定上傳到倉庫的包名
-DartifactId=<artifactId> : 設定該包所屬的模組名
-Dversion=1.0.0 : 設定該包的版本號
-Dpackaging=jar : 設定該包的型別(很顯然jar包)
-Dfile=<myfile.jar> : 設定該jar包檔案所在的路徑與檔名
- 上傳jar包 win+r cmd開啟命令列輸入以下命令
# 進入到jar包目錄,進行cmd命令
# xjbw-security-access
mvn install:install-file -Dfile=xjbw-security-access.jar -DgroupId=com.github -DartifactId=xjbw-security-access -Dversion=1.4.5 -Dpackaging=jar
-
結果
-
使用
<dependency> <groupId>com.github</groupId> <artifactId>xjbw-security-access</artifactId> <version>1.4.5</version> </dependency>