Search code examples
gradlekotlingradle-kotlin-dsl

Configuring maven plugin with Gradle Kotlin


Trying to convert a project to GSK

We have this in Groovy:

subprojects {
    plugins.withType(MavenPlugin) {
        tasks.withType(Upload) {
            repositories {
                mavenDeployer {
                    mavenLocal()
                    repository(url: "xxx") {
                        authentication(userName: "yyy", password: "zzz")
                    }
                    snapshotRepository(url: "xxx") {
                        authentication(userName: "yyy", password: "zzz")
                    }
                    pom.artifactId = "${project.name}"
                    pom.version = "$version"
                }
            }
        }
    }
}

In GSK, I got this far:

plugins.withType(MavenPlugin::class.java) {
    tasks.withType(Upload::class.java) {
        val maven = the<MavenRepositoryHandlerConvention>()
        maven.mavenDeployer {
            // ???
        }
    }
}

How do I actually construct/configure a repository object to assign to repository/snapshotRepository properties of the MavenDeployer? What does mavenLocal() in the Groovy excerpt do for the deployer and how do I call it in Kotlin (since it's a method on a RepositoryHandler and not MavenDeployer)? Questions, questions

Using Gradle 4.4


Solution

  • The mavenDeployer piece works through use of Groovy dynamic invokeMethod calls so it doesn't translate well into the kotlin-dsl.

    There is an example here that shows how to use the withGroovyBuilder method block to configure these special Groovy types. You can see some details about the withGroovyBuilder feature in the 0.11.1 release notes

    Yours might look something like this with the most recent releases of kotlin-dsl (this example with 0.14.x)

            withConvention(MavenRepositoryHandlerConvention::class) {
    
                mavenDeployer {
    
                    withGroovyBuilder {
                        "repository"("url" to "xxx") {
                            "authentication"("userName" to "yyy", "password" to "zzz")
                        }
                        "snapshotRepository"("url" to "xxx") {
                            "authentication"("userName" to "yyy", "password" to "zzz")
                        }
                    }
    
                    pom.project {
                        withGroovyBuilder {
                            "artifactId"("${project.name}")
                            "version"("$version")
                        }
                    }
                }