如何将gradle中的groovy任务转换为Gradle Kotlin DSL以生成pom.xml?

时间:2018-01-30 04:59:02

标签: maven gradle kotlin pom.xml gradle-kotlin-dsl

以下Gradle脚本的build.gradle.kts版本是什么?

apply plugin: 'maven'
apply plugin: 'java'

sourceCompatibility = 7
targetCompatibility = 7

dependencies {
    compile            'com.google.guava:guava:13.0.1'
    compile            'joda-time:joda-time:2.1'

    testCompile        'junit:junit:4.11'
    testCompile        'org.mockito:mockito-core:1.9.5'
}

task writeNewPom << {
    pom {
        project {
            groupId 'org.example'
            artifactId 'test'
            version '1.0.0'

            inceptionYear '2008'
            licenses {
                license {
                    name 'The Apache Software License, Version 2.0'
                    url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
                    distribution 'repo'
                }
            }
        }
    }.writeTo("$buildDir/newpom.xml")
}

参考

1- Gradle样本为here

1 个答案:

答案 0 :(得分:3)

我认为这与build.gradle.kts文件相同:

plugins {
    java
    maven
}

java {
    sourceCompatibility = JavaVersion.VERSION_1_7
    targetCompatibility = JavaVersion.VERSION_1_7
}

repositories {
    jcenter()
}

dependencies {
    compile("com.google.guava:guava:13.0.1")
    compile("joda-time:joda-time:2.1")

    testCompile("junit:junit:4.11")
    testCompile("org.mockito:mockito-core:1.9.5")
}

tasks {
    "writeNewPom" {
        doLast {
            project.the<MavenPluginConvention>().pom {
                project {
                    groupId = "org.example"
                    artifactId = "test"
                    version = "1.0.0"
                    withGroovyBuilder {
                        "inceptionYear"("2008")
                        "licenses" {
                            "license" {
                                "name"("The Apache Software License, Version 2.0")
                                "url"("http://www.apache.org/licenses/LICENSE-2.0.txt")
                                "distribution"("repo")
                            }
                        }
                    }
                }
            }.writeTo("$buildDir/newPom.xml")
        }
    }
}

您必须使用withGroovyBuilder方法将无类型属性添加到模型