Gradle build.gradle to Maven pom.xml

Since Gradle 7, when using Gradle’s Maven-Publish plugin, publishToMavenLocal and publish are automatically added to your tasks, and calling either will always generate a POM file.

So if your build.gradle file looks like this:

plugins {
    id 'java'
    id 'maven-publish'
}

repositories {
    mavenCentral()
}

dependencies {
    implementation group: 'org.slf4j', name: 'slf4j-api', version: '1.7.25'
    runtimeOnly group: 'ch.qos.logback', name:'logback-classic', version:'1.2.3'
    testImplementation group: 'junit', name: 'junit', version: '4.12'
}

// the GAV of the generated POM can be set here
publishing {
    publications {
        maven(MavenPublication) {
            groupId = 'edu.bbte.gradleex.mavenplugin'
            artifactId = 'gradleex-mavenplugin'
            version = '1.0.0-SNAPSHOT'

            from components.java
        }
    }
}

you can call gradle publishToLocalRepo in its folder, you will find in the build/publications/maven subfolder, a file called pom-default.xml. Also, the built JAR together with the POM will be in your Maven local repo. More exactly the gradle generatePomFileForMavenPublication task does the actual generation, if you want to omit publication to your Maven local repo.

Please note that not all dependencies show up here, since the Gradle “configurations” don’t always map one-to-one with Maven “scopes”.

Leave a Comment