gpt4 book ai didi

java - 如何配置Gradle在运行 'gradle test'时不运行其他任务

转载 作者:行者123 更新时间:2023-12-04 09:57:27 28 4
gpt4 key购买 nike

我有一个包含以下任务的 build.gradle 文件,以及 Java 插件、Appengine 插件和 Jetty 插件:

task(copyTestDeps, type: Copy)  {
from configurations.testRuntime - configurations.runtime
into 'test-lib'
def deps = configurations.testRuntime - configurations.runtime
println '\nAdd these to your classpath from the \'test-lib\' folder: \n'
deps.each { println "test-lib/" + it.getName() }
println '\n'
}

当我运行 gradle test 时,会运行测试并编译所有代码,但上面定义的这个任务也会运行。

我不希望该任务运行,至少不是在开始时而是在结束时运行,但我正在努力了解如何告诉 gradle test 只是简单地不运行该任务,因为我可能最终会写我不想让 gradle test 运行的其他任务。

我查看了 Java 插件的文档,但没有看到任何内容可以有效地禁用此行为。

我将如何配置 gradle,以便此 copyTestDeps 任务不会运行,除非我从终端明确运行 gradle copyTestDeps?我假设记录的“doLast”方法可以正常运行,所以让我们专注于阻止 gradle test 假装它拥有一切。

最佳答案

您正在混合配置和执行阶段。您的任务似乎在运行,但只执行了 print 语句;不会复制任何文件。如果您删除目录 test-lib 并对 Gradle 执行任何操作(例如 ./gradlew tasks),您将看到正在打印的说明(在 Gradle 输出的顶部)但目录 test-lib 之后将不存在。

将您的任务定义更改为:

task copyTestDeps(type: Copy)  {
// Configure how the copy task must behave during execution
from configurations.testRuntime - configurations.runtime
into 'test-lib'

// Run the custom code during execution
doLast {
def deps = configurations.testRuntime - configurations.runtime
println '\nAdd these to your classpath from the \'test-lib\' folder: \n'
deps.each { println "test-lib/" + it.getName() }
println '\n'
}
}

参见:https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:build_phases

关于java - 如何配置Gradle在运行 'gradle test'时不运行其他任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33306067/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com