假设我们有一个Gradle项目,其结构如下:
.
├── a
│
└── b
// a build.gradle.kts
plugins {
kotlin("jvm")
}
group = "org.example.a"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
maven(url = "https://some-external-repo.com")
}
dependencies {
implementation("com.externaldependency:1.0.1")
}
// b build.gradle.kts
plugins {
kotlin("jvm")
}
group = "org.example.b"
version = "1.0-SNAPSHOT"
repositories {
mavenCentral()
}
dependencies {
implementation(project(":a"))
}
由于某种未知的原因,Gradle要求在a
中声明的所有存储库也存在于b
中。
如果不这样做,在IntelliJ Idea中同步时,我们将收到以下错误:
Could not find com.externaldependency:1.0.1.
Required by:
project :b > project :a
Possible solution:
- Declare repository providing the artifact, see the documentation at https://docs.gradle.org/current/userguide/declaring_repositories.html
但是,build运行良好。
为什么它是这样工作的?它有点破坏了项目之间的封装。
-
从
a
和b
模块中删除repositories
块。 -
在
settings.gradle.kts
:中声明dependencyResolutionManagement
rootProject.name = "test" include("a") include("b") dependencyResolutionManagement { repositories { mavenCentral() maven(url = "https://some-external-repo.com") } }