104

In projects with several dependencies and repositories, the try-and-error approach of Maven for downloading dependencies is a bit cumbersome and slow, so I was wondering if there is any way to set an specific repo for some declared dependencies.

For example, I want for bouncycastle to check directly BouncyCastle's Maven repo at http://repo2.maven.org/maven2/org/bouncycastle/ instead of official Maven.

3 Answers 3

59

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.

4
  • 1
    Thanks! I was sure it wasn't possible but I wanted to know from other Maven users. I just reduced the defined repos and created a local one. Jul 3, 2010 at 0:10
  • 14
    This would be a good addition to Maven, and I have asked for it. It would enable Maven to work in the github world.
    – TimP
    Jul 4, 2014 at 12:42
  • 2
    As of January 2019, this is now possible in Gradle (although I've not tried it yet). See stackoverflow.com/a/54465569
    – Net Wolf
    Jun 14, 2019 at 19:48
  • Gradle has exclusiveContent allow retrieval dep from a single repository
    – Igor
    May 22 at 17:14
27

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.

-1

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>
1
  • 6
    This is not the solution, this way Maven will always try to download every dependency from both repositories.
    – lubrum
    Feb 2, 2021 at 21:27

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.