在實際開發中,我們可能會遇到有一些JAR包在Maven的公共倉庫中沒有,所以我們要在本地引入
這裏也是剛剛嘗試的,分享下
1. systemPath方式引入
參考網址:http://www.cnblogs.com/richard-jing/archive/2013/01/27/Maven_localjar.html
感謝分享
我們在引入依賴的時候,有一個作用域,可以配置爲system
對於依賴可以參考下這篇博客:(哎,看了下,寫的不太好,有時間,重寫下)
Maven深入學習(二)- 依賴
示例:
-
<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>
-
-
<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
執行命令:
-
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標籤,這個暫未成功,明天嘗試下。