gradleupgrademaven-pluginmaven-publish

Could not find method pom() of type org.gradle.api.DefaultTask after gradle upgrade to 7


I'm upgrading gradle from 2.6 to 7.0 and after the upgrade I'm getting this error

Could not find method pom() for arguments ... on task of type org.grale.api.DefaultTask.

after I removed the apply plugin: 'maven' which was deprecated and replaced in with 'maven-publish'

I tried to find the replacement for pom method in mavel-publish, but I did not succeed

task createPomFiles {
    pom {
        project {
            groupId groupId
            artifactId 'generic-connector'
            version version
        }
    }.writeTo("$project.buildDir/resources/../pom.xml")
}

Relates to: How to make gradle generate a valid pom.xml file at the root of a project for maven users?


Solution

  • To generate a pom.xml file for your Gradle 7.0+ project, I suggest you the following steps:

    1. Add maven-publish to Plugins Declaration

    I reckon you already did that, but for reference:

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

    2. Define a Publication

    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
    
                groupId = 'com.github.tcsizmadia'
                artifactId = 'gradle-sandbox'
                version = '1.0-SNAPSHOT'
    
                pom {
                    name = 'Gradle Sandbox'
                    description = 'Sandbox Project for Gradle Builds'
                }
            }
        }
    }
    

    Make sure to customise the definition for your needs (groupId, artifactId, etc.). For reference, see Maven Publish Plugin's documentation

    3. Create a Task to Rename pom.xml (Optional)

    As a nice to have feature, you can add a task to rename and copy the generated pom.xml file.

    tasks.register('renamePom') {
        doLast {
            def pomFile = file("${layout.buildDirectory.get().asFile}/publications/mavenJava/pom-default.xml")
            if (pomFile.exists()) {
                def newPomFile = file("${layout.buildDirectory.get().asFile}/resources/main/META-INF/maven/pom.xml")
                newPomFile.getParentFile().mkdirs()
                pomFile.renameTo(newPomFile)
            }
        }
    }
    

    Here, I specified a task to copy the default build//publications/mavenJava/pom-default.xml to build/pom.xml. Feel free to modify as you need.

    4. Finalise the Build with pom.xml Generation

    It's not mandatory, but if you wish to have your pom.xml generated with your builds, append this to your build.gradle file:

    generatePomFileForMavenJavaPublication.finalizedBy(renamePom)
    build.finalizedBy generatePomFileForMavenJavaPublication
    

    If you skip this step, you can generate your pom.xml for your project issuing:

    ./gradlew generatePomFileForMavenJavaPublication
    

    A Minimalistic Full Example

    plugins {
        id 'java'
        id 'maven-publish'
    }
    
    group = 'com.github.tcsizmadia'
    version = '1.0-SNAPSHOT'
    
    repositories {
        mavenCentral()
    }
    
    dependencies {
        testImplementation platform('org.junit:junit-bom:5.9.1')
        testImplementation 'org.junit.jupiter:junit-jupiter'
    }
    
    test {
        useJUnitPlatform()
    }
    
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
    
                groupId = 'com.github.tcsizmadia'
                artifactId = 'gradle-sandbox'
                version = '1.0-SNAPSHOT'
    
                pom {
                    name = 'Gradle Sandbox'
                    description = 'Sandbox Project for Gradle Builds'
                }
            }
        }
    }
    
    tasks.register('renamePom') {
        doLast {
            def pomFile = file("${layout.buildDirectory.get().asFile}/publications/mavenJava/pom-default.xml")
            if (pomFile.exists()) {
                def newPomFile = file("${layout.buildDirectory.get().asFile}/resources/main/META-INF/maven/pom.xml")
                newPomFile.getParentFile().mkdirs()
                pomFile.renameTo(newPomFile)
            }
        }
    }
    
    generatePomFileForMavenJavaPublication.finalizedBy(renamePom)
    build.finalizedBy generatePomFileForMavenJavaPublication