【问题标题】:maven setup another repository for certain dependencymaven 为某些依赖项设置另一个存储库
【发布时间】:2013-06-05 20:07:59
【问题描述】:

我有 Maven2 项目。除一个之外的所有依赖项都从公共存储库http://repo.maven.apache.org/maven2/ 下载。

但我有 1 个依赖项,需要从公司内部的存储库中下载(我们使用 Sonatype Nexus 来存储此依赖项)。

另外,我不想在我的内部存储库上创建公共存储库的完整副本。

此时我在 pom.xml 中:

<url>http://maven.apache.org</url>

<repositories>
    <repository>
        <id>thirdparty</id>
        <url>http://<my_nexus_server_ip>:8081/nexus/content/repositories/thirdparty</url>
    </repository>
</repositories>

所以,在构建过程中,我看到了很多垃圾消息(在这种情况下,第一行是垃圾):

Downloading: http://<my_nexus_server_ip>:8081/nexus/content/repositories/thirdparty/ant/ant/1.6.5/ant-1.6.5.pom
Downloading: http://repo.maven.apache.org/maven2/ant/ant/1.6.5/ant-1.6.5.pom
Downloaded: http://repo.maven.apache.org/maven2/ant/ant/1.6.5/ant-1.6.5.pom (861 B at 3.2 KB/sec)  

我想明确指出 Maven 对于哪些依赖项必须使用内部存储库,而对于其他依赖项忽略它(并指出对于其他依赖项 Maven2 必须使用公共存储库)。

您能帮忙在 Maven 中实现这种行为吗?

提前致谢!

【问题讨论】:

    标签: maven-2 dependencies dependency-management repository


    【解决方案1】:

    根据this answer,无法为某些已声明的依赖项设置特定的存储库。

    【讨论】:

      【解决方案2】:

      您需要将 Nexus 中的公共存储库组配置为您的 Maven 构建中唯一使用的一个,如下所示:

      <settings>
        <mirrors>
          <mirror>
            <!--This sends everything else to /public -->
            <id>nexus</id>
            <mirrorOf>*</mirrorOf>
            <url>http://localhost:8081/nexus/content/groups/public</url>
          </mirror>
        </mirrors>
        <profiles>
          <profile>
            <id>nexus</id>
            <!--Enable snapshots for the built in central repo to direct -->
            <!--all requests to nexus via the mirror -->
            <repositories>
              <repository>
                <id>central</id>
                <url>http://central</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
              </repository>
            </repositories>
           <pluginRepositories>
              <pluginRepository>
                <id>central</id>
                <url>http://central</url>
                <releases><enabled>true</enabled></releases>
                <snapshots><enabled>true</enabled></snapshots>
              </pluginRepository>
            </pluginRepositories>
          </profile>
        </profiles>
        <activeProfiles>
          <!--make the profile active all the time -->
          <activeProfile>nexus</activeProfile>
        </activeProfiles>
      </settings>
      

      您必须在 nexus 中设置一个单独的存储库,就像您描述的名为 ThirdParty 的存储库一样,并将此存储库添加到公共存储库组的配置中。此外,您需要将一个依赖项上传到该特定存储库中。除此之外,您还必须使用 releaseSNAPSHOT 存储库,这意味着您需要在公司主 pom 文件中相应地配置您的 distributionManagement。

      【讨论】:

      • 我们使用上面提到的 Sonatype。我们将它用于我们的私有组件管理。但是我们的项目有很多 Maven 从公共存储库下载的外部依赖项。我需要告诉 Maven 仅将我们的内部存储库用于 1 个依赖项,而将其公开用于其他依赖项。问题出在 Maven 日志中。我不希望看到很多 Maven 尝试从错误的存储库(我们的公共存储库中的包和内部存储库中的外部包)下载依赖项。我认为它就像 Maven 配置问题。
      • 啊。对我的网站有误解。相应地更新了答案。
      猜你喜欢
      • 2011-03-26
      • 1970-01-01
      • 2018-07-22
      • 2012-01-10
      • 1970-01-01
      • 2017-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多