首页 文章

从vanilla Maven切换到Jenkins Artifactory插件后出现错误'Not authorized'

提问于
浏览
0

我正在尝试使用Jenkins Artifactory插件来运行Maven构建并将buildInfo发布到我的Artifactory实例 . 所有这些都在Jenkinsfile中编写脚本并作为管道构建执行 . 代码如下:

def server
def buildInfo
def rtMaven

server = Artifactory.server('arty')

rtMaven = Artifactory.newMavenBuild()
rtMaven.tool = 'Maven 3.3.9' // Tool name from Jenkins configuration
rtMaven.deployer releaseRepo: 'ci-test', snapshotRepo: 'ci-test', server: server
rtMaven.resolver releaseRepo: 'libs-release', snapshotRepo: 'libs-snapshot', server: server
rtMaven.deployer.deployArtifacts = false // Disable artifacts deployment during Maven run

buildInfo = Artifactory.newBuildInfo()

rtMaven.run pom: 'pom.xml', goals: "-e -B install".toString(), buildInfo: buildInfo
server.publishBuildInfo buildInfo

这会导致许多错误,类似于以下内容

[main] ERROR org.apache.maven.cli.MavenCli - 插件org.apache.maven.plugins:maven-install-plugin:2.4或其中一个依赖项无法解析:无法读取org.apache的工件描述符.maven.plugins:maven-install-plugin:jar:2.4:无法传输工件org.apache.maven.plugins:maven-plugins:pom:23 from / to artifactory-release(https://arty.example.com :8443 / artifactory / libs-release):未授权,ReasonPhrase:未经授权 . - > [帮助1]

只有当我切换到使用Artifactory插件时才会出现错误 .

在尝试此操作之前,我确认在Jenkins中正确配置了Maven . 像下面的东西工作得很好:

withMaven(maven: 'Maven 3.3.9') {
    sh "mvn -e -B ${args}"
}

执行 mvn deploy 会将war文件推送到Artifactory .

我怀疑它与我的Maven设置有关 . 我已经验证Maven正在作为Jenkins工具正常工作 . 我可以在.m2文件夹中提供settings.xml,或者通过Jenkins提供配置文件,例如:

withMaven(maven: 'Maven 3.3.9', mavenSettingsConfig: '10452c41-5bdb-4d11-b711-9b2d00751c2e')

这两个选项都有效(如果我删除它们会导致预期失败) .

我注意到的是Artifactory插件允许我指定Jenkins工具的名称( Maven 3.3.9 )但是 not mavenSettingsConfig . 为此,我还尝试将调用包装到configFileProvider块中的 rtMaven.run 并将设置传递给mvn:

configFileProvider(
  [configFile(fileId: '10452c41-5bdb-4d11-b711-9b2d00751c2e', variable: 'MAVEN_SETTINGS')]) {
    rtMaven.run pom: 'pom.xml', goals: "-s $MAVEN_SETTINGS -e -B install".toString(), buildInfo: buildInfo
    server.publishBuildInfo buildInfo
    }

这也证明是不成功的 .

在这一点上,任何建议都会有所帮助!

Version info:
詹金斯:2.89.3
Jenkins Artifactory插件:2.13.1
Maven:3.3.9
Artifactory:5.8.3 Pro

settings.xml

<?xml version="1.0" encoding="UTF-8"?>
<settings xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.1.0 http://maven.apache.org/xsd/settings-1.1.0.xsd" xmlns="http://maven.apache.org/SETTINGS/1.1.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <localRepository>/home/jenkins-user/.m2/repository</localRepository> 
  <servers>
    <server>
      <username>artyuser</username>
      <password>ARTYPASSHASH</password>
      <id>central</id>
    </server>
    <server>
      <username>artyuser</username>
      <password>ARTYPASSHASH</password>
      <id>snapshots</id>
    </server>
    <server>
      <username>artyuser</username>
      <password>ARTYPASSHASH</password>
      <id>arty</id>
    </server>
  </servers>
  <profiles>
    <profile>
      <repositories>
        <repository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>libs-release</name>
          <url>https://arty.example.com:8443/artifactory/libs-release</url>
        </repository>
        <repository>
          <snapshots />
          <id>snapshots</id>
          <name>libs-snapshot</name>
          <url>https://arty.example.com:8443/artifactory/libs-snapshot</url>
        </repository>
        <repository>
          <snapshots />
          <id>arty</id>
          <name>ci-test</name>
          <url>https://arty.example.com:8443/artifactory/ci-test</url>
        </repository>
      </repositories>
      <pluginRepositories>
        <pluginRepository>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <id>central</id>
          <name>plugins-release</name>
          <url>https://arty.example.com:8443/artifactory/plugins-release</url>
        </pluginRepository>
        <pluginRepository>
          <snapshots />
          <id>snapshots</id>
          <name>plugins-snapshot</name>
          <url>https://arty.example.com:8443/artifactory/plugins-snapshot</url>
        </pluginRepository>
      </pluginRepositories>
      <id>artifactory</id>
    </profile>
  </profiles>
  <activeProfiles>
    <activeProfile>artifactory</activeProfile>
  </activeProfiles>
</settings>

1 回答

  • 1

    在这种情况下,您在Jenkins配置Configure System中指定了Artifactory Server "arty" . 在那里,您还需要添加凭据 .

    如果您不想使用Jenkins配置,您可以对管道中的所有信息进行硬编码 def server = Artifactory.newServer url: 'artifactory-url', username: 'username', password: 'password'

    或者从Jenkins Credentials获取凭证 def server = Artifactory.newServer url: 'artifactory-url', credentialsId: 'credential'

相关问题