1. 程式人生 > 其它 >java spring boot maven 引入第三方jar包 以及scope配置

java spring boot maven 引入第三方jar包 以及scope配置

技術標籤:javamavenmavenjavajarspring boot

首先 專案根目錄下(與src等平行目錄)建立資料夾 lib 然後將你所需要用的jar包放在該資料夾下 如圖所示

然後pom中引入你新加的jar包

<dependency>
			<groupId>*****</groupId>
			<artifactId>***</artifactId>
			<version>2.0</version>
			<scope>system</scope>
			<systemPath>${project.basedir}/lib/***.jar</systemPath>
</dependency>

關於scope

  • compile 預設的scope
  • 表示 dependency 可以在生命週期中使用。而且這些dependencies 會傳遞到依賴的專案中。
  • provided
  • 跟compile相似,但是表明了dependency 由JDK或者容器提供。
    例如Servlet AP和一些Java EE APIs。這個scope 只能作用在編譯和測試時,同時沒有傳遞性。
    使用這個時,不會將包打入本專案中,只是依賴過來。(when building a web application for the Java Enterprise Edition, you would set the dependency on the Servlet API and related Java EE APIs to scope provided because the web container provides those classes. This scope is only available on the compilation and test classpath, and is not transitive.)
  • runtime
  • 表示dependency不作用在編譯時,但會作用在執行和測試時
  • test
  • 表示dependency作用在測試時,不作用在執行時
  • system
  • 跟provided 相似,但是在系統中要以外部JAR包的形式提供,maven不會在repository查詢它。

  • import (Maven 2.0.9 之後新增)
  • 它只使用在<dependencyManagement>中,表示從其它的pom中匯入dependency的配置,例如 (B專案匯入A專案中的包配置):

依賴引入之後 你本地就可以使用了 但是要想將其打到專案的jar包裡 還需要如下配置

pom build節點下新增

<resources>  
     <resource>  
         <directory>lib</directory>  
         <targetPath>BOOT-INF/lib/</targetPath>  
         <includes>  
             <include>**/*.jar</include>  
         </includes>  
     </resource>
 </resources>

以及

<plugin>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-maven-plugin</artifactId>
		<configuration>
			<includeSystemScope>true</includeSystemScope>
		</configuration>
</plugin>

看是否成功 檢查你的target目錄下是否出現了 lib/***.jar檔案

如上配置之後第三方jar包就可以正常使用了。