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


当前回答

依赖关系的重要部分是:${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>

其他回答

命令行:

mvn install:install-file -Dfile=c:\kaptcha-{version}.jar -DgroupId=com.google.code
-DartifactId=kaptcha -Dversion={version} -Dpackaging=jar

依赖关系的重要部分是:${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>

如果您是Spring开发人员,您会发现从Spring Boot构建中生成的JAR在另一个构建中不能作为依赖JAR工作。检查您的POM是否具有以下功能:

<plugin>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-maven-plugin</artifactId>
</plugin>

此外,如果运行mvn包,请检查是否在目标/目录中看到.jar原始文件以及.jar。如果是这样的话,您需要将.jar.original文件用于任何本地开发(将其放入lib/或其他文件中)

你当然可以解决这个问题;请参阅参考Spring插件手册的原始帖子。我认为,您需要<configuration><attach>false</attach><configuration>,然后必须进行部署,Maven应该使用.jar.original文件。我自己还没有尝试过,所以请告诉我!

真正快速而肮脏的方法是指向本地文件,请注意“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>

一种方法是将其上传到您自己的Maven存储库管理器(如Nexus)。无论如何,拥有一个自己的存储库管理器是很好的做法。

我最近看到的另一个好方法是在构建生命周期中包含Maven安装插件:在POM中声明将文件安装到本地存储库。这是一个有点但很小的开销,并且不涉及手动步骤。

http://maven.apache.org/plugins/maven-install-plugin/install-file-mojo.html