Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to specify a repository for a dependency in Maven

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.

like image 811
Dario Castañé Avatar asked Jul 02 '10 18:07

Dario Castañé


People also ask

Which file is used to define dependency in Maven repository?

Which file is used to define dependency in maven? Explanation: pom. xml is used to define dependency which is used to package the jar. POM stands for project object model.


2 Answers

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.

like image 157
Pascal Thivent Avatar answered Sep 28 '22 05:09

Pascal Thivent


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.

like image 41
Tobias Liefke Avatar answered Sep 28 '22 05:09

Tobias Liefke