blog podcast

Kotlin to Groovy Gradle DSL

I wanted to follow this example on how to use a github maven repository for your dependencies with Gradle. Unfortunately Github only supports authorized access to packages, which means you need to configure credentials in gradle. The following section contains the configuration:

// build.gradle.kts
repositories {
   maven("https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCH/releases") {
      credentials(HttpHeaderCredentials::class.java) {
         name = "Authorization"
         value = "Bearer 4774b9b4af1bef7dd8924793ba2962a679e0df47"
      }
      authentication {
         register("header", HttpHeaderAuthentication::class)
      }
   }
}

Now, I had a small problem. I’m not using the Kotlin DSL. So how do you write it in Groovy? It turns out that the answer is undramatically simple. Simply ditch the ::class stuff. It becomes something like this:

    maven {
        url("https://raw.githubusercontent.com/USERNAME/REPOSITORY/BRANCH/releases")
        credentials(HttpHeaderCredentials) {
            name = "Authorization"
            value = "Bearer ${project.findProperty("gpr.token") as String}"
        }
        authentication {
            register("header", HttpHeaderAuthentication)
        }
    }

The whole gpr.token thing allows you to configure the properties in a gradle.properties file in your gradle home project.