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

未配置compileOnly却出现Gradle构建错误的原因及解决方法

问题:未手动配置compileOnly依赖,却出现解析该配置的报错

错误提示

解析依赖配置‘compileOnly’不被允许,因为它被定义为‘canBeResolved=false’。应解析一个继承自‘compileOnly’且可解析(‘canBeResolved=true’)的依赖配置。

用户提供的build.gradle代码

buildscript {
    ext.kotlin_version = '1.6.0'

    repositories {
        mavenCentral()

        maven {
            url "https://oss.sonatype.org/content/repositories/snapshots/"
        }

        maven {
            url "https://plugins.gradle.org/m2/"
        }

        repositories {
            flatDir {
                dirs 'jars'
            }
        }
    }

    dependencies {
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
    }
}


plugins {
    id "org.jetbrains.intellij" version "1.10.1"
    id "org.jetbrains.grammarkit" version "2020.1"

}

configurations {
    configurations.implementation.setCanBeResolved(true)
}

group 'com.interfaced'
version '0.2.7'

apply plugin: 'kotlin'
apply plugin: 'org.jetbrains.intellij'

sourceSets {
    all {
        java.srcDirs += ['src/main/gen']
        kotlin.srcDirs += ['src/main/kotlin']
        resources.srcDirs = ['src/main/resources']
    }
}


grammarKit {
    jflexRelease = '1.7.0-2'
}

repositories {
    mavenCentral()
    maven {
        url "https://cache-redirector.jetbrains.com/intellij-dependencies"
    }
}

dependencies {
    // https://mvnrepository.com/artifact/org.jetbrains.kotlin/kotlin-stdlib
    compileClasspath  group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.7.22'

    compileClasspath  files('jars/grammar-kit.jar')
    compileClasspath  group: 'junit', name: 'junit', version: '4.12'
    compileClasspath  "org.jetbrains.kotlin:kotlin-stdlib-jdk7"

}

intellij {
    version '2022.3'
}

import org.jetbrains.grammarkit.tasks.GenerateLexer
import org.jetbrains.grammarkit.tasks.GenerateParser

def GENERATE_GROUP = 'Generate'

task generateLexer(type: GenerateLexer) {
    source = "src/main/grammar/BrightScript.flex"
    targetDir = "src/main/gen/com/interfaced/brs/lang/lexer"
    targetClass = "_BrsLexer"
    skeleton = "src/main/grammar/idea-flex.skeleton"
    purgeOldFiles = true

    description = 'Generate Lexer Java sources for BrightScript'
    group = GENERATE_GROUP
}

task generateParser(type: GenerateParser) {
    source = "src/main/grammar/BrightScript.bnf"
    targetRoot = 'src/main/gen'
    pathToParser = 'src/main/gen/com/interfaced/brs/lang/BrsParser.java'
    pathToPsiRoot = 'src/main/gen/com/interfaced/brs/lang/psi'
    purgeOldFiles = true

    description = 'Generate Parser Java sources for BrightScript'
    group = GENERATE_GROUP

    // patch up to date check
    outputs.upToDateWhen { false }
}

compileKotlin {
    kotlinOptions.jvmTarget = "17"
}

compileKotlin.dependsOn(generateLexer, generateParser)

问题原因

  • org.jetbrains.intellij插件会自动引入compileOnly这类消费型依赖配置,这类配置默认canBeResolved=false,仅用于声明依赖,不能直接被解析。
  • 脚本中configurations.implementation.setCanBeResolved(true)的操作破坏了Gradle默认的依赖配置规则,导致插件内部的compileOnly配置解析逻辑冲突。
  • 直接给compileClasspath添加依赖的方式不规范,compileClasspath是解析型配置,应由implementation等消费型配置自动传递依赖,而非手动修改。

解决方法

1. 移除违规配置

删除以下代码块,Gradle默认的implementation为消费型配置,无需手动设置canBeResolved=true:

configurations {
    configurations.implementation.setCanBeResolved(true)
}

2. 修正依赖声明方式

将dependencies块中的compileClasspath替换为对应类型的依赖配置,区分主代码依赖和测试依赖:

dependencies {
    implementation group: 'org.jetbrains.kotlin', name: 'kotlin-stdlib', version: '1.7.22'
    implementation files('jars/grammar-kit.jar')
    testImplementation group: 'junit', name: 'junit', version: '4.12'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7"
}
  • implementation:用于主代码编译和运行时都需要的依赖
  • testImplementation:仅用于测试代码的依赖(如JUnit)

3. 升级插件版本(可选)

旧版本org.jetbrains.intellij插件可能存在兼容性问题,建议升级到稳定新版本,比如:

plugins {
    id "org.jetbrains.intellij" version "1.16.0"
    id "org.jetbrains.grammarkit" version "2020.1"
}

内容的提问来源于stack exchange,提问作者Vladimir Kozhaev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 15:45:53