使用gradle脚本kotlin配置uploadArchives任务

我想将我的库切换到Gradle脚本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中如何工作?

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

更好地支持Groovy重的DSL ( #142 , #47 , #259 )。 随着withGroovyBuilder和withConvention实用程序扩展的引入。 withGroovyBuilder提供了一个动态调度的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") } } } } } } } } }