【问题标题】:How to upload jars with different classifiers and same pom.xml on Nexus 2 from REST API with curl?如何使用 curl 从 REST API 在 Nexus 2 上上传具有不同分类器和相同 pom.xml 的 jars?
【发布时间】:2021-05-12 06:12:03
【问题描述】:

我已经为此苦苦挣扎了一段时间。

背景:我们有一些生成 jar 的 Jenkins 作业和其他将 jar 上传到 nexus 的作业。在我们的例子中,我们正在寻找myJar-1.0.jarmyJar-1.0-myClassifier.jar。显然它们都是使用相同的pom.xml 文件生成的。

我想要实现的是通过 REST API 和 curl 命令使用 pom.xml 将它们上传到 Nexus(我们使用 Nexus2)。 pom.xml 文件如下所示:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>my.group.id</groupId>
    <artifactId>myJar</artifactId>
    <version>1.0</version>
    <name>My Name</name>
    <packaging>pom</packaging>
    <properties>
          <otherJar.version>1.5</otherJar.version>
          ...
    </properties>
    <dependencies>
          <dependency>
                    <groupId>x.y</groupId>
                    <artifactId>art</artifactId>
                    <version>{$otherJar.version}</version>
          </dependency>
          ...
    </dependencies>
</project>

我用来上传的curl命令(没有分类器的jar)是:

curl -v -F r=releases -F hasPom=true -F e=jar -F file=@pom.xml -F file=@myJar-1.0.jar -u user:pass http://link/to/nexus/service/local/artifact/maven/content

这按预期工作。 jar 使用 pom.xml 文件中提到的 groupid、artifactid 和 version 上传到 nexus。


我的问题是 - 在我上传这个 jar 之后,如果我们有相同的 pom.xml,我如何上传带有 classifier 的 jar?

  • 我应该更改pom.xml 文件吗?如果有,怎么做?
  • 我应该修改 curl 命令吗?我尝试添加-F c=myClassifier,但没有奏效。这导致 Nexus 产生错误:&lt;html&gt;&lt;body&gt;&lt;error&gt;Repository with ID='releases' does not allow updating artifacts.&lt;/error&gt;&lt;/body&gt;&lt;/html&gt;(因为已经有一个具有相同 groupid、artifactid 和版本的工件 - 我刚刚上传的那个;似乎忽略了分类器)

【问题讨论】:

  • 为什么要在 Jenkins 中构建和部署单独的作业。 Maven 可以毫无问题地进行部署,一切都会正常工作......使用 Curl 等没有麻烦。
  • 对这个问题的简短回答是——这很复杂:) 为了提出这个问题,我试图尽可能地简化上下文。很遗憾,我们目前无法更改工作的定义方式。
  • 所有工件和 pom 应一次性上传。由于这是 NXRM 2,您可以使用 UI 进行测试,同时观察 HTTP 流量以确定正确的参数:blog.sonatype.com/…

标签: maven curl nexus


【解决方案1】:

所以我尝试了多种解决方案,但无法通过一次或多次调用找到满足我需求的解决方案(通过 REST API 上传具有相同 pom 和不同分类器的多个 jar)。

我最终在运行我的脚本并使用 maven deploy:deploy-file 的机器上安装了 maven,如下所示:

maven deploy:deploy-file
    -DpomFile=pom.xml
    -Dfile=myJar-1.0.jar
    -Dpackaging=jar                    // this doesn't have to be specified, it will be taken from the pom file, however in my case the pom was specifying <packaging>pom</packaging> and the jar ended up with .pom extention
    -Dclassifiers=myClassifier         // here you can define multiple classifiers, comma-separated
    -Dtypes=jar                        // same as above
    -Dfiles=myJar-1.0-myClassifier.jar // same as above
    -DrepositoryId=nexus
    -Durl=http://host:port/nexus/content/repositories/releases

您可以找到完整的文档here

另外,请注意,您必须在 maven 的配置文件 (maven_home/conf/settings.xml) 中定义您的 nexus 存储库凭据。 在该文件中找到节点 servers 并添加类似以下内容:

    <server>
      <id>nexus</id>
      <username>yourUserName</username>
      <password>yourPass</password>
    </server>

最后,我想强调一个事实,即我认为这是 Nexus2 的 REST API 缺少的功能/错误。从上面的 sn-p 可以看出,这可以通过使用 maven 上传 jar 来实现,也可以从 Nexus 界面手动实现。但是,我没有设法通过 REST API 做到这一点。这可能是因为我没有找到正确的调用方式,但我找不到(almost) any documentation either.. 真是太糟糕了。

另外,这个问题的最后一点 - 我会留下这个未解决的问题,因为这不能回答我原来的问题(如何通过 REST API 来实现?)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-26
    • 2016-03-10
    • 2015-11-27
    • 2017-08-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多