如何向maven本地仓库添加jar包?

如何向maven本地仓库添加jar包?,第1张

原则上Maven的设计是不需要这么做的,因为pom.xml中依赖的jar包会自动实现从中央仓库下载到本地仓库。但是公司设计了一个setting,如果本地仓库没有,就去setting指定的url中下载jar包,如果还没有就报错。\x0d\x0a考虑到setting中url的jar包比较老,如果需要用最新的,则需要将jar包手动下载到本地仓库。\x0d\x0a\x0d\x0a比如我下载了\x0d\x0alucene-queryparser-4.6.1.jar\x0d\x0a\x0d\x0a怎么添加到本地仓库呢?\x0d\x0a步骤:\x0d\x0a1.cmd命令进入该jar包所在路径\x0d\x0a2.执行命令\x0d\x0amvn install:install-file -Dfile=lucene-queryparser-4.6.1.jar -DgroupId=org.apache.lucene -DartifactId=lucene-queryparser -Dversion=4.6.1 -Dpackaging=jar\x0d\x0a(不同的jar包相对应替换对应部分)\x0d\x0a\x0d\x0a另外我尝试了下面的方法,发现不行:\x0d\x0a直接写一个空的pom,里头包含对所需要jar包的依赖,通过这种方式希望将jar包下载到本地仓库。但是应用代码中没用到,maven命令没有下载这个jar包到本地仓库。\x0d\x0a\x0d\x0a补充,-DgroupId和-DartifactId的作用其实是指定了这个jar包的安装在repository的安装路径,只是用来告诉项目去这个路径下寻找这个名称的jar包。比如:\x0d\x0amvn install:install-file -Dfile=freemarker-2.3.13.jar -DgroupId=freemarker -DartifactId=freemarker -Dversion=2.3.13 -Dpackaging=jar\x0d\x0a\x0d\x0a就是安装到了repository\freemarker\freemarker\2.3.13目录下,如果在dependency的依赖里也这么写的话,就可以到对应的目录去寻找。\x0d\x0a对于maven中存在classifier的,例如\x0d\x0a\x0d\x0aorg.apache.hadoop\x0d\x0ahadoop-hdfs\x0d\x0a2.2.0\x0d\x0atests\x0d\x0a\x0d\x0a\x0d\x0a就加一个-D参数就可以了,如下:\x0d\x0amvn install:install-file -Dfile=hadoop-hdfs-2.2.0-tests.jar -DgroupId=org.apache.hadoop -DartifactId=hadoop-hdfs -Dversion=2.2.0 -Dclassifier=tests -Dpackaging=jar

步骤:

1.cmd命令进入该jar包所在路径

2.执行命令:

mvn install:install-file -Dfile=lucene-queryparser-4.6.1.jar

-DgroupId=org.apache.lucene -DartifactId=lucene-queryparser

-Dversion=4.6.1 -Dpackaging=jar

其中:-DgroupId和-DartifactId的作用是指定了这个jar包在repository的安装路径,只是用来告诉项目去这个路径下寻找这个名称的jar包。

比如:

mvn install:install-file -Dfile=hadoop-hdfs-2.2.0.jar

-DgroupId=org.apache.hadoop -DartifactId=hadoop-hdfs -Dversion=2.2.0 -D

-Dpackaging=jar

就是指把hadoop-hdfs-2.2.0.jar安装到repository\org.apache.hadoop\hadoop-hdfs\2.2.0目录下,执行完命令后,如果需要在项目中使用这个jar,则在pom.xml中添加如下配置即可:

<dependency>

<groupId>org.apache.hadoop</groupId>

<artifactId>hadoop-hdfs</artifactId>

<version>2.2.0</version>

</dependency>

注意在每个参数前有个-D

二、怎么在pom.xml中添加项目中libs下的jar呢,而不是从本地仓库中添加?

1、首先将要添加的jar包复制到项目中的libs文件夹下

2、然后在pom.xml中添加如下代码:

[html] view plain copy

<dependency>

<groupId>htmlunit</groupId>

<artifactId>htmlunit</artifactId>

<version>2.21-OSGi</version>

<scope>system</scope>

<systemPath>${project.basedir}/libs/htmlunit-2.21-OSGi.jar</systemPath>

</dependency>

注意scope元素和systemPath元素,其中systemPath元素指定的就是jar包在项目中的路径。

注意libs文件夹下的这个jar包不需要Add to Build Path


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11734920.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-18
下一篇 2023-05-18

发表评论

登录后才能评论

评论列表(0条)

保存