无法获取 org.gradle.api.internal.artifacts.configurations 类型的配置容器的未知属性“runtime”

2022-09-02 13:52:09

当我按照如何使用gradle复制所有源jars的答案时,我得到了错误(无法为org.gradle.api.internal.artifacts.configurations.类型的配置容器获取未知属性“runtime”),并在IJ IDEA中运行该任务:

task copySourceJars( type: Copy ) {
  def sources = configurations.runtime.resolvedConfiguration.resolvedArtifacts.collect { artifact ->
    project.dependencies.create( [
      group: artifact.moduleVersion.id.group,
      name: artifact.moduleVersion.id.name,
      version: artifact.moduleVersion.id.version,
      classifier: 'sources'
    ] )
  }
  from configurations.detachedConfiguration( sources as Dependency[] )
    .resolvedConfiguration.lenientConfiguration.getFiles( Specs.SATISFIES_ALL )
  into file( 'some-directory/' )
}

我对错误进行了网络搜索,但在stackoverflow上只找到了Gradle - 无法为配置容器获取未知属性“scm”,建议像我在阅读该帖子后所做的那样添加配置:

configurations {
    runtime
}

之后,在任务运行时,它写了构建成功,但没有文件出现在硬盘驱动器上的特定目录中。由于某种原因,构建输出未列出:copySourceJarscopySourceJars

Executing tasks: [:app:generateDebugSources]

Gradle Daemon started in 1 s 427 ms
> Task :app:preBuild UP-TO-DATE
> Task :app:preDebugBuild UP-TO-DATE
> Task :app:compileDebugAidl NO-SOURCE
> Task :app:compileDebugRenderscript NO-SOURCE
> Task :app:checkDebugManifest UP-TO-DATE
> Task :app:generateDebugBuildConfig UP-TO-DATE
> Task :app:prepareLintJar UP-TO-DATE
> Task :app:generateDebugSources UP-TO-DATE

BUILD SUCCESSFUL in 9s
4 actionable tasks: 4 up-to-date

然后我再次从IDEA菜单中选择,并得到另一个错误:run

无法打开 dulrvlqnmf04w7h3gseaejvhm 的 init remapped class cache (/Users/user/.gradle/caches/5.2.1/scripts-remapped/ijresolvers_8btlazswoejgb1uwugudd695x/dulrvlqnmf04w7h3gseaejvhm/init3ca90766b0adfce53d4b035e7e9dc5fe).

无法为初始化脚本 '/private/var/folders/g7/2zrjv4hd04v4vkhkbm3stchjwwbz27/T/ijresolvers.gradle' (/Users/user/.gradle/caches/5.2.1/scripts/dulrvlqnmf04w7h3gseaejvhm/init/init3ca90766b0adfce53d4b035e7e9dc5fe) 打开 init 泛型类缓存。错误!源单元“BuildScript”中的“语义分析”阶段中的异常 不支持的类文件主要版本 57

我是否正确地使用IJ IDEA下载依赖项?也许添加配置没有正确完成?runtime


答案 1

这是因为您的 gradle 版本和 gradle7 之间存在差异。在 Gradle 6.x 中,禁用了使用参数来获取程序执行的配置。相反,您可以使用 来实现相同的能力。Gradle7 文档在那之后,我成功地完成了编译,并获得了项目的真正构造。configurations.runtimeconfigurations.runtimeClasspath

Deprecated Gradle features were used in this build, making it incompatible with Gradle 8.0.

You can use '--warning-mode all' to show the individual deprecation warnings and determine if they come from your own scripts or plugins.

See https://docs.gradle.org/7.1/userguide/command_line_interface.html#sec:command_line_warnings

项目建设


答案 2

按照 gradle 的升级文档中提到的用法:runtimeOnly

https://docs.gradle.org/current/userguide/upgrading_version_6.html#sec:configuration_removal


推荐