问题
如何直接在我的项目的 repository 中添加本地 jar 文件(尚未成为Maven存储库的一部分)?
#1 热门回答(978 赞)
您可以直接添加本地依赖项,如下所示:
<dependency>
<groupId>com.sample</groupId>
<artifactId>sample</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/src/main/resources/yourJar.jar</systemPath>
</dependency>
#2 热门回答(410 赞)
按如下方式将JAR安装到本地Maven存储库中:
mvn install:install-file
-Dfile=<path-to-file>
-DgroupId=<group-id>
-DartifactId=<artifact-id>
-Dversion=<version>
-Dpackaging=<packaging>
-DgeneratePom=true
Where: <path-to-file> the path to the file to load
<group-id> the group that the file should be registered under
<artifact-id> the artifact name for the file
<version> the version of the file
<packaging> the packaging of the file e.g. jar
#3 热门回答(80 赞)
将本地 jar 文件作为依赖项的最佳选择是创建本地 maven 存储库。这样的repo 只是 pom 文件的正确目录结构。
在我的例子中:我在${master_project}
位置有一个主项目,而 subroject1 在 $ {master_project}/$ {subproject1}
上。
那么我正在创建mvn仓库:${master_project} / local-maven-repo
在位于$ {master_project} / $ {subproject1} / pom.xml
的subproject1中的pom文件中,需要指定将文件路径作为url参数的库:
<repositories>
<repository>
<id>local-maven-repo</id>
<url>file:///${project.parent.basedir}/local-maven-repo</url>
</repository>
</repositories>
可以像任何其他存储库一样指定依赖关系。这使您的pom存储库独立。例如,一旦需要的jar在maven central中可用,你只需要从你的本地仓库中删除它,它将从默认的仓库中被取消。
<dependency>
<groupId>org.apache.felix</groupId>
<artifactId>org.apache.felix.servicebinder</artifactId>
<version>0.9.0-SNAPSHOT</version>
</dependency>
最后一件事是使用 -DlocalRepositoryPath
开关将 jar 文件添加到本地存储库,如下所示:
mvn org.apache.maven.plugins:maven-install-plugin:2.5.2:install-file \
-Dfile=/some/path/on/my/local/filesystem/felix/servicebinder/target/org.apache.felix.servicebinder-0.9.0-SNAPSHOT.jar \
-DgroupId=org.apache.felix -DartifactId=org.apache.felix.servicebinder \
-Dversion=0.9.0-SNAPSHOT -Dpackaging=jar \
-DlocalRepositoryPath=${master_project}/local-maven-repo
一旦 jar 文件被安装,mvn repo 就可以被提交给代码库,并且整个设置是独立于系统的。 (working example in github)
我同意让 jar 提交到 maven 仓库并不是一个好的做法,但在现实生活中,快速和肮脏的解决方案有时比完全成熟的nexus repo 更好,以承载一个无法发布的jar。