简体   繁体   English

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

[英]Maven - deploy dependencies to remote repository

I have a few projects with LOTS of maven dependencies. 我有一些项目有很多maven依赖项。 When I invoke the command mvn deploy (or some variation of it), I would like to not only have the project itself deployed to the remote repository, but also all of its dependencies as well. 当我调用命令mvn deploy(或其中的一些变体)时,我不仅要将项目本身部署到远程存储库,还要将其所有依赖项部署到远程存储库。 Is this possible? 这可能吗? I see many 'similar questions' on this site, but I can't seem to find anything that is as simply put as this. 我在这个网站上看到了许多“类似的问题”,但我似乎无法找到任何简单的东西。 Everything else I've seen seems to expect some additional functionality. 我见过的其他所有东西似乎都期待一些额外的功能。 I simply want to deploy my project, plus all of its dependencies to the remote repo. 我只是想将我的项目及其所有依赖项部署到远程仓库。 I'm using the maven compiler plugin 1.5 我正在使用maven编译器插件1.5

This is a snippet of my settings.xml. 这是我的settings.xml的片段。 Any idea what I'm missing? 知道我错过了什么吗?

<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>

Thanks in advance 提前致谢
~j 〜Ĵ

I propose the following solution which looks a lot less like trial and error for resolving dependencies compared to the existing answers. 我提出了以下解决方案,与现有答案相比,它看起来更像是试验和错误解决依赖关系。

What you can do is to call mvn -Dmdep.copyPom=true dependency:copy-dependencies after deploying the main project. 你可以做的是在部署主项目后调用mvn -Dmdep.copyPom=true dependency:copy-dependencies This will copy transitively all dependencies of your project to target/dependency including their respective pom files. 这会将项目的所有依赖项传递给target/dependency包括它们各自的pom文件。

You can then iterate through all of the dependencies and deploy them to the repository using deploy:deploy-file , eg with such a bash loop: 然后,您可以使用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"

Here's how it works in a nutshell, assuming your remote repository (Nexus, or Artifactory, or the like) and settings.xml are configured correctly. 下面是它的工作原理,假设您的远程存储库(Nexus,或Artifactory等)和settings.xml配置正确。

Let's say you have a project with one dependency on commons-logging . 假设您有一个项目,其中一个依赖于commons-logging When Maven resolves your project's dependencies as part of a build, it does these steps: 当Maven将项目的依赖项作为构建的一部分解析时,它会执行以下步骤:

  1. Checks local repo for commons-logging . 检查本地仓库以进行commons-logging
  2. If found, done. 如果找到,完成。 Continue with build. 继续构建。
  3. If not found: checks for commons-logging in the remote repo. 如果未找到:检查远程仓库中的commons-logging
  4. If found, download artifact to local repo. 如果找到,请将工件下载到本地仓库。 Done; 完成; continue with build. 继续构建。
  5. If not found in remote repo: remote repo contacts central to download commons-logging . 如果在远程仓库中找不到: 远程仓库联系人中心下载commons-logging Then it's available for Maven to download to local repo. 然后它可供Maven下载到本地仓库。 Done; 完成; continue with build. 继续构建。

At the end of these steps, commons-logging should be in both your local and remote repos with nothing further to do. 在这些步骤结束时, commons-logging应该在您的本地和远程存储库中,无需进一步操作。 If this is not the case, then either your settings.xml is not configured to connect to the remote repo when searching for dependencies (is it contacting central directly?) or Nexus isn't configured correctly. 如果不是这种情况,那么在搜索依赖项时,您的settings.xml是否未配置为连接到远程仓库(是否直接与中心联系?)或未正确配置Nexus。

---- Edit ---- ---- 编辑 ----

Here's a snippet of my settings.xml that works. 这是我的settings.xml的一个片段。 @Raghuram gave you a good tip when he suggested you enable both profiles; 当他建议你启用两个配置文件时,@ Raghuram给了你很好的建议; if you somehow enabled only the public-snapshots profile your builds would continue to hit maven central directly. 如果你以某种方式只启用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>

Note the activeProfiles element at the bottom; 注意底部的activeProfiles元素; that's how to ensure you'll use Nexus instead of Maven central with every mvn command. 这就是如何确保你在每个mvn命令中使用Nexus而不是Maven central。

You still need to make sure Nexus is configured so that the URL defined in the <mirror> includes content from Maven central, but how to configure Nexus would be a separate question. 您仍然需要确保配置Nexus,以便<mirror>定义的URL包含来自Maven central的内容,但如何配置Nexus将是一个单独的问题。

Reference: Nexus docs for Maven configuration 参考: 用于Maven配置的Nexus文档

You could start with a clean local repository, attempt to build your application and for any dependency failure, deploy that application to your corporate repository. 您可以从一个干净的本地存储库开始,尝试构建您的应用程序以及任何依赖性失败,将该应用程序部署到您的公司存储库。 This would ensure that all the dependencies that your application needs resides in the corporate repository prior to your application getting built and deployed. 这将确保在构建和部署应用程序之前,应用程序所需的所有依赖项都驻留在公司存储库中。

Prior to this, you would configure your local repository to mirror central and other well-known repositories, so that open source third-party libraries automatically get into your remote repository instead of having to be manually uploaded. 在此之前,您将配置本地存储库以镜像central和其他众所周知的存储库,以便开源第三方库自动进入您的远程存储库,而不必手动上载。

It may turn out that you may not have too many third-party libraries which you would need to manually deploy. 可能会发现您可能没有太多需要手动部署的第三方库。

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

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