java 将 Maven 工件部署到具有不同设置的多个存储库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13564180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:14:11  来源:igfitidea点击:

Deploying Maven artifact to multiple repositories with different settings

javamavenjenkinsnexus

提问by Siaynoq

We have numerous Java projects, which are CI built with Jenkins. These are deployed to our own Nexus server just fine. The problem is, we need to provide these libraries to a third party, but without the source code. So for each project, in Nexus we have:

我们有很多 Java 项目,都是用 Jenkins 构建的 CI。这些部署到我们自己的 Nexus 服务器就好了。问题是,我们需要将这些库提供给第三方,但没有源代码。所以对于每个项目,在 Nexus 中我们有:

  • Releasesrepository for our devs (includes deployed source code)
  • Snapshotsrepositories for our devs (includes deployed source code)
  • Third party releaserepository (only JAR + POM)
  • (and would be good to have): Third party snapshotrepository (only JAR + POM) for third party nightly builds
  • 为我们的开发人员发布存储库(包括部署的源代码)
  • 我们开发人员的快照存储库(包括部署的源代码)
  • 第三方发布存储库(仅限 JAR + POM)
  • (并且最好拥有):第三方每晚构建的第三方快照存储库(仅限 JAR + POM)

The question is: how is this usually handled in Jenkins/Nexus world? I'd prefer to have one single Job in Jenkins which handles the CI build and the release (artefact deployment) process "automatically". Currently I'm using multiple <distributionManagement>profiles in our "main root pom.xml" (included by all projects):

问题是:在 Jenkins/Nexus 世界中,这通常是如何处理的?我更喜欢在 Jenkins 中拥有一个单独的 Job,它可以“自动”处理 CI 构建和发布(人工制品部署)过程。目前我<distributionManagement>在我们的“主根 pom.xml”中使用了多个配置文件(包含在所有项目中):

[...]
<profiles>
    <profile>
        <id>default</id>
        <distributionManagement>
            <repository>
                <id>releases</id>
                <name>Release</name>
                <url>http://path/to/nexus/content/repositories/releases/</url>
            </repository>
            <snapshotRepository>
                <id>snapshots</id>
                <name>Snapshot</name>
                <url>http://path/to/nexus/content/repositories/snapshots/</url>
                <uniqueVersion>false</uniqueVersion>
            </snapshotRepository>
        </distributionManagement>
    </profile>
    <profile>
        <id>third-party</id>
        <distributionManagement>
            <repository>
                <id>releases</id>
                <name>Release</name>
                <url>http://path/to/nexus/content/repositories/third-party/</url>
            </repository>
            <snapshotRepository>
                <id>snapshots</id>
                <name>Snapshot</name>
                <url>http://path/to/nexus/content/repositories/third-party-snapshots/</url>
                <uniqueVersion>false</uniqueVersion>
            </snapshotRepository>
        </distributionManagement>
    </profile>
</profiles>

From the Maven docs, it seems to be no way of using multiple repositories during the same build lifecycle, not to mention the fact that we need/don't need the source based on the target repo.

从 Maven 文档来看,似乎无法在同一个构建生命周期中使用多个存储库,更不用说我们需要/不需要基于目标存储库的源的事实。

I can do a trick with creating a Job in Jenkins, with the Maven "Goals and options": clean deploy -P third-partyand then adding the Post-build action - "Deploy artifacts to Maven repository" with the "default" data - but in this case, only SNAPSHOTs are going to both repo and artefacts released via Jenkins Maven Release Plug-inare going into one repository only.

我可以用 Maven 的“目标和选项”在 Jenkins 中创建一个作业来做一个技巧:clean deploy -P third-party然后添加构建后操作 - “将工件部署到 Maven 存储库”和“默认”数据 - 但在这种情况下,只有SNAPSHOT将进入存储库,通过Jenkins Maven 发布插件发布的人工制品仅进入一个存储库。

Any practical ideas how can I do this without overcomplicating our CI job hierarchy?

任何实用的想法如何在不使我们的 CI 工作层次结构过于复杂的情况下做到这一点?

Thanks in advance!

提前致谢!

采纳答案by Manfred Moser

You can just handle this all in Nexus. Create a repository target that contains a pattern like the one used in the preconfigured example "All but sources (Maven 2)" and narrow that target down even further with another pattern that restricts the groupid, artifactid and maybe even version.

您可以在 Nexus 中处理这一切。创建一个存储库目标,其中包含与预配置示例“除源之外的所有(Maven 2)”中使用的模式类似的模式,并使用限制 groupid、artifactid 甚至版本的另一种模式进一步缩小该目标的范围。

Then create a privilege that uses that repository target and assign it to the user or role you want to have the respective access.

然后创建一个使用该存储库目标的权限,并将其分配给您希望具有相应访问权限的用户或角色。

No need to do multiple deployments or some such..

无需进行多次部署或诸如此类..

See http://books.sonatype.com/nexus-book/reference/repository-targets.html

http://books.sonatype.com/nexus-book/reference/repository-targets.html

回答by Piotr Gwiazda

You can use Maven Wagon Pluginand upload a single jar to a remote location on deploy phase.

您可以使用Maven Wagon 插件并在部署阶段将单个 jar 上传到远程位置。