Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Configure uploadArchives task using gradle script kotlin

Tags:

I'd like to switch my libraries to Gradle Script Kotlin but I can't find a way to configure the uploadArchive task.

Here's the groovy kotlin script I'd like to translate:

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

So far, I've understood that it should start with

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

... And that's pretty much it !

AFAIU, in Groovy, the Upload task is "augmented" by the MavenPlugin. How does it work in Kotlin ?

like image 834
Salomon BRYS Avatar asked Sep 14 '16 18:09

Salomon BRYS


1 Answers

0.11.x (in Gradle 4.2) added better support for tasks that have convention plugins and better support for heavy Groovy DSLs. The full release notes are on GitHub. Here is a relevant snippet from those notes:

Better support for Groovy-heavy DSLs (#142, #47, #259). 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")
                }
              }
            }
          }
        }
      }
    }
  }
}
like image 104
mkobit Avatar answered Oct 04 '22 22:10

mkobit