Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Gradle to upload archives to GitLab Maven repository

I use Gradle and Gradle Maven plugin to upload archives to Maven Central. This works fine. I want to upload the same archives also to the GitLab Maven repository. Upload to this repo requires authentication with special HTTP header values. But I don't know how to do that with the Gradle Maven plugin. The GitLab documentation describes a Maven based build process using pom.xml and settings.xml. What I currently have is this:

Files created with Gradle:

build/libs
├── wms-1.2.0.jar
├── wms-1.2.0.jar.asc
├── wms-1.2.0-javadoc.jar
├── wms-1.2.0-javadoc.jar.asc
├── wms-1.2.0-sources.jar
└── wms-1.2.0-sources.jar.asc
build/poms
└── pom-default.xml

Files according to the GitLab documentation:

pom.xml
settings.xml

When I execute

mvn deploy -s settings.xml

then pom-default.xml must be "mixed into" pom.xml and build/libs/* must be used as archive location. How can I do this?

like image 718
G. Fiedler Avatar asked Dec 22 '18 19:12

G. Fiedler


1 Answers

Please note that the maven-publish plugin “is now the preferred option for publishing [Maven] artifacts” with Gradle (see also the note at the top of the page you linked). Doing what you need with the maven plugin would be more cumbersome as it’s both more intricate and not as well documented, IMHO. Hence, I hope you won’t mind when I answer your question with the maven-publish plugin in mind.

As for the authentication with special HTTP header values, you should be able to solve this as follows (via):

publishing {
    repositories {
        maven {
            url "http://repo.mycompany.com/maven2"
            credentials(HttpHeaderCredentials) {
                name = "Private-Token"
                value = "REPLACE_WITH_YOUR_PERSONAL_ACCESS_TOKEN"
            }
            authentication {
                header(HttpHeaderAuthentication)
            }
        }
    }
}

I you really want to use the old maven plugin then I’d suggest to start researching from this section of the documentation on how to solve the authentication issue.


BTW: I have mainly answered your question on how to authenticate with GitLab here. If you should have trouble with configuring the publication, then I’d recommend to post this as a separate question with more details on your setup and on maybe what you’ve tried already.


Edited to add: if you need to use different header credentials depending on certain command line options, you could change the credentials configuration above to something like the following:

credentials(HttpHeaderCredentials) {
    if (project.hasProperty('jobToken')) {
      name = "Job-Token"
      value = project.property('jobToken')
    } else {
      name = "Private-Token"
      value = "REPLACE_WITH_YOUR_PERSONAL_ACCESS_TOKEN"
    }
}

In this example, you could run ./gradlew -PjobToken=REPLACE_WITH_YOUR_JOB_TOKEN … in GitLab CI jobs while leaving out the -PjobToken=… part when working locally.

like image 194
Chriki Avatar answered Sep 19 '22 23:09

Chriki