如何使用 kotlinscript DSL (build.gradle.kts)通过 URL 添加 maven 存储库

下面的代码片段在 build.gradle.kts版本中与 build.gradle的代码片段等价吗?

repositories {
mavenCentral()
maven {
url '<MAVEN REPO URL>'
}
}
34555 次浏览

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

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

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.

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

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

repositories {
maven {
url = uri("<your-custom-url>")
}
}