如何将以下发布从Gradle 4迁移到5?

publishing {
    publications {
        foo(MavenPublication) {
            artifactId "foo"
            artifact jar

            pom.withXml {
                def dependenciesNode = asNode().appendNode('dependencies')

                def dependencyNode = dependenciesNode.appendNode('dependency')
                dependencyNode.appendNode('groupId', 'mygroup')
                dependencyNode.appendNode('artifactId', 'bar')
                dependencyNode.appendNode('version', '1.0')

            }
        }
    }
}

这适用于gradle 4.5.1。但是对于gradle 5.4.1,错误:
Execution failed for task ':publishFooPublicationToMavenLocal'.
> Failed to publish publication 'foo' to repository 'mavenLocal'
   > Invalid publication 'foo': artifactId is not a valid Maven identifier ([A-Za-z0-9_\-.]+).

最佳答案

使用gradle 4.8+,您必须将publishing{}块包含在project.afterEvaluate

project.afterEvaluate {
    publishing {
      publications {
        foo(MavenPublication) {
          artifactId "foo"
          artifact jar

          pom.withXml {
            def dependenciesNode = asNode().appendNode('dependencies')

            def dependencyNode = dependenciesNode.appendNode('dependency')
            dependencyNode.appendNode('groupId', 'mygroup')
            dependencyNode.appendNode('artifactId', 'bar')
            dependencyNode.appendNode('version', '1.0')

         }
      }

    }
}

您可以找到official doc here:

10-08 00:45