如何在 Maven 中为依赖项指定存储库

在具有多个依赖项和存储库的项目中,Maven 用于下载依赖项的尝试和错误方法有点麻烦和缓慢,所以我想知道是否有办法为一些已声明的依赖项设置特定的 repo。

例如,我想让 BouncyCastle 在 http://repo2.maven.org/maven2/org/bouncycastle/直接检查 BouncyCastle 的 Maven 回购,而不是正式的 Maven。

84685 次浏览

Not possible. Maven checks the repositories in their declaration order until a given artifact gets resolved (or not).

Some repository manager can do something approaching this though. For example, Nexus has a routes feature that does something equivalent.

I have moved libraries from 3rd party repositories to their own project and included this project as first module in my base project:

base/pom.xml

...
<modules>
<module>thirdparty</module>
<module>mymodule</module>
...
</modules>

base/thirdparty/pom.xml:

...
<artifactId>thirdparty</artifactId>
<packaging>pom</packaging>


<repositories>
<repository>
<id>First thirdparty repository</id>
<url>https://...</url>
</repository>
...
</repositories>


<dependencies>
<dependency>
<!-- Dependency from the third party repository -->
</dependency>
....
</dependencies>

base/mymodule/pom.xml:

<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>thirdparty</artifactId>
<version>${project.version}</version>
<type>pom</type>
</dependency>
...
</dependencies>

This will ensure that the libraries from the thirdparty repository are downloaded into the local repository as soon as the root project is build. For all other dependencies the repositories are not visible and therefore not included when downloading.

This post could be very old but might be useful to someone. I specified the two repositories in pom.xml like below and it worked.

<repositories>
<repository>
<id>AsposeJavaAPI</id>
<name>Aspose Java API</name>
<url>http://repository.aspose.com/repo/</url>
</repository>
<repository>
<id>Default</id>
<name>All apart from Aspose</name>
<url>https://repo.maven.apache.org/maven2/</url>
</repository>
</repositories>