Maven中引入本地jar包

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. <groupId>org.ygy</groupId>
  5. <artifactId>helloworld</artifactId>
  6. <version>0.0.1-SNAPSHOT</version>
  7. <packaging>jar</packaging>
  8. <name>helloworld</name>
  9. <url>http://maven.apache.org</url>
  10. <properties>
  11. <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  12. </properties>
  13. <dependencies>
  14. <dependency>
  15. <groupId>junit</groupId>
  16. <artifactId>junit</artifactId>
  17. <version>3.8.1</version>
  18. <scope>test</scope>
  19. </dependency>
  20. <dependency>
  21. <groupId>org.postgresql</groupId>
  22. <artifactId>postgresql</artifactId>
  23. <version>9.3</version>
  24. <scope>system</scope>
  25. <systemPath>${project.basedir}/src/main/resources/lib/postgresql-9.3.jar</systemPath>
  26. </dependency>
  27. </dependencies>
  28. <build>
  29. <resources>
  30. <!-- 这种方式可以将JAR包引入,还不清楚原因,待研究 -->
  31. <resource>
  32. <targetPath>lib/</targetPath>
  33. <directory>lib/</directory>
  34. <includes>
  35. <include>**/postgresql-9.3.jar</include>
  36. </includes>
  37. </resource>
  38. </resources>
  39. </build>
  40. </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命令才可以正常使用


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值