Maven-publish 不能在 pom 文件上应用 withXML(没有方法签名)

Maven-publish cannot not apply withXML on pom file (No signature of method)

好的,所以我遇到了此处描述的确切问题:

我将提供的答案复制到我的 gradle 文件中,如下所示:

publishing {
    publications {
        mavenAar(MavenPublication) {
            groupId group
            artifactId 'exampleId'
            version version
            artifact source: file('build/outputs/aar/example-release.aar')

            //The publication doesn't know about our dependencies, so we have to manually add them to the pom
            pom.withXml {
                // for dependencies and exclusions
                def dependenciesNode = asNode().appendNode('dependencies')
                configurations.compile.allDependencies.each { ModuleDependency dp ->
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', dp.group)
                    dependencyNode.appendNode('artifactId', dp.name)
                    dependencyNode.appendNode('version', dp.version)

                    // for exclusions
                    if (dp.excludeRules.size() > 0) {
                        def exclusions = dependencyNode.appendNode('exclusions')
                        dp.excludeRules.each { ExcludeRule ex ->
                            def exclusion = exclusions.appendNode('exclusion')
                            exclusion.appendNode('groupId', ex.group)
                            exclusion.appendNode('artifactId', ex.module)
                        }
                    }
                }
            }
        }
    }
    repositories {
        mavenLocal()
        maven {
            url  selectDeploymentURL()
            if ( project.hasProperty( 'nexusUser' ) ) {
                credentials {
                    username project.getProperty('nexusUser')
                    password project.getProperty('password')
                }
            }
        }
    }
}

但是我在尝试 publishToMavenLocal 时遇到错误,我不确定为什么?

FAILURE: Build failed with an exception.

* Where:
Build file 'path/build.gradle' line: 136

* What went wrong:
Execution failed for task ':example:generatePomFileForMavenAarPublication'.
        > Could not apply withXml() to generated POM
> No signature of method: build_42xq5bsii69isvukzktk1oy51$_run_closure5_closure19_closure21_closure23_closure24.doCall() is applicable for argument types: (org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency_Decorated) values: [org.gradle.api.internal.artifacts.dependencies.DefaultSelfResolvingDependency_Decorated@433edba9]
Possible solutions: doCall(org.gradle.api.artifacts.ModuleDependency), findAll(), findAll(), isCase(java.lang.Object), isCase(java.lang.Object)

注意:第136行就是这一行:

configurations.compile.allDependencies.each { ModuleDependency dp ->

正如 Karma 所说,我在发布后不久就找到了答案

我刚刚删除了 ModuleDependency dp 并从中引用了所有内容,并且它克服了这个错误,我现在在 pom 文件中看到了新节点。

configurations.compile.allDependencies.each {
    def dependencyNode = dependenciesNode.appendNode('dependency')
    dependencyNode.appendNode('groupId', it.group)
    dependencyNode.appendNode('artifactId', it.name)
    dependencyNode.appendNode('version', it.version)

崩溃的根本原因是并非所有依赖项都是ModuleDependency对象。像下面这样的东西应该可以解决问题并让你仍然添加排除项(不在基础 class)。

configurations.compile.allDependencies.each { dp ->
    if (dp instanceof ModuleDependency) {
        // do stuff
    }
}

编译已弃用,我们必须使用实现。

例如

 configurations.implementation.allDependencies.each {
                if(it.group != null && (it.name != null || "unspecified".equals(it.name)) && it.version != null)
                {
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                }
            }