Maven中引入本地jar包

来源:互联网 发布:淘宝低价高配主机 编辑:程序博客网 时间:2024/05/16 05:54

在实际开发中,我们可能会遇到有一些JAR包在Maven的公共仓库中没有,所以我们要在本地引入

这里也是刚刚尝试的,分享下

1. systemPath方式引入

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

感谢分享

我们在引入依赖的时候,有一个作用域,可以配置为system

对于依赖可以参考下这篇博客:(哎,看了下,写的不太好,有时间,重写下)

Maven深入学习(二)- 依赖 

示例:

[html] view plain copy
 在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 plain copy
 在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 plain copy
 在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标签,这个暂未成功,明天尝试下。

原文链接:http://blog.csdn.net/yuguiyang1990/article/details/40188461

0 0
原创粉丝点击