22
votes

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?.

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

2
I see that you run gradle command. I can't find gradle.exe on my PC. I'm new to Android and Gradle build system. Where can I find the gradle executable?Saeed Neamati
@SaeedNeamati Use gradlew if you don't have Gradle installed (most Gradle projects contain the wrapper gradlew), otherwise you can install Gradle from the Gradle website.ThePyroEagle
You do not need to apply each plugin to each task individually. Simply doing apply plugin: 'java' and apply plugin: 'groovy' at the start of the file will work.ThePyroEagle

2 Answers

21
votes

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.

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.

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.

If you call

gradle runSQL

Than you you'll see:

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

runSchema is executed because runSQL depends on it.

4
votes

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