使用gradle脚本kotlin

时间:2016-09-14 18:10:22

标签: 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... */
            }
        }
    }
}

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

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

......这就是它!

AFAIU,在Groovy中,Upload任务由MavenPlugin“增强”。 它在Kotlin中如何运作?

1 个答案:

答案 0 :(得分:2)

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

  

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

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