How to declare a dependency from a private Gitlab Maven Project Registry Repository in Gradle?

2.9k views Asked by At

I have a private Project in which I configured a GitLab CI/CD Pipeline that builds the project and publishes it to the GitLab Package Registry of the same project. That works.

In an other project I want to add a dependency to that package. Although I am adding the maven repository declaration in build.gradle in that project, I get an error which says, that the dependency could not be resolved and I should declare the repository which provides the artifact.

I also tried to add a Authentication Token but somehow I didn't manage to get it to work.

Does someone see what I am doing wrong here?

Here is what I tried:

build.gradle

repositories {
  mavenCentral()
  maven {
    url 'https://gitlab.com/api/v4/groups/MY-GROUP-ID-THAT-I-ENTERED/-/packages/maven'
    name 'Gitlab'
    credentials(HttpHeaderCredentials) {
      name = 'Private-Token'
      value = 'myPrivateTokenThatIGenerated'
    }
    authentication {
      header(HttpHeaderAuthentication)
    }
  }
}

dependencies{
 implementation 'de.dependeny.organisation:project-name:0.1.0-SNAPSHOT'
}

I created the token in the source Project from Settings->Access Tokens, named it Private-Token, checked read-registry. I also tried it with read_api.

The build.gradle of my published project is like follows:


repositories {
    mavenCentral()
    maven{
      url "https://gitlab.com/api/v4/projects/MYPROJECTID/packages/maven"
      name "GitLab"
      credentials(HttpHeaderCredentials){
        name = 'Job-Token'
        value = System.getenv("CI_JOB_TOKEN")
      }
      authentication{
        header(HttpHeaderAuthentication)
      }
    }
}


publishing {
    publications {
        maven(MavenPublication) {
            artifactId = 'my-customprojectname'

            artifact sourceJar
            artifact jar
        }
    }
    repositories {
      maven{
        url "https://gitlab.com/api/v4/projects/MYPROJECTID/packages/maven"
        name "GitLab"
        credentials(HttpHeaderCredentials){
          name = "Job-Token"
          value = System.getenv("CI_JOB_TOKEN")
        }
        authentication{
          header(HttpHeaderAuthentication)
        }
      }
    }
}

1

There are 1 answers

0
Kerem On

There was an error in the link repository declaration in build.gradle. It should be projects instead of group and no /-/ after the package name. It should be like 'https://gitlab.com/api/v4/projects/PROJECT_ID/packages/maven' instead of 'https://gitlab.com/api/v4/groups/MY-GROUP-ID-THAT-I-ENTERED/-/packages/maven'