문제

I have a very simple build script like so

task hello{
    println("hello World")
}

task bye {
    println("bye")
}

On the command line I run gradle hello and I get the following output:

hello World
bye
:hello UP-TO-DATE

Why is it executing the task "bye" (I'm assuming it gets executed since "bye" gets printed)? Thanks.

도움이 되었습니까?

해결책

It's a common pitfall:

task hello {
    println("Any code in here is about *configuring* the\
    task. By default, all tasks always get configured.")
    doLast {
        println("Any code in here is about *executing* the task.\
        This code only gets run if and when Gradle decides to execute the task.")
    }
}

The distinction between configuration phase and execution phase is probably the single most important concept to understand in Gradle. It can be confusing at first, and may go away in the future. A kind of analogue in the Ant/Maven world is that these tools first parse XML build scripts and build an object model (perhaps resolving some properties along the way), and only then execute the build.

다른 팁

Adding to Peter answer, If you want to execute all task , you can specify the defaultTasks list.

defaultTasks 'clean', 'run'

task clean {
    doLast {
        println 'Default Cleaning!'
    }
}

task run {
    doLast {
        println 'Default Running!'
    }
}

task other {
    doLast {
        println "I'm not a default task!"
    }
}

Output

Output of gradle -q
> gradle -q
Default Cleaning!
Default Running!

More details can be found here https://docs.gradle.org/current/userguide/tutorial_using_tasks.html

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top