首页 > 解决方案 > Using maven to push an artifact to Gitlab

问题描述

I have a project that used to push artifacts out to Nexus, but we now need to change that to push it to Gitlab. When my CI/CD pipeline tries to do this deployment, it is getting an error of:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-deploy-plugin:2.8.2:deploy 
(default-deploy) on project foo: Failed to retrieve remote metadata foo:1.0.4-SNAPSHOT/maven-
metadata.xml: Could not transfer metadata foo:1.0.4-SNAPSHOT/maven-metadata.xml from/to foo-
snapshots (https://foo.nexus.com/repository/foo-snapshots/): Not authorized -> [Help 1]

Note that it is still trying to push it out to Nexus, instead of pushing it to Gitlab. How can I change my gitlab-ci.yml file, or my settings.xml, to have it push to Gitlab?

Here is my gitlab-ci.yml file:

stages:
  - build
deploy:
  stage: build
  script:
    - mvn clean deploy -e -B -U -Ddist=true -s settings.xml

Note that before when it was pushing it to Nexus, this was the first line of code in the script: mvn-settings -u "${NEXUS_DEPLOY_USER}" -p "${NEXUS_DEPLOY_PASS}" --master-password "${NEXUS_MASTER_PASS}" -s foo-snapshots. However I thought I can just override this by doing -s settings.xml.

In this settings.xml is my gitlab token:

     <server>
       <id>foo</id>
       <configuration>
         <httpHeaders>
            <property>
              <name>Job-Token</name>
              <value>${env.CI_JOB_TOKEN}</value>

And under that defines some distribution repositories:

<profiles>
    <profile>
      <id>gitlab-foo</id>
      <repositories>
        <repository>
          <id>foo</id>
          <url>https://gitlab.foo/api/v4/projects/700/packages/maven</url>
        </repository>
      </repositories>
      <properties>
        <distribution.repository.id>foo-snapshots</distribution.repository.id>
        <url>https://gitlab.foo/api/v4/projects/700/packages/maven</url>

Also here is a relevant part of my pom.xml file:

<distributionManagement>
        <repository>
            <id>foo-snapshots</id>
    <url>https://gitlab.foo/api/v4/projects/706/packages/maven</url>
        </repository>
        <snapshotRepository>
            <id>foo-snapshots</id>
    <url>https://gitlab.foo/api/v4/projects/706/packages/maven</url>
        </snapshotRepository>
</distributionManagement>

标签: javamavengitlab-cinexus

解决方案


Make sure you've specified the deployment repository. Couple different ways you could do so:

  1. Make sure there's a distributionManagement section in your pom that points at gitlab (docs here)
  2. Override the deploy plugin's destination via maven command line specified parameter: -DaltDeploymentRepository=foo-snapshots::https://gitlab.foo/api/v4/projects/700/packages/maven (docs here)

推荐阅读