部署 Maven 工件时出现“401 Unauthorized”错误

Got "401 Unauthorized" error when deploy a maven artifact

根据评论更新:

  <distributionManagement>
    <repository>
      <id>releases</id>
      <name>Releases</name>
      <url>https://mycompany.jfrog.io/artifactory/my-app-libs-release</url>
    </repository>

    <snapshotRepository>
      <id>snapshots</id>
      <name>Snapshots</name>
      <url>https://mycompany.jfrog.io/artifactory/my-app-libs-snapshot/</url>
    </snapshotRepository>
  </distributionManagement>

原文:

我的目标是尝试端到端的 Maven 存储库部署和依赖项导入。

我正在将一个 maven 项目(项目名称 my-app)工件部署到 jFrog,然后将该工件作为依赖项添加到另一个 maven 项目(项目名称是 my-second-app)

  1. 我已经设置了 jFrog 帐户
  2. 使用 jFrog 凭据(用户名和密码)在文件夹 ~/.m2/ 下初始化 settings.xml 并指定 URL 用于发布和快照
  3. 我在 my-app 项目中添加了以下插件:
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-jar-plugin</artifactId>
          <executions>
            <execution>
              <id>make-a-jar</id>
              <phase>compile</phase>
              <goals>
                <goal>jar</goal>
              </goals>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-install-plugin</artifactId>
          <executions>
            <execution>
              <phase>install</phase>
              <goals>
                <goal>install-file</goal>
              </goals>
              <configuration>
                <packaging>jar</packaging>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>
                          ${project.build.directory}/${project.artifactId}-${project.version}.jar
                      </file>
              </configuration>
            </execution>
          </executions>
        </plugin>
        <plugin>
          <groupId>org.apache.maven.plugins</groupId>
          <artifactId>maven-deploy-plugin</artifactId>
          <executions>
            <execution>
              <phase>deploy</phase>
              <goals>
                <goal>deploy-file</goal>
              </goals>
              <configuration>
                <packaging>jar</packaging>
                <generatePom>true</generatePom>
                <url>https://mycompany.jfrog.io/artifactory/my-app-libs-release</url>
                <artifactId>${project.artifactId}</artifactId>
                <groupId>${project.groupId}</groupId>
                <version>${project.version}</version>
                <file>${project.build.directory}/${project.artifactId}-${project.version}.jar</file>
              </configuration>
            </execution>
          </executions>
        </plugin>
  1. 当我 运行 mvn deploy 时,项目显示 401 Unauthorized 错误,但工件已部署并且可以在 jFrog 包中找到。
  2. 我在项目my-second-app中添加了依赖,当运行 mvn install
  3. 时可以下载依赖
    <dependency>
      <groupId>com.mycompany.app</groupId>
      <artifactId>my-app</artifactId>
      <version>0.0.6-SNAPSHOT</version>
  </dependency>

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.7:deploy-file (default) on project my-app: Failed to retrieve remote metadata com.mycompany.app:my-app:0.0.6-SNAPSHOT/maven-metadata.xml: Could not transfer metadata com.mycompany.app:my-app:0.0.6-SNAPSHOT/maven-metadata.xml from/to remote-repository (https://myurl.jfrog.io/artifactory/my-app-libs-release): authentication failed for https://mycompany.jfrog.io/artifactory/my-app-libs-release/com/mycompany/app/my-app/0.0.6-SNAPSHOT/maven-metadata.xml, status: 401 Unauthorized -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

有人知道我错过了什么吗?为什么我收到 401 未授权错误?我在 settings.xml 中添加了凭据,我的理解是不需要在项目级别添加任何凭据。

感谢任何帮助,提前致谢。

Maven 配置了在大多数情况下都有效的内置默认值,标准的 Maven 感知存储库服务器(如 Nexus 和 Artifactory)将以最少的配置与它们一起工作。

删掉 maven-deploy-plugin 的自定义配置,让它使用默认设置。相反,为您的服务器添加一个带有 repositorydistributionManagement 部分,并确保您的 ~/.m2/settings.xml 文件中有匹配的凭据。