如何将本地jar文件(还不是Maven存储库的一部分)直接添加到项目的库源中?


当前回答

在POM文件中添加您自己的本地JAR,并在maven构建中使用它。

mvn install:install-file -Dfile=path-to-jar -DgroupId=owngroupid -DartifactId=ownartifactid -Dversion=ownversion -Dpackaging=jar

例如:

mvn install:install-file -Dfile=path-to-jar -DgroupId=com.decompiler -DartifactId=jd-core-java -Dversion=1.2 -Dpackaging=jar

然后将其添加到POM中,如下所示:

其他回答

请注意,使用本地回购并不一定是个好主意。如果这个项目与其他人共享,那么当它不起作用时,其他人都会遇到问题和疑问,甚至在您的源代码管理系统中也无法使用jar!

尽管共享回购是最好的解决方案,但如果由于某种原因无法做到这一点,那么嵌入jar比本地回购要好。仅限本地的回购内容可能会导致很多问题,特别是随着时间的推移。

在本地存储库中,您可以通过发出以下命令来安装jar

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

点击这个有用的链接,从mkyoung的网站进行同样的操作。您也可以查看maven指南

真正快速而肮脏的方法是指向本地文件,请注意“system”现在已被弃用:

<dependency>
    <groupId>com.sample</groupId>  
    <artifactId>samplifact</artifactId>  
    <version>1.0</version> 
    <scope>system</scope>
    <systemPath>C:\DEV\myfunnylib\yourJar.jar</systemPath>
</dependency>

然而,这只会存在于您的机器上(显然),因为共享通常使用适当的m2存档(nexus/artifactory)是有意义的,或者如果您没有这些存档或不想设置本地maven结构化存档并在pom中配置“存储库”:

本地:

<repositories>
    <repository>
        <id>my-local-repo</id>
        <url>file://C:/DEV//mymvnrepo</url>
    </repository>
</repositories>

远程:

<repositories>
    <repository>
        <id>my-remote-repo</id>
        <url>http://192.168.0.1/whatever/mavenserver/youwant/repo</url>
    </repository>
</repositories>

对于此解决方案,还可以使用basedir变量创建相对路径:

<url>file:${basedir}</url>

依赖关系的重要部分是:${pom.basedir}(而不仅仅是${basedir})

<dependency>
    <groupId>org.example</groupId>
    <artifactId>example</artifactId>
    <version>1.0</version>
    <scope>system</scope>
    <systemPath>${pom.basedir}/src/lib/example.jar</systemPath>
</dependency>

在Apache Maven 3.5.4中,我必须添加双引号。如果没有双重报价,这对我来说是行不通的。

例子:mvn安装:安装文件“-Dfile=jar文件的位置”“-DgroupId=组id”“-DastifactId=工件id”“-D版本=版本”“-Dpackage=包类型”