首页 > 解决方案 > Gradle 构建问题:无法在咖啡因模拟器中获取未知属性“库”

问题描述

我曾经使用 Eclipse 在我的 Caffeine's Simulator 分支上工作,并且该项目已编译并构建好。

突然,我在运行 Gradle 构建时开始收到以下错误:

Could not get unknown property 'libraries' for object of type org.gradle.api.internal.artifacts.dsl.dependencies.DefaultDependencyHandler.

我确实不理解 Caffeine 的 Simulator build.gradle 依赖项中的语法“implementation libraries.X”。

为了研究这个问题,我在 Eclipse 中生成了 MWE 项目,其 build.gradle 如下:

plugins {
    id 'java-library'
}

repositories {
    jcenter()
}

dependencies {
    api 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:28.2-jre'
    testImplementation 'junit:junit:4.12'
    implementation libraries.xz
}

这个 MWE 给出了同样的错误。删除行 implementation libraries.xz 后,项目构建正常。

我猜“库”是在咖啡因某处定义的属性/变量,但找不到它。

标签: gradlebuildpropertiessimulatorcaffeine

解决方案


依赖项的清单在 dependencies.gradle 中定义。根构建文件将此应用于所有子项目,

subprojects {
  apply plugin: 'biz.aQute.bnd.builder'
  apply plugin: 'java-library'
  apply plugin: 'eclipse'
  apply plugin: 'idea'

  apply from: "${rootDir}/gradle/publish.gradle"
  apply from: "${rootDir}/gradle/codeQuality.gradle"
  apply from: "${rootDir}/gradle/dependencies.gradle"
  ...
}

中央清单允许更抽象地引用依赖项,以便可以全局升级版本并减少复制和粘贴问题。

这听起来像是对构建的本地更改导致它不再导入清单并且无法解析属性。


推荐阅读