使用 Gradle Kotlin 配置 Maven 插件

Configuring maven plugin with Gradle Kotlin

正在尝试将项目转换为 GSK

我们在 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"
                }
            }
        }
    }
}

在 GSK,我走到了这一步:

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

我实际上如何 construct/configure 一个存储库对象分配给 MavenDeployer 的 repository/snapshotRepository 属性? Groovy 摘录中的 mavenLocal() 为部署程序做了什么,我如何在 Kotlin 中调用它(因为它是 RepositoryHandler 而不是 MavenDeployer 上的方法)? 问题,问题

使用 Gradle 4.4

mavenDeployer 部分通过使用 Groovy 动态 invokeMethod 调用来工作,因此它不能很好地转换为 kotlin-dsl.

有个例子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

你的 kotlin-dsl 的最新版本可能看起来像这样(此示例使用 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")
                    }
                }
            }

在您的 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)
                    }
                }
            }
        }
    }
}

在你 gradle.properties:

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