You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Gradle maven-publish依赖多发布模块报错,如何指定对应publication?

可以保留api project(':library')的项目依赖写法,无需提前发布core模块即可正常编译,以下是两种可行解决方案:

方案1:发布时重写POM依赖坐标(无需修改library模块配置)

直接在transaction-manager的发布配置中,对release/debug版本的POM文件依赖进行针对性替换,编译阶段仍走本地项目依赖,发布时自动替换为对应Maven坐标:

// build.gradle(Module:transaction-manager)
dependencies {
    api project(':library')
}

afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId "com.example.sdk"
                artifactId "transaction-manager"
                version "1.0.0"
                // 替换release版本的依赖坐标
                pom.withXml {
                    asNode().dependencies.dependency.each { dep ->
                        if (dep.artifactId.text() == project(':library').name) {
                            dep.groupId.setValue("com.example.sdk")
                            dep.artifactId.setValue("core")
                            dep.version.setValue("1.0.0")
                        }
                    }
                }
            }
            debug(MavenPublication) {
                from components.debug
                groupId "com.example.sdk"
                artifactId "transaction-manager-debug"
                version "1.0.0"
                // 替换debug版本的依赖坐标
                pom.withXml {
                    asNode().dependencies.dependency.each { dep ->
                        if (dep.artifactId.text() == project(':library').name) {
                            dep.groupId.setValue("com.example.sdk")
                            dep.artifactId.setValue("core-debug")
                            dep.version.setValue("1.0.0")
                        }
                    }
                }
            }
        }
    }
}

该方案改动量小,适配性高,适合快速解决问题。

方案2:基于Gradle变体能力匹配(官方推荐方案)

通过Gradle原生的变体感知能力规则,自动匹配对应构建类型的Publication,无需手动修改POM:

  1. 先修改:library模块的发布配置,为两个Publication声明对应能力:
// build.gradle(Module:library)
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId "com.example.sdk"
                artifactId "core"
                version "1.0.0"
                capabilities {
                    requireCapability("com.example.sdk:core:1.0.0")
                }
            }
            debug(MavenPublication) {
                from components.debug
                groupId "com.example.sdk"
                artifactId "core-debug"
                version "1.0.0"
                capabilities {
                    requireCapability("com.example.sdk:core-debug:1.0.0")
                }
            }
        }
    }
}
  1. 再修改transaction-manager模块的配置,声明不同构建类型需要的能力:
// build.gradle(Module:transaction-manager)
dependencies {
    api project(':library')
}

// 声明构建类型对应的能力匹配规则
configurations {
    releaseApi {
        capabilities.requireCapability("com.example.sdk:core:1.0.0")
    }
    debugApi {
        capabilities.requireCapability("com.example.sdk:core-debug:1.0.0")
    }
}

// 原有发布配置保持不变即可
afterEvaluate {
    publishing {
        publications {
            release(MavenPublication) {
                from components.release
                groupId "com.example.sdk"
                artifactId "transaction-manager"
                version "1.0.0"
            }
            debug(MavenPublication) {
                from components.debug
                groupId "com.example.sdk"
                artifactId "transaction-manager-debug"
                version "1.0.0"
            }
        }
    }
}

该方案是Gradle官方推荐的多变体依赖管理标准实现,扩展性更强,适合多模块多构建类型的复杂项目。


内容的提问来源于stack exchange,提问作者JC Castano

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.08 22:30:02