在實際開發中,我們可能會遇到有一些JAR包在Maven的公共倉庫中沒有,所以我們要在本地引入
這里也是剛剛嘗試的,分享下
### 1. systemPath方式引入
參考網址:[http://www.cnblogs.com/richard-jing/archive/2013/01/27/Maven_localjar.html](http://www.cnblogs.com/richard-jing/archive/2013/01/27/Maven_localjar.html)
感謝分享
我們在引入依賴的時候,有一個作用域,可以配置為system
對于依賴可以參考下這篇博客:(哎,看了下,寫的不太好,有時間,重寫下)
[Maven深入學習(二)- 依賴?](http://blog.csdn.net/yuguiyang1990/article/details/8866719)
示例:
~~~
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/lib/postgresql-9.3.jar</systemPath>
</dependency>
~~~
該jar包在項目中的位置:

上面參考的網址提醒:使用這種方式引入的JAR包在打包時不會一起打包,所以打包后找不到該JAR包
解決方法:
修改JAR包的位置,將JAR包放在resources目錄下

修改pom.xml
~~~
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.ygy</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>helloworld</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.3</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/lib/postgresql-9.3.jar</systemPath>
</dependency>
</dependencies>
<build>
<resources>
<!-- 這種方式可以將JAR包引入,還不清楚原因,待研究 -->
<resource>
<targetPath>lib/</targetPath>
<directory>lib/</directory>
<includes>
<include>**/postgresql-9.3.jar</include>
</includes>
</resource>
</resources>
</build>
</project>
~~~
### 2. 將JAR包安裝到本地倉庫
官方介紹:[http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html](http://maven.apache.org/guides/mini/guide-3rd-party-jars-local.html)
執行命令:
~~~
D:\WorkSpace\cognos\helloworld\src\main\resources\lib>mvn install:install-file -
Dfile=postgresql-9.3.jar -DgroupId=org.postgresql -DartifactId=postgresql -Dvers
ion=9.3 -Dpackaging=jar
~~~

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

但是其他的話,也需要執行以下mvn install命令才可以正常使用
### 3. 將本地lib發布為倉庫
使用repository標簽,這個暫未成功,明天嘗試下。
- 前言
- (一)- 環境搭建
- (二)- 安裝m2eclipse插件
- (三)- 使用Maven構建Web項目
- (四)- 使用Maven構建Web項目-測試
- (五)- 使用Maven構建Struts2項目
- (六)- 構建Hibernate項目
- (七)- 構建Spring項目
- (八)- 構建MyBatis項目
- (九)- 構建SSH項目
- (十) - 階段小結
- Maven深入學習(一)- 坐標
- Maven深入學習(二)- 依賴
- Maven深入學習(三)- 聚合與繼承
- Maven深入學習(四)- 知識總結
- Maven創建的Web項目無法使用EL表達式
- Maven知識點記錄 - profile
- Maven知識點記錄 - repositories
- Maven最佳實踐:版本管理
- Ubuntu上安裝Maven3
- Maven常用命令-創建Java項目
- Maven常用命令-創建Web項目
- Maven中引入本地jar包
- Maven私服(一) - The nexus service was launched, but failed to start.
- Maven私服(二) - Nexus的安裝