Search code examples
androidgradlemaven-publishmaven-bom

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


I was using the following for generating pom with maven-publish plugin in my build.gradle :

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)
    }
}

which was working great when using dependencies as follow :

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'

but not anymore after migrating to 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'

which do not get versions (and thus do not include dependencies) while publishing :

> 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'}

Does one knows how to read versions from the BOM using gradle DSL ? Or how to generate the dependencyManagement part of the pom.xml to reflect the BOM ?


Solution

  • Decided to import BOM under dependencyManagement in generated pom.xml

    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)
                        }
                    }
                }
            }
        }
    }