迁移到 BOM 后如何使用 maven-publish gradle 插件生成 maven pom

How to generate maven pom with maven-publish gradle plugin after migrating to BOM

我在 build.gradle 中使用以下内容通过 maven-publish 插件生成 pom:

configurations.implementation.allDependencies.each {
    if (it.group != null && (it.name != null || "unspecified" == 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)
    }
}

在使用如下依赖项时效果很好:

implementation 'com.google.firebase:firebase-config:19.2.0'
implementation 'com.google.firebase:firebase-messaging:20.3.0'
implementation 'com.google.firebase:firebase-analytics:17.6.0'
implementation 'com.google.firebase:firebase-crashlytics:17.2.2'

但在迁移到 BOM 后不再是:

implementation platform('com.google.firebase:firebase-bom:26.0.0')
implementation 'com.google.firebase:firebase-config'
implementation 'com.google.firebase:firebase-messaging'
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-crashlytics'

发布时不获取版本(因此不包括依赖项):

> Task :vendor:generatePomFileForPublication
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-bom', version='26.0.0', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-config', version='null', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-messaging', version='null', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-analytics', version='null', configuration='default'}
DefaultExternalModuleDependency{group='com.google.firebase', name='firebase-crashlytics', version='null', configuration='default'}

是否知道如何使用 gradle DSL 从 BOM 读取版本? 或者如何生成 pom.xml 的 dependencyManagement 部分来反映 BOM?

决定在生成的pom.xml

中的dependencyManagement下导入BOM
google(MavenPublication) {
    groupId 'group'
    artifactId 'artifact'
    version android.defaultConfig.versionName
    artifact(sourceJar)
    artifact("$buildDir/outputs/aar/my-release.aar") {
        extension 'aar'
    }
    pom.withXml {
        def dependenciesNode = asNode().appendNode('dependencies')
        def dependenciesManagementNode = asNode().appendNode('dependencyManagement').appendNode('dependencies')
        configurations.implementation.allDependencies.each {
            if (it.group != null && (it.name != null || "unspecified" == it.name)) {
                if (it.name.endsWith("-bom")) {
                    def dependencyNode = dependenciesManagementNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    dependencyNode.appendNode('version', it.version)
                    dependencyNode.appendNode('scope', "import")
                    dependencyNode.appendNode('type', "pom")
                } else {
                    def dependencyNode = dependenciesNode.appendNode('dependency')
                    dependencyNode.appendNode('groupId', it.group)
                    dependencyNode.appendNode('artifactId', it.name)
                    if (it.version != null) {
                        dependencyNode.appendNode('version', it.version)
                    }
                }
            }
        }
    }
}