Gradle“无法解析项目”,即使路径设置正确

4zcjmb1e  于 2023-10-19  发布在  其他
关注(0)|答案(1)|浏览(114)

我有这样的文件夹结构:

├── build.gradle.kts
├── processor
│   ├── build.gradle.kts
├── processor-api
│   ├── build.gradle.kts
├── settings.gradle.kts
└── src
    ├── main
    └── test

这是我的processor模块build.gradle.kts的样子:

plugins {
    `java-library`
    kotlin("jvm") version "1.9.10"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

//buildscript {
//    dependencies {
//        classpath(kotlin("gradle-plugin", version = "1.9.10"))
//    }
//}

dependencies {
    implementation(project(":processor-api"))
    implementation("com.google.devtools.ksp:symbol-processing-api:1.9.10-1.0.13")
}

tasks.test {
    useJUnitPlatform()
}

这是我的processor-api模块build.gradle.kts的样子:

plugins {
    kotlin("jvm") version "1.9.10"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

下面是:(主)模块build.gradle.kts的外观:

plugins {
    kotlin("jvm") version "1.9.10"
    id("com.google.devtools.ksp") version "1.9.10-1.0.13"
}

group = "org.example"
version = "1.0-SNAPSHOT"

repositories {
    mavenCentral()
}

dependencies {
    implementation(project(":processor-api"))
    ksp(project(":processor"))
    testImplementation("org.jetbrains.kotlin:kotlin-test:1.9.10")
}

tasks.test {
    useJUnitPlatform()
}

kotlin {
    jvmToolchain(8)
    sourceSets.main {
        kotlin.srcDir("build/generated/ksp/main/kotlin")
    }
    sourceSets.test {
        kotlin.srcDir("build/generated/ksp/test/kotlin")
    }
}

这就是我的settings.gradle.kts文件的样子:

rootProject.name = "leetcode-kotlin"
include("processor", "processor-api")

所以,我在这里试图实现的是,processormain都依赖于processor-api,并且main模块使用processor模块作为其注解处理器(这对这个问题来说并不重要,但我想我也会提到这一点)。现在,如果我从main模块运行build任务,我会得到这个错误:

Could not determine the dependencies of task ':compileJava'.
> Could not resolve all task dependencies for configuration ':compileClasspath'.
   > Could not resolve project :processor-api.
     Required by:
         project :
      > No matching variant of project :processor-api was found. The consumer was configured to find a library for use during compile-time, compatible with Java 8, preferably in the form of class files, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm' but:
          - Variant 'apiElements' capability org.example:processor-api:1.0-SNAPSHOT declares a library for use during compile-time, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8
          - Variant 'mainSourceElements' capability org.example:processor-api:1.0-SNAPSHOT declares a component, and its dependencies declared externally:
              - Incompatible because this component declares a component of category 'verification' and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its usage (required compile-time)
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')
          - Variant 'runtimeElements' capability org.example:processor-api:1.0-SNAPSHOT declares a library for use during runtime, packaged as a jar, preferably optimized for standard JVMs, and its dependencies declared externally, as well as attribute 'org.jetbrains.kotlin.platform.type' with value 'jvm':
              - Incompatible because this component declares a component, compatible with Java 17 and the consumer needed a component, compatible with Java 8
          - Variant 'testResultsElementsForTest' capability org.example:processor-api:1.0-SNAPSHOT:
              - Incompatible because this component declares a component of category 'verification' and the consumer needed a library
              - Other compatible attributes:
                  - Doesn't say anything about how its dependencies are found (required its dependencies declared externally)
                  - Doesn't say anything about its target Java environment (preferred optimized for standard JVMs)
                  - Doesn't say anything about its target Java version (required compatibility with Java 8)
                  - Doesn't say anything about its elements (required them preferably in the form of class files)
                  - Doesn't say anything about its usage (required compile-time)
                  - Doesn't say anything about org.jetbrains.kotlin.platform.type (required 'jvm')

main模块无法解析processor-api,我不知道为什么。

j9per5c4

j9per5c41#

我通过从main模块的build.gradle.kts中删除这一行来修复这个问题:

jvmToolchain(8)

Gradle不喜欢其他模块使用JDK 17,而主模块使用JDK 8。

相关问题