Search code examples
mavengradlekotlinpom.xmlgradle-kotlin-dsl

How can I convert a groovy task in gradle into Gradle Kotlin DSL to generate a pom.xml?


What is the build.gradle.kts version of the the following Gradle script?

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

References

1- Gradle sample was here.


Solution

  • I believe this is the same as a build.gradle.kts file:

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

    You have to use the withGroovyBuilder method to add the untyped properties to the model