bruto
bruto

Reputation: 537

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

Upvotes: 5

Views: 2393

Answers (2)

Dmitry Kaltovich
Dmitry Kaltovich

Reputation: 2270

Try this task in your build.gradle.kts:

getByName<Upload>("uploadArchives") {
    val repositoryUrl: String by project
    val repositoryUser: String by project
    val repositoryPassword: String by project
    repositories {
        withConvention(MavenRepositoryHandlerConvention::class) {
            mavenDeployer {
                withGroovyBuilder {
                    "repository"("url" to uri(repositoryUrl)) {
                        "authentication"("userName" to repositoryUser, "password" to repositoryPassword)
                    }
                }
            }
        }
    }
}

and in yuor gradle.properties:

repositoryUrl=${repositoryUrl}
repositoryUser=${repositoryUser}
repositoryPassword=${repositoryPassword}

Upvotes: 0

mkobit
mkobit

Reputation: 47249

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")
                    }
                }
            }

Upvotes: 3

Related Questions