Skip to content

Instantly share code, notes, and snippets.

@jmruc
Created June 24, 2013 19:19
Show Gist options
  • Save jmruc/5852692 to your computer and use it in GitHub Desktop.
Save jmruc/5852692 to your computer and use it in GitHub Desktop.
Generating pom.xml from gradle

To generate a pom.xml file just run gradle writeNewPom

If you want to generate it as pom.xml in the root of the project, replace writeTo("$buildDir/newpom.xml") with writeTo("pom.xml")

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")
}
@amirhadad
Copy link

How can I generate the 'build.gradle.kts' version of this guys? I am struggling with converting the groovy gradle function to Kotlin script.

@devlifealways
Copy link

Perhaps it would be better if instead of hard coding group & version, use Groovy DSL :

               groupId project.group.toString()
               version version

@tillkuhn
Copy link

@amirhadad I also ended up here trying to figure out how to do the same thing in Kotlin, and finally a colleague came up with the following code, which works fine for our project:

task("createPom") {
    group = "maven"
    description = "generates a maven pom.xml file based on the build.gradle.kts file"
    doLast {
        project.the<MavenPluginConvention>().pom() {
            project {
                groupId = "my.group"
                artifactId = "my-app"
                version = "latest"
                withGroovyBuilder {
                    "inceptionYear"("2042")
                }
            }
        }.writeTo("pom.xml")
    }
}

@devlifealways
Copy link

I've done quite the same

task generatePom {
    doLast {
        pom {
            project {
                groupId project.group.toString()
                artifactId 'awesome-app'
                version version

                inceptionYear '2018'
                licenses {
                    license {
                        name 'MIT License'
                        url 'MIT'
                        distribution 'repo'
                    }
                }
            }
        }.writeTo("$buildDir/pom.xml")
    }
}

@iZooto-sdk
Copy link

I have added the code in bulid.gradle file
task("createPom") {
group = "maven"
description = "generates a maven pom.xml file based on the build.gradle.kts file"
doLast {
project.the().pom() {
project {
groupId = "my.group"
artifactId = "my-app"
version = "latest"
withGroovyBuilder {
"inceptionYear"("2042")
}
}
}.writeTo("pom.xml")
}
}
but Its not working,Please guide me guys.

@sarnobat
Copy link

sarnobat commented Dec 6, 2022

Anyone have a gradle 6 equivalent?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment