多次运行gradle任务 [英] Run gradle task multiple times

查看:256
本文介绍了多次运行gradle任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个gradle项目,在子目录中包含两个模块。目录结构如下。

I have a gradle project containing two modules in subdirectories. The directory structure is as below.

root
    module1
        build.gradle
    module2
        build.gradle
    build.gradle
    settings.gradle

顶级 settings.gradle 包括两个模块。顶层 build.gradle 包含以下内容。

The top level settings.gradle includes the two modules. The top level build.gradle contains the following.

task runScript(type: Exec) {
    workingDir 'scripts'
    commandLine 'python3', 'myscript.py'
}

project(':module1') {
    evaluationDependsOn(':module1')

    final test = tasks.findByName('test')
    test.dependsOn(runScript)
}

project(':module2') {
    evaluationDependsOn(':module2')

    final test = tasks.findByName('test')
    test.dependsOn(runScript)
}

任务 runScript 设置数据库处于已知状态,并且必须在每个模块测试任务之前运行

Task runScript sets the database to a known state and must be run before each module test task.

当我运行测试任务时,我的脚本只能执行一次。我如何确保其执行多次?

When I run the test task my script only executes once. How can I ensure it executes multiple times?

$ ./gradlew test 
... some output

:runScript
RUNNING MY SCRIPT

:module1:test
RUNNING MODULE1 TESTS

... some output

:module2:test
RUNNING MODULE2 TESTS



我尝试过的事情



我尝试将 outputs.upToDateWhen {false} 添加到任务 runScript 因此Gradle从未认为它是最新的。这没有任何区别;我认为是因为任务仍然只在任务图中出现一次?

Things I Tried

I tried adding outputs.upToDateWhen {false} to the task runScript so Gradle never thinks it is up to date. This didn't make any difference; I assume because the task still only occurs once in the task graph?

我尝试将包含 dependsOn 的行替换为每个带有 test.doFirst {runScript.execute()} 的模块。这会在任务执行时发生变化,但不会导致多次执行。

I tried replacing the lines containing dependsOn for each module with test.doFirst {runScript.execute()}. This changes when the task gets executed but does not result in multiple executions.

$ ./gradlew test 
... some output

:module1:test
RUNNING MY SCRIPT
RUNNING MODULE1 TESTS

... some output

:module2:test
RUNNING MODULE2 TESTS

我尝试为每个模块创建一个新任务。

I tried creating a new task for each module. This works but it's duplicating code.

project(':module1') {
    evaluationDependsOn(':module1')

    final test = tasks.findByName('test')

    task runScript(type: Exec) {
        workingDir '../scripts'
        commandLine 'python3', 'myscript.py'
    }

    test.dependsOn(runScript)
}

project(':module2') {
    evaluationDependsOn(':module2')

    final test = tasks.findByName('test')

    task runScript(type: Exec) {
        workingDir '../scripts'
        commandLine 'python3', 'myscript.py'
    }

    test.dependsOn(runScript)
}


推荐答案

如果每个的每次运行都需要脚本Test 任务,只需确保在每个 Test 任务之前执行它即可。为什么还要使用任务呢?

If your script is necessary for each run of each Test task, simply assure its execution before each Test task. Why even use a task then?

subprojects {
    tasks.withType(Test) {
        doFirst {
            exec {
                workingDir 'scripts'
                commandLine 'python3', 'myscript.py'
            }
        }
    }
}

这篇关于多次运行gradle任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆