【发布时间】:2022-11-29 12:26:42
【问题描述】:
所以我在 kotlin 中得到了这个由多个子项目组成的项目。所有这些都与单个settings.gradle 文件链接。一切正常,直到对另一部分进行了一周的编码后,我注意到我从其中一个模块中得到了很多编译错误。所以我开始挖掘并发现 Intellij 无法从另一个子项目导入类,即使它通过 api project(':lib:content:model') 依赖于它。我无法弄清楚它停止工作并尝试在没有intellij的情况下仅使用graddlew构建项目的原因。然后我得到了奇怪的错误:
命令:./gradlew :lib:game:model:build
FAILURE: Build failed with an exception.
* What went wrong:
Circular dependency between the following tasks:
:lib:game:model:classes
\--- :lib:game:model:compileJava
+--- :lib:game:model:compileKotlin
| +--- :lib:game:model:jar
| | +--- :lib:game:model:classes (*)
| | +--- :lib:game:model:compileJava (*)
| | +--- :lib:game:model:compileKotlin (*)
| | \--- :lib:game:model:kaptKotlin
| | +--- :lib:game:model:jar (*)
| | \--- :lib:game:model:kaptGenerateStubsKotlin
| | \--- :lib:game:model:jar (*)
| \--- :lib:game:model:kaptKotlin (*)
\--- :lib:game:model:jar (*)
(*) - details omitted (listed previously)
我试图用谷歌搜索我的出路,但互联网上的所有建议都不适合我。最奇怪的是,当我以前写它的时候它曾经工作过。然后我就切换到另一个模块,当我回来的时候,一切都是这样的,坏了=(
我试图将有漏洞的代码隔离到测试中,并以包含两个子模块content和game的lib模块结束。另一个是 project-types 模块,我在其中存储 gradle 插件以克服配置重复。 content项目只有一个名为model的子项目和game项目有三个:api、impl,还有'model`。
lib->game->model项目依赖于lib->content->model
lib->game->impl项目依赖于lib->game->api
所以另一个非常奇怪的事情是 lib->game->impl WORKD 工作得很好,即使他依赖于 lib->game->api 也能编译。但同样的事情不适用于“lib->game->model”,它使用相同的机制依赖于“lib->content->model”
所以我迷路了,不知道该怎么办。有一个很好的理由,我需要所有这些都是不同的项目,但我不想每次更改其中一个中的代码时都将新工件发布到本地存储库。谁能帮我修一下 T-T
我的./gradlew -q projects输出:
------------------------------------------------------------
Root project 'test'
------------------------------------------------------------
Root project 'test'
\--- Project ':lib'
+--- Project ':lib:content'
| \--- Project ':lib:content:model'
\--- Project ':lib:game'
+--- Project ':lib:game:api'
+--- Project ':lib:game:impl'
\--- Project ':lib:game:model'
Included builds
\--- Included build ':project-types'
project-types 包含common 和kotlin-project 子项目。
commonbuild.gradle:
plugins {
id 'groovy-gradle-plugin'
}
repositories {
mavenCentral()
}
和 src 中的 commons.gradle 文件:
plugins {
id 'java'
}
repositories {
mavenCentral()
mavenLocal()
}
dependencies {
implementation 'com.fasterxml.jackson.core:jackson-core:2.13.4'
implementation 'com.fasterxml.jackson.core:jackson-annotations:2.13.4'
implementation 'com.fasterxml.jackson.core:jackson-databind:2.13.4'
implementation 'com.fasterxml.jackson.datatype:jackson-datatype-jsr310:2.13.4'
implementation 'com.fasterxml.jackson.module:jackson-module-paranamer:2.13.4'
implementation 'org.slf4j:slf4j-api:1.7.36'
implementation 'ch.qos.logback:logback-classic:1.2.11'
implementation 'commons-io:commons-io:2.11.0'
implementation 'org.apache.commons:commons-lang3:3.12.0'
implementation 'commons-codec:commons-codec:1.15'
}
kotlin-projectbuild.gradle 文件:
dependencyResolutionManagement {
repositories {
mavenLocal()
mavenCentral()
gradlePluginPortal()
google()
}
}
rootProject.name = 'project-types'
include 'commons'
include 'kotlin-project'
它是 src 中的 kotlin-project.gradle 文件:
plugins {
id 'commons'
id 'org.jetbrains.kotlin.jvm'
id 'org.jetbrains.kotlin.kapt'
}
dependencies {
implementation 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
implementation 'org.jetbrains.kotlin:kotlin-reflect'
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-jdk8:1.6.4'
implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.9.1'
testImplementation 'org.junit.jupiter:junit-jupiter-params:5.9.1'
testImplementation 'org.mockito:mockito-junit-jupiter:4.8.1'
testImplementation 'org.mockito.kotlin:mockito-kotlin:4.0.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.9.1'
}
test {
useJUnitPlatform()
}
compileKotlin {
kotlinOptions.jvmTarget = '17'
kotlinOptions.freeCompilerArgs = ['-Xjsr305=strict', '-Xemit-jvm-type-annotations']
}
compileTestKotlin {
kotlinOptions.jvmTarget = '17'
kotlinOptions.freeCompilerArgs = ['-Xjsr305=strict', '-Xemit-jvm-type-annotations']
}
kapt {
correctErrorTypes = true
}
所以我的主要项目结构是:
我的./setting.gradle文件内容:
rootProject.name = 'test'
includeBuild 'project-types'
include 'lib:game'
include 'lib:game:model'
include 'lib:game:api'
include 'lib:game:impl'
include 'lib:content'
include 'lib:content:model'
./build.gradle文件内容
plugins {
id 'org.jetbrains.kotlin.jvm' version "1.7.21" apply false
id 'org.jetbrains.kotlin.kapt' version "1.7.21" apply false
}
repositories {
mavenCentral()
}
./lib/content/model/build.gradle文件内容:
plugins {
id 'kotlin-project'
}
group 'cvazer.test'
version '1.0.0'
dependencies {
}
./lib/game/api/build.gradle文件内容:
plugins {
id 'kotlin-project'
}
group 'cvazer.test'
version '1.0.0'
dependencies {
}
./lib/game/impl/build.gradle文件内容:
plugins {
id 'kotlin-project'
}
group 'cvazer.test'
version '1.0.0'
dependencies {
api project(':lib:game:api')
}
./lib/game/model/build.gradle文件内容:(有问题的那个)
plugins {
id 'kotlin-project'
}
group 'cvazer.test'
version '1.0.0'
dependencies {
api project(':lib:content:model')
}
【问题讨论】: