gradle-kotlin-dsl – 使用gradle脚本kotlin配置uploadArchives任务

我想将我的库切换到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... */
            }
        }
    }
}

到目前为止,我已经明白它应该从一开始

task<Upload>("uploadArchives") {
    /* ??? */
}

……这就是它!

AFAIU,在Groovy中,上传任务由MavenPlugin“扩充”.
它在Kotlin中如何运作?

最佳答案 0.11.x(在Gradle 4.2中)为具有约定插件的任务添加了更好的支持,并为更重的Groovy DSL提供了更好的支持.完整发行说明在
GitHub.以下是这些说明中的相关摘要:

Better support for Groovy-heavy DSLs (07001, 07002, 07003). With the introduction of the withGroovyBuilder and withConvention utility extensions. withGroovyBuilder provides a dynamic dispatching DSL with Groovy semantics for better integration with plugins that rely on Groovy builders such as the core maven plugin.

Here is an example taken directly from the source code

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")
                }
              }
            }
          }
        }
      }
    }
  }
}
点赞