java 部署maven工件时出现“401未经授权”错误

unguejic  于 2023-01-11  发布在  Java
关注(0)|答案(2)|浏览(545)

根据评论更新:

<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帐户
1.使用jFrog凭据(用户名和密码)初始化文件夹~/.m2/下的settings.xml,并指定发布和快照的URL
1.我在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包中找到。
1.我在my-second-app项目中添加了依赖项,运行mvn install时可以下载依赖项

<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中添加了凭据,我的理解是不需要在项目级别添加任何凭据。

gcuhipw9

gcuhipw91#

Maven配置了在大多数情况下有效的内置默认值,而Nexus和Artifactory等标准的Maven感知存储库服务器只需最小的配置就可以使用它们。
删除maven-deploy-plugin的自定义配置,让它使用默认设置,而是为服务器添加一个包含repositorydistributionManagement节,并确保在~/.m2/settings.xml文件中有匹配的凭据。

a11xaf1n

a11xaf1n2#

在我的例子中,点是id:repository.id和server.id必须相同。
pom.xml:

<distributionManagement>
    <repository>
        <id>cloud-3party</id>
        <name>cloud-3party</name>
        <url>xxx</url>
    </repository>
</distributionManagement>

apaphe-maven-3.6.3/conf/setting.xml

<server>
  <id>cloud-3party</id>
  <username>xxx</username>
  <password>xxx</password>
</server>

相关问题