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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27837421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 05:07:41  来源:igfitidea点击:

How to run a single gradle task from command line

javagradle

提问by Tomin

In my project I have several tasks in my 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. How to prevent a task being running?.

在我的项目中,我的 build.gradle 中有几个任务。我希望这些任务在运行时是独立的。即我需要从命令行运行单个任务。但是命令“gradle taskA”将同时运行我不想要的 taskA 和 taskB。如何防止任务正在运行?

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. enter image description here

这是我得到的输出。 在此处输入图片说明

采纳答案by Opal

You can use -xoption or --exclude-taskswitch to exclude task from task graph. But it's good idea to provide runnable example.

您可以使用-xoption 或--exclude-taskswitch 从任务图中排除任务。但是提供可运行的示例是个好主意。

回答by TobiSH

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'. 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”的快捷方式“<<”。doLast 步骤仅在任务执行时执行,而任务的配置将在解析 gradle 文件时执行。

When you call

你打电话时

gradle runSchema

You'll see the 'Configuring SQL-Task'and afterwards the 'Creating schema'output. That means the runSQLTask will be configured but not executed.

您将看到“配置 SQL 任务”,然后是“创建模式”输出。这意味着 runSQLTask 将被配置但不执行。

If you call

如果你打电话

gradle runSQL

Than you you'll see:

比你会看到:

Configuring SQL-Task :runSchema Creating schema :runsSQL Executing SQL

配置 SQL 任务 :runSchema 创建模式 :runsSQL 执行 SQL

runSchema is executed because runSQL depends on it.

runSchema 的执行是因为 runSQL 依赖于它。