微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

迁移到BOM后如何使用Maven-Publish Gradle插件生成Maven Pom

如何解决迁移到BOM后如何使用Maven-Publish Gradle插件生成Maven Pom

我在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',name='firebase-messaging',name='firebase-analytics',name='firebase-crashlytics',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.name)
                    if (it.version != null) {
                        dependencyNode.appendNode('version',it.version)
                    }
                }
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。