Unable to publish jar to Gitlab package registry with gradle

12.3k views Asked by At

I am trying to publish some jar artefacts on gitlab package registry but I get this error from the server :

Received status code 415 from server: Unsupported Media Type

Here is the publishing section of my build.gradle.kts :

publishing {
    publications {
        create<MavenPublication>("maven"){
            artifact(tasks["bootJar"])
        }
    }
    repositories {
        maven {
            url =  uri("https://gitlab.com/api/v4/groups/my-group/-/packages/maven")
            name = "Gitlab"
            credentials(HttpHeaderCredentials::class) {
                name = "Token"
                value = System.getenv("CI_JOB_TOKEN")
            }
            authentication {
                create<HttpHeaderAuthentication>("header")
            }
        }
    }
}

In my gitlab-ci, I added a task for publish the artefacts :

deploy:
  stage: deploy
  script: gradle publish
  only:
    - master

Any help would be appreciated

3

There are 3 answers

3
Jan On

Quick answer

Replace your publishing url pointing to the group-scope with the one pointing to the specific-package-repository, e.g. on gitlab.com:

https://gitlab.com/api/v4/projects/<your-project-id>/packages/maven

You need to replace <your-project-id> with your specific project-id of course.

Related to this a quote from docs.gitlab:

Note: In all cases, you need a project specific URL for uploading a package in the distributionManagement section.

Or in other words: Only the general repositories section can use your groups-url for searching other already published artifacts! (I also had to understand that). So:

  • you cannot publish to the group-package-store on gitlab, you can just search there.
  • Publication goes always to the project-specific package-store, which will then be visible at group-scope too.

Example gradle config (kotlin-dsl)

repositories {
    mavenCenter()
    jcenter()

    // Here you USE the group api/v4 url for SEARCHING packages
    maven {
        name = "GitLab"

        url = uri("https://gitlab.com/api/v4/groups/my-group/-/packages/maven")
        credentials(HttpHeaderCredentials::class) {
            name = "Job-Token"
            value = System.getenv("CI_JOB_TOKEN")
        }
        authentication {
            create<HttpHeaderAuthentication>("header")
        }
    }

}

publishing {
    publications {
        create<MavenPublication>("maven"){
            artifact(tasks["bootJar"])
        }
    }
    repositories {
        maven {
            
            // here your PROVIDE the PROJECT-URI for publishing your package
            // in the project-specific package-space which is also visible at
            // the group scope above
            url =  uri("https://gitlab.com/api/v4/projects/<your-project-id>/packages/maven")
            name = "Gitlab"
            credentials(HttpHeaderCredentials::class) {
                name = "Job-Token"
                value = System.getenv("CI_JOB_TOKEN")
            }
            authentication {
                create<HttpHeaderAuthentication>("header")
            }
        }
    }
}

More Info

There are multiple scenarios on how you may interact with the maven-repository-space on GitLab. The three switches are:

  1. The place where you want to look for existing published packages
    • project-scope (https://.../api/v4/projects/<project-id>/packages/maven)
    • group-scope (https://.../api/v4/groups/<group-id>/-/packages/maven)
    • instance-scope (https://.../api/v4/packages/maven)
  2. The authorization-method you want to use
  3. The place where your want to publish your package
    • this must always be a specific project-url (https://.../api/v4/projects/<project-id>/packages/maven)
0
Myoch On

I think the most important thing is to make sure you've enabled archives in your project:

  1. Go to Project Settings

Go to Project Settings

  1. Expand Permissions

Look for permissions

  1. Switch on "Packages"

Switch on Packages

0
justfortherec On

Apparently, there are also other reasons for the response status 415 Unsupported Media Type:

I ran into the same error message while trying to publish to the project repository. The error that I made was to use the URL-encoded path of the project instead of the project ID in the repository URL.

From the Gitlab documentation (emphasis added):

For retrieving artifacts, use either the URL-encoded path of the project (like group%2Fproject) or the project's ID (like 42). However, only the project's ID can be used for publishing.

This is the build.gradle.kts configuration that worked for me on my self-hosted Gitlab instance for publishing a Spring Boot fat JAR:

plugins {
    /* ... other stuff ... */

    `java-library`
    `maven-publish`
}

publishing {
    publications {
        create<MavenPublication>("bootJava") {
            artifact(tasks.getByName("bootJar"))
        }
    }
    repositories {
        maven {
            val projectId = System.getenv("CI_PROJECT_ID")
            name = "Project Name"
            url = uri("https://gitlab.example.com/api/v4/projects/${projectId}/packages/maven")
            credentials(HttpHeaderCredentials::class) {
                name = "Job-Token"
                value = System.getenv("CI_JOB_TOKEN")
            }
            authentication {
                create<HttpHeaderAuthentication>("header")
            }
        }
    }
}