2016-09-14 395 views
4

我想将我的库切换到Gradle Script Kotlin,但我找不到配置uploadArchive任务的方法。使用gradle脚本配置uploadArchives任务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.11.x(在Gradle 4.2中)增加了对具有约定插件和更好地支持Groovy DSL的任务的更好支持。完整的发行说明在GitHub。下面是从这些笔记相关片段:

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