2016-09-14 55 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") 
       } 
       } 
      } 
      } 
     } 
     } 
    } 
    } 
}