我希望 jar 位于源代码控制中的 3rdparty 库中,并通过 pom.xml 文件中的相对路径链接到它。
如果您真的想要这个(请理解,如果您不能使用公司存储库),那么我的建议是使用项目本地的“文件存储库”并且不使用 system 范围依赖。应该避免system 作用域,这种依赖在许多情况下(例如在汇编中)不能很好地工作,它们带来的麻烦多于好处。
因此,改为声明项目本地的存储库:
<repositories>
<repository>
<id>my-local-repo</id>
<url>file://${project.basedir}/my-repo</url>
</repository>
</repositories>
使用install:install-file 和localRepositoryPath 参数在其中安装您的第三方库:
罢工>
mvn install:install-file -Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
更新: 使用 2.2 版插件时,install:install-file 似乎忽略了 localRepositoryPath。但是,它适用于 2.3 及更高版本的插件。所以使用插件的全限定名来指定版本:
mvn org.apache.maven.plugins:maven-install-plugin:2.3.1:install-file \
-Dfile=<path-to-file> -DgroupId=<myGroup> \
-DartifactId=<myArtifactId> -Dversion=<myVersion> \
-Dpackaging=<myPackaging> -DlocalRepositoryPath=<path>
maven-install-plugin documentation
最后,像任何其他依赖项一样声明它(但没有system 范围):
<dependency>
<groupId>your.group.id</groupId>
<artifactId>3rdparty</artifactId>
<version>X.Y.Z</version>
</dependency>
恕我直言,这是一个比使用 system 范围更好的解决方案,因为您的依赖项将被视为一个好公民(例如,它将被包含在程序集中等等)。
现在,我不得不提一下,在公司环境中处理这种情况的“正确方法”(可能不是这里的情况)是使用公司存储库。