【问题标题】:Gradle Task messes with runtime DependenciesGradle 任务与运行时依赖项混淆
【发布时间】:2018-10-29 17:50:09
【问题描述】:

gradle 的另一个奇怪行为...

所以我找到了这篇文章: Gradle exclude module for Copy task

完全没问题,就像一种魅力,可以将某些东西排除在复制之外。

但这就是有趣的地方。这是我的复制任务的外观:

task copyDependencies(type: Copy) {
    into "$buildDir/libs/dependencies"
    from configurations.runtime {
        exclude module: 'groovy'
        exclude module: 'aws-java-sdk-s3'
        exclude module: 'commons-io'
    }
}

如果我尝试通过 Gradles 的“应用程序运行”任务运行应用程序。 “无法找到或加载主类 xxx” 失败。深入研究这个问题,我发现 Groovy 无法解决。

我什至不运行这个任务,或者依赖它。 但如果我这样注释掉第 4 行:

task copyDependencies(type: Copy) {
    into "$buildDir/libs/dependencies"
    from configurations.runtime {
        //exclude module: 'groovy'
        exclude module: 'aws-java-sdk-s3'
        exclude module: 'commons-io'
    }
}

应用程序正常启动,直到达到需要 Commons-IO 的程度。我仍然想在其他时候使用这个 copyDependencies 任务,但不改变那里的代码。

有人可以解释一下这种行为吗?

我想在 gradle 文件中的任何位置操作 configuration.runtime,为其他所有任务更改它?

【问题讨论】:

  • 我试图在我的回答中解释这种行为,如果足够清楚,请告诉我。
  • 接受了。它奏效了,尽管我现在使用另一种方式将应用程序与其依赖项分开。仍然很高兴知道这些似乎是静态声明并在各个任务之间共享。

标签: gradle groovy


【解决方案1】:

在您的from 配置块中,您引用了runtime 配置,但同时您通过添加一些排除规则来更改此配置。这将改变原始(和唯一的)runtime 配置,正如您所猜测的那样,您的构建项目中的所有其他任务都将使用该配置。这解释了您在尝试执行 run 任务时遇到的“无法找到或加载主类 xxx” 错误,因为 runtime 配置(类路径)不包含所需的库.

如果您想在您的 copyDependencies 任务中按组和/或模块编写排除规则,一种可能的方法是处理原始文件的副本 runtime 配置;你可以为此定义一个新的配置

configurations{
    runtimeDeps.extendsFrom runtime
}

task copyDependencies(type: Copy) {
    into "$buildDir/libs/dependencies"
    from configurations.runtimeDeps {
        exclude module: 'groovy'
        exclude module: 'aws-java-sdk-s3'
        exclude module: 'commons-io'
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-12
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多