How to specify a repository for a dependency in Maven

天大地大妈咪最大 提交于 2019-12-04 22:11:35

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!