使用Gradle Kotlin配置maven插件

时间:2018-01-18 20:39:50

标签: gradle kotlin gradle-kotlin-dsl

尝试将项目转换为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 {
            // ???
        }
    }
}

如何实际构建/配置存储库对象以分配给MavenDeployer的repository / snapshotRepository属性? Groovy摘录中的mavenLocal()为部署者做了什么,如何在Kotlin中调用它(因为它是RepositoryHandler而不是MavenDeployer的方法)? 问题,问题

使用Gradle 4.4

2 个答案:

答案 0 :(得分:1)

mavenDeployer部分通过使用Groovy动态invokeMethod调用,因此无法很好地转换为kotlin-dsl

有一个示例here,它显示了如何使用withGroovyBuilder方法块来配置这些特殊的Groovy类型。您可以在0.11.1 release notes

中查看有关withGroovyBuilder功能的一些详细信息

最新版本的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")
                    }
                }
            }

答案 1 :(得分:0)

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