Configure Maven repository by code

Hi there!

as of version 4.10 Gradle supports token authentication via HTTP headers which has to be configured in the following manner:

maven {
url = “https://gitlab.com/api/…”
credentials(HttpHeaderCredentials) {
name = credentials_name
value = credentials_value
}
authentication {
header(HttpHeaderAuthentication)
}
}

So far, so good. My goal, however, is to configure such a maven repository by using a function

repository = {String name -> ...}

I managed to configure url and credentials but I am not able to configure the Authenatication correctly. What I get is an Action of an AuthenaticationContainer. I tried to create a HttpHeaderAuthentication and add it to the container without any success.

Does anybody know how to replace the (domain-specific) header(HttpHeaderAuthentication) using an Action of an AuthenticationContainer instead?

Thanks in advance.

For reference see my code below:

getRepository = {String id →
return new Action() {
@Override
void execute(MavenArtifactRepository repository){
repository.setUrl(“https://gitlab.com/api/v4/projects/“+id+”/packages/maven”)
repository.credentials(HttpHeaderCredentials, newAction(){
@Override
void execute(HttpHeaderCredentialscredentials){
credentials.name=“Private Token”
credentials.value = value
}
})
repository.authentication(newAction() {
@Override
voidexecute(AuthenticationContainer authentications) {
// does not work
authentications.register(“header”,HttpHeaderAuthentication)
printlnauthentications
}
})
}
}
}

I got the following working (all I did was paste your code and then tweak it until it compiled in my build.gradle, I also changed my getRepository into a function). I’m not sure how you weren’t getting all the same compile errors as me???

Action<MavenArtifactRepository> getRepository(String id)
{
    return new Action<MavenArtifactRepository>() {
        @Override
        void execute(MavenArtifactRepository repository){
            repository.setUrl("https://gitlab.com/api/v4/projects/"+id+"/packages/maven")
            repository.credentials(HttpHeaderCredentials, new Action<HttpHeaderCredentials>(){
                @Override
                public void execute(HttpHeaderCredentials credentials){
                    credentials.name="Private Token"
                    credentials.value = "value"
                }
            })
            repository.authentication(new Action<AuthenticationContainer>() {
                @Override
                public void execute(AuthenticationContainer authentications) {
                    authentications.register("header",HttpHeaderAuthentication)
                    println authentications
                }
            })
        }
    }
}

repositories {
    maven( getRepository("123") )
}

Output:

$ ~/.gradle/wrapper/dists/gradle-4.10-bin/bg6py687nqv2mbe6e1hdtk57h/gradle-4.10/bin/gradle tasks

> Configure project :
['header'(HttpHeaderAuthentication)]

> Task :tasks
...

Hi Chris!

Thanks for your reply. It does work right now :blush: