当前位置:   article > 正文

将本地Jar包导入Maven项目的4种方式_将jar包放入本地maven仓库

将jar包放入本地maven仓库

介绍

本文提供三种将一个自定义的JAR文件添加到你的Maven项目中的方法。

1 手动安装JAR到本地maven仓库

涉及到的命令

mvn install:install-file -Dfile=<path-to-file>

这里没指定JAR 文件的 groupIdartifactIdversion 和packaging信息。

因为Maven-install-plugin 2.5版本以后这些信息可以从指定的pom文件中提取。

当然也可以指定这些信息:

mvn install:install-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version>

如:

mvn install:install-file –Dfile=C:\dev\app.jar -DgroupId=com.roufid.tutorials -DartifactId=example-app -Dversion=1.0

在pom.xml中添加如下依赖,就可以在maven项目中使用了

  1. <dependency>
  2. <groupId>com.roufid.tutorials</groupId>
  3. <artifactId>example-app</artifactId>
  4. <version>1.0</version>
  5. </dependency>

这种方案的代价非常大。

因为你如果修改了本地maven仓库的地址,还得重新安装这个jar文件。

如果有多个人一起开发,每个人都得这么搞一次。

项目的可移植性也是一个需要重点考虑的问题。

另外一种方案是,在pom.xml文件中使用 maven-install-plugin插件,在初始化阶段安装jar包。

要想这么搞,你先得在pom文件中定义jar的位置。

最佳的实践是将jar包和pom.xml文件放在同一级目录(项目根目录)。

假设你放在了<PROJECT_ROOT_FOLDER>/lib/app.jar这里。

则pom.xml文件插件则可以这么写

  1. <plugin>
  2. <groupId>org.apache.maven.plugins</groupId>
  3. <artifactId>maven-install-plugin</artifactId>
  4. <version>2.5</version>
  5. <executions>
  6. <execution>
  7. <phase>initialize</phase>
  8. <goals>
  9. <goal>install-file</goal>
  10. </goals>
  11. <configuration>
  12. <groupId>com.roufid.tutorials</groupId>
  13. <artifactId>example-app</artifactId>
  14. <version>1.0</version>
  15. <packaging>jar</packaging>
  16. <file>${basedir}/lib/app.jar</file>
  17. </configuration>
  18. </execution>
  19. </executions>
  20. </plugin>

${basedir} 表示包含 pom.xml的目录

2-添加system范围的直接引用

另外一种方案是直接采用system 的范围,指定本地jar包的绝对路径。

如果jar报放在 <PROJECT_ROOT_FOLDER>/lib这里

  1. <dependency>
  2. <groupId>com.roufid.tutorials</groupId>
  3. <artifactId>example-app</artifactId>
  4. <version>1.0</version>
  5. <scope>system</scope>
  6. <systemPath>${basedir}/lib/yourJar.jar</systemPath>
  7. </dependency>

${basedir} 表示包含 pom.xml的目录

3- 创建一个新的本地 Maven仓库

第三个方案和第一个很像,不同点在于JAR包安装到另外一个本地maven仓库中。

假设本地仓库名称为:maven-repository。位于${basedir}目录.

先将本地JAR包发布到新的本地仓库中

vn deploy:deploy-file -Dfile=<path-to-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar

然后在pom.xml文件中添加仓库

  1. <repositories>
  2. <repository>
  3. <id>maven-repository</id>
  4. <url>file:///${project.basedir}/maven-repository</url>
  5. </repository>
  6. </repositories>

然后就可以在pom.xml文件中添加依赖了

  1. <dependency>
  2. <groupId>com.roufid.tutorials</groupId>
  3. <artifactId>example-app</artifactId>
  4. <version>1.0</version>
  5. </dependency>

4- 最佳方式:使用Nexus仓库管理器

最好的方法是使用包含你自定义JAR包的Nexus仓库管理器,可将其用下载依赖的远程仓库。

具体用法这里就不详细展开了,自行查询。

英文原文:3 ways to add local jar to maven project - Roufid

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/人工智能uu/article/detail/825533
推荐阅读
相关标签
  

闽ICP备14008679号