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?
To generate a pom.xml
file for your Gradle 7.0+ project, I suggest you the following steps:
I reckon you already did that, but for reference:
plugins {
id 'java'
id 'maven-publish'
}
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
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.
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
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