chenzhao

  • java
  • iOS
  • IT
知识积累
不积跬步无以至千里
  1. 首页
  2. java
  3. 正文

mvn本地jar包

2022年 1月 19日 115点热度 0人点赞 0条评论

From: https://blog.csdn.net/w605283073/article/details/90120722

 

介绍

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

 

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

涉及到的命令

mvn install:install-file -Dfile=

这里没指定JAR 文件的 groupId, artifactId, version 和_packaging信息。_

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

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

mvn install:install-file -Dfile= -DgroupId= -DartifactId= -Dversion=

如:

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

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

<dependency>
        <groupId>com.roufid.tutorials</groupId>
        <artifactId>example-app</artifactId>
        <version>1.0</version>
</dependency>

这种方案的代价非常大。

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

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

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

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

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

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

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

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

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-install-plugin</artifactId>
    <version>2.5</version>
    <executions>
        <execution>
            <phase>initialize</phase>
            <goals>
                <goal>install-file</goal>
            </goals>
            <configuration>
                <groupId>com.roufid.tutorials</groupId>
                <artifactId>example-app</artifactId>
                <version>1.0</version>
                <packaging>jar</packaging>
                <file>${basedir}/lib/app.jar</file>
            </configuration>
        </execution>
    </executions>
</plugin>

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

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

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

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

<dependency>
        <groupId>com.roufid.tutorials</groupId>
        <artifactId>example-app</artifactId>
        <version>1.0</version>
        <scope>system</scope>
        <systemPath>${basedir}/lib/yourJar.jar</systemPath>
</dependency>

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

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

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

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

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

vn deploy:deploy-file -Dfile= -DgroupId= -DartifactId= -Dversion= -Dpackaging=jar

 

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

<repositories>
    <repository>
        <id>maven-repository</id>
        <url>file:///${project.basedir}/maven-repository</url>
    </repository>
</repositories>

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

<dependency>
        <groupId>com.roufid.tutorials</groupId>
        <artifactId>example-app</artifactId>
        <version>1.0</version>
</dependency>

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

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

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

 

英文原文:http://roufid.com/3-ways-to-add-local-jar-to-maven-project/

标签: 暂无
最后更新:2022年 11月 11日

陈昭

IT 程序员

打赏 点赞
< 上一篇
下一篇 >

文章评论

取消回复

COPYRIGHT © 2022 chenzhao. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang