【问题标题】:Deploying to a Nexus OSS 2.12 Server from Maven 3从 Maven 3 部署到 Nexus OSS 2.12 服务器
【发布时间】:2023-03-14 14:35:01
【问题描述】:

每当我尝试使用 nexus-staging-maven-plugin 从 maven 3.05 将工件部署到我的 Nexus OSS 2.12 服务器时,我都会收到一条错误消息:

[ERROR] Failed to execute goal org.sonatype.plugins:nexus-staging-maven-plugin:1.6.7:deploy (default-deploy) on project myproject
Failed to deploy artifacts: Could not transfer artifact com myproject:myproject:jar:0.0.1-20160325.164052-1 from/to snapshots (http://nexus.myproject.com/content/repositories/snapshots)
Failed to transfer file: http://nexus.myproject.com/content repositories/snapshots/com/myproject/myproject/0.0.1-SNAPSHOT/myproject-0.0.1-20160325.164052-1.jar. 
Return code is: 401, ReasonPhrase: Unauthorized.

现在奇怪的是,我可以使用 cURL 和手动指定的凭据来部署 pom:

curl -u deployment:deployment123 http://nexus.myproject.com/content/repositories/snapshots/com/myproject/myproject/0.0.1-SNAPSHOT/myproject-0.0.1.pom --request PUT --data @pom.xml

我知道我的~/.m2/settings.xml 文件中配置了服务器凭据。这是我的:

<?xml version="1.0" encoding="UTF-8" ?>
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
    <servers>
        <server>
            <id>nexus</id>
            <username>deployment</username>
            <password>deployment123</password>
        </server>
    </servers>
    <mirrors>
        <mirror>
            <id>central</id>
            <url>http://nexus.myproject.com/content/repositories/central/</url>
            <mirrorOf>*</mirrorOf>
        </mirror>
    </mirrors>
</settings>

请注意,该文件中的 mirrors 部分运行良好 - 当我构建时,maven 使用匿名用户从 nexus 服务器获取我的依赖项。

我在~/.m2/settings.xml 文件中配置的凭据与ID 为nexus 的服务器相关联。这是我在 pom.xml 文件的 plugins 部分中引用的同一台服务器:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-deploy-plugin</artifactId>
    <version>2.8.2</version>
    <configuration>
        <skip>true</skip>
    </configuration>
</plugin>
<plugin>
    <groupId>org.sonatype.plugins</groupId>
    <artifactId>nexus-staging-maven-plugin</artifactId>
    <version>1.6.7</version>
    <extensions>true</extensions>
    <executions>
        <execution>
            <id>default-deploy</id>
            <phase>deploy</phase>
            <goals>
                <goal>deploy</goal>
            </goals>
        </execution>
    </executions>
    <configuration>
        <serverId>nexus</serverId>
        <nexusUrl>http://nexus.myproject.com/</nexusUrl>
    </configuration>
</plugin>

要上传到的存储库在我的 pom 文件的 repositories 部分中配置:

<repositories>
    <repository>
        <id>snapshots</id>
        <url>http://nexus.myproject.com/content/repositories/snapshots</url>
    </repository>
    <repository>
        <id>releases</id>
        <url>http://nexus.myproject.com/content/repositories/releases</url>
    </repository>
</repositories>

最后,通过运行mvn help:effective-settings,我可以确定~/.m2/settings.xml 文件正在被读取。

当我尝试从命令行运行部署时,我使用命令mvn clean deploy -DskipTests=true

回顾一下,我有一个启用了库存部署帐户的 Nexus 服务器。我可以使用该帐户将工件从 cURL 部署到存储库,但是当我尝试使用相同的凭据从 Maven 进行部署时,我得到了 HTTP 401。

有人知道为什么这里可能存在差异吗?

【问题讨论】:

  • 如何调用 Maven 将 atifacts 部署到 Nexus?
  • 你的 pom 中有 &lt;repositories&gt; 部分吗?那看起来像什么? nexus-staging-maven-plugin 有什么用?我以前从来不需要它来使用 Maven 部署到 nexus。
  • @khmarbaise:我运行 mvn clean deploy。对不起,应该把它放在问题中
  • @jpennell 将&lt;repositories&gt; 部分添加到问题中。我使用了nexus-staging-maven-plugin,因为我在关注 Nexus 网站上的一些文档。那是他们的插件。您是否建议使用标准的maven-deploy-plugin 来代替?
  • 您是否正确配置了暂存存储库在 Nexus 中,如果我正确地看到您没有...

标签: java maven curl nexus


【解决方案1】:

我已经能够使用 mvn clean deploy 成功部署工件,只需要以下内容:

pom.xml

...

<properties>
  <nexus.url>http://mynexus.com:8081/nexus/content</nexus.url>
</properties>

<repositories>
  <repository>
    <id>nexus</id>
    <url>${nexus.url}/groups/public</url>
    <snapshots>
      <enabled>true</enabled>
      <updatePolicy>always</updatePolicy>
    </snapshots>
  </repository>
</repositories>

<pluginRepositories>
  <pluginRepository>
    <id>nexus</id>
    <url>${nexus.url}/groups/public</url>
  </pluginRepository>
</pluginRepositories>

<distributionManagement>
  <repository>
    <uniqueVersion>false</uniqueVersion>
    <id>nexus</id>
    <name>Releases</name>
    <url>${nexus.url}/repositories/releases</url>
  </repository>
  <snapshotRepository>
    <uniqueVersion>false</uniqueVersion>
    <id>nexus</id>
    <name>Snapshots</name>
    <url>${nexus.url}/repositories/snapshots</url>
  </snapshotRepository>
</distributionManagement>

...

settings.xml

...

<servers>
    <server>
        <id>nexus</id>
        <username>deployment</username>
        <password>deployment123</password>
    </server>
</servers>

...

我相信部署到 nexus 的重要部分是 &lt;distributionManagement&gt; 部分。我从来不需要明确声明maven-deploy-pluginnexus-staging-maven-plugin

希望有帮助!

【讨论】:

  • 这非常有效。原来我有几个问题:我在我的 settings.xml 中声明了一个 Maven Central 的镜像,我不必这样做,因为 Nexus 为我做了,而且我在我的存储库中使用了错误的 id元素。显然 id 在不同的上下文中意味着不同的东西。
  • 401 表示 Maven 没有将凭据发送到有权发布该凭据的 Nexus。您改进的 pom.xml 在 中有 标记,并且 id 与 settings.xml sn-p 中的 id 匹配。
【解决方案2】:

如果您想使用 Nexus Staging Maven 插件,您需要声明存储库以使用暂存端点,或者您需要显式配置暂存配置文件 ID。在您的情况下,您已经配置了实际的发布存储库而不是登台端点。

查看Maven pluginstaging suite 的文档。我还记录了一个关于它的free video series 以及一个suite of example projects

【讨论】:

    猜你喜欢
    • 2018-11-29
    • 2013-03-06
    • 2017-02-24
    • 2017-04-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多