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
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:
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")
            }
        }
    }
}
There are multiple scenarios on how you may interact with the maven-repository-space on GitLab. The three switches are:
https://.../api/v4/projects/<project-id>/packages/maven)https://.../api/v4/groups/<group-id>/-/packages/maven)https://.../api/v4/packages/maven)https://.../api/v4/projects/<project-id>/packages/maven)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 (like42). 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")
            }
        }
    }
}
I think the most important thing is to make sure you've enabled archives in your project:



If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With