Maven中引入本地jar包

在实际开发中,我们可能会遇到有一些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>
			<!-- 这种方式可以将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

执行命令:

D:WorkSpacecognoshelloworldsrcmain
esourceslib>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标签,这个暂未成功,明天尝试下。

文章导航