Maven學習(二十二)--Maven中引入本地jar包

在實際開發中,我們可能會遇到有一些JAR包在Maven的公共倉庫中沒有,所以我們要在本地引入

這裏也是剛剛嘗試的,分享下

1. systemPath方式引入

參考網址:http://www.cnblogs.com/richard-jing/archive/2013/01/27/Maven_localjar.html

感謝分享

我們在引入依賴的時候,有一個作用域,可以配置爲system

對於依賴可以參考下這篇博客:(哎,看了下,寫的不太好,有時間,重寫下)

Maven深入學習(二)- 依賴 

示例:

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <dependency>  
  2.     <groupId>org.postgresql</groupId>  
  3.     <artifactId>postgresql</artifactId>  
  4.     <version>9.3</version>  
  5.     <scope>system</scope>  
  6.     <systemPath>${project.basedir}/lib/postgresql-9.3.jar</systemPath>  
  7. </dependency>  

該jar包在項目中的位置:

上面參考的網址提醒:使用這種方式引入的JAR包在打包時不會一起打包,所以打包後找不到該JAR包

解決方法:

修改JAR包的位置,將JAR包放在resources目錄下

修改pom.xml

[html] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.   
  5.     <groupId>org.ygy</groupId>  
  6.     <artifactId>helloworld</artifactId>  
  7.     <version>0.0.1-SNAPSHOT</version>  
  8.     <packaging>jar</packaging>  
  9.   
  10.     <name>helloworld</name>  
  11.     <url>http://maven.apache.org</url>  
  12.   
  13.     <properties>  
  14.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  15.     </properties>  
  16.   
  17.     <dependencies>  
  18.         <dependency>  
  19.             <groupId>junit</groupId>  
  20.             <artifactId>junit</artifactId>  
  21.             <version>3.8.1</version>  
  22.             <scope>test</scope>  
  23.         </dependency>  
  24.   
  25.         <dependency>  
  26.             <groupId>org.postgresql</groupId>  
  27.             <artifactId>postgresql</artifactId>  
  28.             <version>9.3</version>  
  29.             <scope>system</scope>  
  30.             <systemPath>${project.basedir}/src/main/resources/lib/postgresql-9.3.jar</systemPath>  
  31.         </dependency>  
  32.     </dependencies>  
  33.   
  34.     <build>  
  35.         <resources>  
  36.             <!-- 這種方式可以將JAR包引入,還不清楚原因,待研究 -->  
  37.             <resource>  
  38.                 <targetPath>lib/</targetPath>  
  39.                 <directory>lib/</directory>  
  40.                 <includes>  
  41.                     <include>**/postgresql-9.3.jar</include>  
  42.                 </includes>  
  43.             </resource>  
  44.         </resources>  
  45.     </build>  
  46. </project>  

2. 將JAR包安裝到本地倉庫

官方介紹:http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html

執行命令:

[java] view plaincopy在CODE上查看代碼片派生到我的代碼片
  1. D:\WorkSpace\cognos\helloworld\src\main\resources\lib>mvn install:install-file -  
  2. Dfile=postgresql-9.3.jar -DgroupId=org.postgresql -DartifactId=postgresql -Dvers  
  3. ion=9.3 -Dpackaging=jar  

執行該命令後,Maven會將該JAR包部署到本地倉庫中,這樣在Maven中就可以正常使用了

但是其他的話,也需要執行以下mvn install命令纔可以正常使用


3. 將本地lib發佈爲倉庫

使用repository標籤,這個暫未成功,明天嘗試下。



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章