简体   繁体   English

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

[英]How to configure Gradle to not run other tasks when running 'gradle test'

I have a build.gradle file that contains the following task, as well as the Java plugin, Appengine plugin, and Jetty plugin: 我有一个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'
}

When I run gradle test , the tests are run and all code is compiled, but this task defined above also runs. 当我运行gradle test ,运行gradle test并编译所有代码,但上面定义的此任务也会运行。

I do not want that task to run, at least not in the beginning but instead at the end, but I'm struggling to see how to tell gradle test to just plain not run this task, as I may end up writing other tasks that I don't want gradle test to run. 我不希望这个任务运行,至少不是在开始时而是在结束时,但是我很难看到如何告诉gradle测试只是简单地不运行这个任务,因为我可能最终编写其他任务我不希望gradle测试运行。

I've looked through the documentation for the Java plugin, but I don't see anything in there that would effectively disable this behavior. 我查看了Java插件的文档,但我没有看到任何可以有效禁用此行为的内容。

How would I configure gradle so that this copyTestDeps task does not run unless I explicitly run gradle copyTestDeps from the terminal? 我如何配置gradle以便除非我从终端显式运行gradle copyTestDeps ,否则此copyTestDeps任务不会运行? I assume that the documented "doLast" method would work properly in making this run, so let's just focus on stopping gradle test from pretending like it owns everything. 我假设记录的“doLast”方法可以正常运行,所以让我们只关注停止gradle test ,假装它拥有一切。

You are mixing the configuration and execution phases. 您正在混合配置和执行阶段。 Your task seems to run, but only the print statements are executed; 您的任务似乎正在运行,但只执行了print语句; no files will be copied. 没有文件会被复制。 If you delete directory test-lib and do anything with Gradle (eg ./gradlew tasks ), you will see the instructions being printed (at the top of the Gradle output) but directory test-lib won't exist afterwards. 如果删除目录test-lib并对Gradle执行任何操作(例如./gradlew tasks ),您将看到正在打印的指令(位于Gradle输出的顶部),但之后将不再存在目录test-lib

Change your task definition to: 将您的任务定义更改为:

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'
    }
}

See: https://docs.gradle.org/current/userguide/build_lifecycle.html#sec:build_phases 请参阅: https//docs.gradle.org/current/userguide/build_lifecycle.html#sec : build_phases

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM