简体   繁体   English

如何从命令行运行单个gradle任务

[英]How to run a single gradle task from command line

In my project I have several tasks in my build.gradle. 在我的项目中,我在build.gradle中有几个任务。 I want those tasks to be independent while running. 我希望这些任务在运行时是独立的。 ie I need to run a single task from command line. 即我需要从命令行运行单个任务。 But the command "gradle taskA" will run both taskA and taskB which I do not want. 但命令“gradle taskA”将运行我不想要的taskA和taskB。 How to prevent a task being running?. 如何防止任务正在运行?

Here's a sample of what I am doing. 这是我正在做的事情的样本。

   task runsSQL{
    description 'run sql queries'
    apply plugin: 'java'
    apply plugin: 'groovy'

    print 'Run SQL'  }

task runSchema{
    apply plugin: 'java'
    apply plugin: 'groovy'

    print 'Run Schema' }

Here's the output I'm getting. 这是我得到的输出。 在此输入图像描述

I guess the point that you missed is that you dont define tasks here but you configured tasks. 我想你错过的一点是你不在这里定义任务,但是你配置了任务。 Have a look at the gradle documentation: http://www.gradle.org/docs/current/userguide/more_about_tasks.html . 请查看gradle文档: http//www.gradle.org/docs/current/userguide/more_about_tasks.html

What you wanted is something like this: 你想要的是这样的:

task runsSQL (dependsOn: 'runSchema'){
    description 'run sql queries'
    println 'Configuring SQL-Task' 
    doLast() {
        println "Executing SQL"
    }
}

task runSchema << {
    println 'Creating schema' 
}

Please mind the shortcut '<<' for 'doLast'. 请注意“doLast”的快捷方式“<<”。 The doLast step is only executed when a task is executed while the configuration of a task will be executed when the gradle file is parsed. doLast步骤仅在执行任务时执行,而在解析gradle文件时将执行任务配置。

When you call 你打电话的时候

gradle runSchema

You'll see the 'Configuring SQL-Task' and afterwards the 'Creating schema' output. 您将看到“配置SQL任务” ,然后是“创建模式”输出。 That means the runSQLTask will be configured but not executed. 这意味着将运行runSQLTask但不会执行。

If you call 如果你打电话

gradle runSQL

Than you you'll see: 比你看,你会看到:

Configuring SQL-Task :runSchema Creating schema :runsSQL Executing SQL 配置SQL-Task:runSchema创建模式:runsSQL执行SQL

runSchema is executed because runSQL depends on it. 执行runSchema是因为runSQL依赖于它。

You can use -x option or --exclude-task switch to exclude task from task graph. 您可以使用-x选项或--exclude-task开关从任务图中排除任务。 But it's good idea to provide runnable example. 但提供可运行的示例是个好主意。

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

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