Maven Could not resolve dependency using gitlab

Viewed 2734

I need to resolve a dependency using maven However it gives an error. I'm using GitLab to store the artifact under the package registry.

Below is the POM.XML file

<repositories>
  <repository>
    <id>gitlab-maven</id>
    <url>https://code.xyz.com/api/v4/projects/17/packages/maven</url>
  </repository>
</repositories>

<distributionManagement>
  <repository>
    <id>gitlab-maven</id>
    <url>https://code.xyz.com/api/v4/projects/17/packages/maven</url>
  </repository>

  <snapshotRepository>
    <id>gitlab-maven</id>
    <url>https://code.xyz.com/api/v4/projects/17/packages/maven</url>
  </snapshotRepository>
</distributionManagement>

When I run below the maven command I get below error:

mvn dependency:get -Dartifact=com.xyz:common:1.0.0.0

Below is the error:

[INFO] --- maven-dependency-plugin:3.1.2:get (default-cli) @ sweta-test ---
[INFO] Resolving com.xyz:common:jar:1.0.0.0 with transitive dependencies
Downloading from gitlab-maven: https://code.xyz.com/api/v4/projects/17/packages/maven/com/xyz/common/1.0.0.0/common-1.0.0.0.pom
Downloading from central: https://repo.maven.apache.org/maven2/com/xyz/common/1.0.0.0/common-1.0.0.0.pom
[WARNING] The POM for com.xyz:common:jar:1.0.0.0 is missing, no dependency information available
Downloading from gitlab-maven: https://code.xyz.com/api/v4/projects/17/packages/maven/com/xyz/common/1.0.0.0/common-1.0.0.0.jar
Downloading from central: https://repo.maven.apache.org/maven2/com/xyz/common/1.0.0.0/common-1.0.0.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  6.136 s
[INFO] Finished at: 2021-06-10T21:10:20+05:30
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:3.1.2:get (default-cli) on project sweta-test: Couldn't download artifact: org.eclipse.aether.resolution.DependencyResolutionException: Could not find artifact com.xyz:common:jar:1.0.0.0 in gitlab-maven (https://code.xyz.com/api/v4/projects/17/packages/maven) -> [Help 1]

Can someone help me with the missing part?

1 Answers

In this case, the dependency couldn't be found, because the maven client was not authenticated with the gitlab-repository. To solve this, one needs to follow these steps. For safety reasons, it is recommended to not push any tokens to a (publicly) hosted repository, instead the environment variables could be used. A quick tutorial can be found here.

From my experience Could not find artifact can also have a few other reasons. All of the points should more or less apply to gradle as well (or anything that is pulling artifacts/ containers, e.g. docker)

  1. The artifact really doesn't exist, it could be spelled wrong or the desired version simply doesn't exist in the repository.
  2. There are safety measures on the network side that prevent you from accessing the artifact. It could be a firewall or the DNS lookup fails (maybe a missing entry in the hosts file).
Related