'Maven api pulish with Gradle 7.2

I want to publish an Android in jitpack.io but api dependencies are missing, that's why I use with Gradle 7.2 this

project.afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                pom {
                    configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }
                    configurations.implementation.getDependencies().each { dep -> addDependency(dep, "runtime") }
                }
            }
        }
    }
}

This works with Gradle 6.x but with Gradle 7.x it fails

configurations.api.getDependencies().each { dep -> addDependency(dep, "compile") }

with

Could not find method addDependency() for arguments [DefaultProjectDependency{dependencyProject='project ':libs:gvr'', configuration='default'}] on object of type org.gradle.api.publish.maven.internal.publication.DefaultMavenPom.

Does someone knows how to solve it ?



Solution 1:[1]

Use this snippet

       release(MavenPublication) {
            // Applies the component for the release build variant.
            from components.release
            artifact(sourcesJar)

            // You can then customize attributes of the publication as shown below.
            groupId = '[group]'
            artifactId = '[artifact]'
            version = '[version]'

            pom.withXml {
                def dependenciesNode = (asNode().get("dependencies") as groovy.util.NodeList).get(0) as groovy.util.Node
                def configurationNames = ["implementation", "api"]

                configurationNames.forEach { configurationName ->
                    configurations[configurationName].allDependencies.forEach {
                        if (it.group != null && it.version != "unspecified") {
                            def dependencyNode = dependenciesNode.appendNode("dependency")
                            dependencyNode.appendNode("groupId", it.group)
                            dependencyNode.appendNode("artifactId", it.name)
                            dependencyNode.appendNode("version", it.version)
                        }
                    }
                }
            }
        }
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 EpicPandaForce