105

Whats the equivalent of the following code snippet from a build.gradle in a build.gradle.kts version?

repositories {
  mavenCentral()
  maven {
    url '<MAVEN REPO URL>'
  }
}
Florian Reisinger
  • 2,638
  • 4
  • 23
  • 34

4 Answers4

171

As an addition to the other answers, in #kotlin-dsl/256 shortcut methods were added to the various repository methods to do something like the following:

repositories {
  mavenCentral()
  maven(url = "<MAVEN REPO URL>")
}

According to the issue, this was added in the Kotlin DSL version 0.11.1. The 0.11.x versions were included in the Gradle 4.2 release.

To see the Gradle version you are running with your build when using the Gradle wrapper run ./gradlew --version.

mkobit
  • 43,979
  • 12
  • 156
  • 150
  • 1
    Which version are we talking about? I am using the Kotlin plugin "1.2.10-release-IJ2017.3-1" and Kotlin version "1.2.10" and your syntax is producing the following error "None of the following functions can be called with the argument supplied" - so how to check for your version? I do not know where to check or get this version (of the Kotlin DSL for Gradle). I also do not have any plugin updates pending. – Florian Reisinger Jan 14 '18 at 08:51
  • @FlorianReisinger I've updated my answer a little bit. The Kotlin version or plugin you use in your build (say with `id "org.jetbrains.kotlin.jvm" version "1.2.10")`) is different than the Kotlin version Gradle uses to execute the build. – mkobit Jan 14 '18 at 15:34
  • Just checked, I had the 4.0 Gracie wrapper :) – Florian Reisinger Jan 14 '18 at 19:19
46

The official doco allows you to switch the examples between the Groovy and Kotlin DSLs. Currently the answer listed there to your question is:

repositories {
    mavenCentral()
    maven {
        url = uri("<MAVEN REPO URL>")
    }
}

I needed to add Gitlab with authentication, which has a more complicated syntax. For others that stumble upon this, here is the official Gitlab example translated to the kts/Kotlin syntax.

val gitLabPrivateToken: String by project

maven {
    url = uri("https://<gitlab-url>/api/v4/groups/<group>/-/packages/maven")
    name = "GitLab"
    credentials(HttpHeaderCredentials::class) {
        name = "Private-Token"
        value = gitLabPrivateToken
    }
    authentication {
        create<HttpHeaderAuthentication>("header")
    }
}

The example URL here is true to Gitlab doco. But for me, it only worked with a URL like this: https://gitlab.com/api/v4/projects/12345/packages/maven

Fletch
  • 4,829
  • 2
  • 41
  • 55
  • I was looking precisely for the translation of the Gitlab example and could not figure out how to do it for the `HttpHeaderAuthentication`. Thank you very much! – monsterkrampe Nov 17 '20 at 14:25
  • Thanks! This got me out of a pinch trying to get packages from GitLab. The URL issue is caused by some GitLab strangeness (and bad documentation). You can only publish to project registry URLs, but you can read from group URLs. Therefore, you always need to use the project registry (as you specified) when publishing, but in your parent projects, it's easier to specify just the group URL once. – CatalinM Dec 08 '20 at 14:09
  • exactly what I was looking for – Rainmaker Apr 29 '21 at 18:29
  • How to write the credentials block is what I needed. – Chris Feb 22 '23 at 12:45
30

At 2018-01-13 the correct syntax is the following (instead of url, the function setUrl):

repositories {
    mavenCentral()
    maven {
        setUrl("<MAVEN REPO URL>")
    }
}
Nat
  • 3,587
  • 20
  • 22
Florian Reisinger
  • 2,638
  • 4
  • 23
  • 34
9

You can add a custom Maven URL in the following way as per official docs:

repositories {
    maven {
        url = uri("<your-custom-url>")
    }
}
Saikat
  • 14,222
  • 20
  • 104
  • 125
  • Cannot access class 'java.net.URI'. Check your module classpath for missing or conflicting dependencies – StuartDTO Oct 10 '21 at 18:15