繁体   English   中英

Maven - 将依赖项部署到远程存储库

[英]Maven - deploy dependencies to remote repository

我有一些项目有很多maven依赖项。 当我调用命令mvn deploy(或其中的一些变体)时,我不仅要将项目本身部署到远程存储库,还要将其所有依赖项部署到远程存储库。 这可能吗? 我在这个网站上看到了许多“类似的问题”,但我似乎无法找到任何简单的东西。 我见过的其他所有东西似乎都期待一些额外的功能。 我只是想将我的项目及其所有依赖项部署到远程仓库。 我正在使用maven编译器插件1.5

这是我的settings.xml的片段。 知道我错过了什么吗?

<mirrors>
<mirror>
  <!--This is used to direct the public snapshots repo in the 
      profile below over to a different nexus group -->
  <id>nexus-public-snapshots</id>
  <mirrorOf>public-snapshots</mirrorOf>
  <url>http://{ourServer}/nexus/content/groups/public-snapshots</url>
</mirror>
<mirror>
  <!--This sends everything else to /public -->
  <id>nexus</id>
  <mirrorOf>*</mirrorOf>
  <url>http://{ourServer}/nexus/content/groups/public</url>
</mirror>
  </mirrors>
<profiles>
<profile>
  <id>development</id>
  <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>
<profile>
  <!--this profile will allow snapshots to be searched when activated-->
  <id>public-snapshots</id>
  <repositories>
    <repository>
      <id>public-snapshots</id>
      <url>http://public-snapshots</url>
      <releases><enabled>false</enabled></releases>
      <snapshots><enabled>true</enabled></snapshots>
    </repository>
  </repositories>
 <pluginRepositories>
    <pluginRepository>
      <id>public-snapshots</id>
      <url>http://public-snapshots</url>
      <releases><enabled>false</enabled></releases>
      <snapshots><enabled>true</enabled></snapshots>
    </pluginRepository>
  </pluginRepositories>
</profile>
 </profiles>

提前致谢
〜Ĵ

我提出了以下解决方案,与现有答案相比,它看起来更像是试验和错误解决依赖关系。

你可以做的是在部署主项目后调用mvn -Dmdep.copyPom=true dependency:copy-dependencies 这会将项目的所有依赖项传递给target/dependency包括它们各自的pom文件。

然后,您可以使用deploy:deploy-file遍历所有依赖项并将它们部署到存储库,例如使用这样的bash循环:

for pom in target/dependency/*.pom; do mvn deploy:deploy-file -Durl=http://your/repo -Dfile="${pom%%.pom}.jar" -DgeneratePom=false -DpomFile="$pom"

下面是它的工作原理,假设您的远程存储库(Nexus,或Artifactory等)和settings.xml配置正确。

假设您有一个项目,其中一个依赖于commons-logging 当Maven将项目的依赖项作为构建的一部分解析时,它会执行以下步骤:

  1. 检查本地仓库以进行commons-logging
  2. 如果找到,完成。 继续构建。
  3. 如果未找到:检查远程仓库中的commons-logging
  4. 如果找到,请将工件下载到本地仓库。 完成; 继续构建。
  5. 如果在远程仓库中找不到: 远程仓库联系人中心下载commons-logging 然后它可供Maven下载到本地仓库。 完成; 继续构建。

在这些步骤结束时, commons-logging应该在您的本地和远程存储库中,无需进一步操作。 如果不是这种情况,那么在搜索依赖项时,您的settings.xml是否未配置为连接到远程仓库(是否直接与中心联系?)或未正确配置Nexus。

---- 编辑 ----

这是我的settings.xml的一个片段。 当他建议你启用两个配置文件时,@ Raghuram给了你很好的建议; 如果你以某种方式只启用public-snapshots配置文件,你的构建将继续直接命中maven central。

....
<mirrors>
    <!-- redirects all traffic to internal Nexus repo instead of Maven central -->
    <mirror>
        <id>maven2</id>
        <mirrorOf>*</mirrorOf>
        <url>http://repository.someCompany.com/maven2RepoOrGroupInNexus</url>
    </mirror>
</mirrors>
....
<profiles>
    <profile>
        <id>repo-profile</id>
        <repositories>
            <repository>
                <id>central</id>
                <url>http://gotoNexus</url>  <!-- URL is unimportant here -->
                <snapshots>
                    <enabled>true</enabled>
                    <updatePolicy>daily</updatePolicy>
                </snapshots>
                <releases>
                    <enabled>true</enabled>
                    <updatePolicy>daily</updatePolicy>
                </releases>
            </repository>
        </repositories>
    </profile>
</profiles>
<activeProfiles>
    <activeProfile>repo-profile</activeProfile>  <!-- important -->
</activeProfiles>

注意底部的activeProfiles元素; 这就是如何确保你在每个mvn命令中使用Nexus而不是Maven central。

您仍然需要确保配置Nexus,以便<mirror>定义的URL包含来自Maven central的内容,但如何配置Nexus将是一个单独的问题。

参考: 用于Maven配置的Nexus文档

您可以从一个干净的本地存储库开始,尝试构建您的应用程序以及任何依赖性失败,将该应用程序部署到您的公司存储库。 这将确保在构建和部署应用程序之前,应用程序所需的所有依赖项都驻留在公司存储库中。

在此之前,您将配置本地存储库以镜像central和其他众所周知的存储库,以便开源第三方库自动进入您的远程存储库,而不必手动上载。

可能会发现您可能没有太多需要手动部署的第三方库。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM