使用 gradle 脚本 kotlin 配置 uploadArchives 任务

Sal*_*RYS 5 gradle-kotlin-dsl

我想将我的库切换到 Gradle Script Kotlin,但我找不到配置 uploadArchive 任​​务的方法。

这是我想要翻译的 groovy kotlin 脚本:

uploadArchives {
    repositories {
        mavenDeployer {
            repository(url: "https://oss.sonatype.org/service/local/staging/deploy/maven2/") {
                    authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            snapshotRepository(url: "https://oss.sonatype.org/content/repositories/snapshots/") {
                authentication(userName: ossrhUsername, password: ossrhPassword)
            }

            pom.project {
                /* A lot of stuff... */
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

到目前为止,我明白它应该从

task<Upload>("uploadArchives") {
    /* ??? */
}
Run Code Online (Sandbox Code Playgroud)

... 差不多就这样了!

AFAIU,在 Groovy 中,该Upload任务由MavenPlugin. 它在 Kotlin 中如何工作?

mko*_*bit 4

0.11.x(在 Gradle 4.2 中)增加了对具有约定插件的任务的更好支持以及对重型 Groovy DSL 的更好支持。完整的发行说明位于GitHub上。以下是这些注释中的相关片段:

更好地支持 Groovy-heavy DSL#142#47#259)。随着 withGroovyBuilder 和实用程序扩展的引入withConventionwithGroovyBuilder提供具有 Groovy 语义的动态调度 DSL,以便更好地与依赖 Groovy 构建器的插件(例如核心maven插件)集成。

这是直接取自源代码的示例

plugins {
  java
  maven
}

group = "org.gradle.kotlin-dsl"

version = "1.0"

tasks {

  "uploadArchives"(Upload::class) {

    repositories {

      withConvention(MavenRepositoryHandlerConvention::class) {

        mavenDeployer {

          withGroovyBuilder {
            "repository"("url" to uri("$buildDir/m2/releases"))
            "snapshotRepository"("url" to uri("$buildDir/m2/snapshots"))
          }

          pom.project {
            withGroovyBuilder {
              "parent" {
                "groupId"("org.gradle")
                "artifactId"("kotlin-dsl")
                "version"("1.0")
              }
              "licenses" {
                "license" {
                  "name"("The Apache Software License, Version 2.0")
                  "url"("http://www.apache.org/licenses/LICENSE-2.0.txt")
                  "distribution"("repo")
                }
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)