Sergei Ledvanov
Sergei Ledvanov

Reputation: 2251

Executing assembleRelease Gradle task does now work from custom task

I have the following Gradle task defined in my build.gradle file

task myTask() << {
    println "Start"
    assembleRelease.execute()
    println "end"
}

What I want is to execute assembleRelease gradle task when I execute myTask. However, what I get is the following output

Executing tasks: [myTask]

Configuration on demand is an incubating feature.
Relying on packaging to define the extension of the main artifact has been deprecated and is scheduled to be removed in Gradle 2.0
:gymgym:myTask
Start
end

BUILD SUCCESSFUL

As you can see, "Start" is followed by "end", meaning that assembleRelease has not been invoked.

What am I doing wrong?

Upvotes: 2

Views: 1898

Answers (1)

Krylez
Krylez

Reputation: 17820

Each task in Gradle is node on a DAG. It sounds like you're trying to surround one task with another, which doesn't fit the model. You can, however, append work to the beginning and/or end of any task using doFirst and doLast. If you want it to be optional, you can make it depend on an optional command line argument.

ext.SHOULD_WRAP = hasProperty('shouldWrap') ? shouldWrap.toBoolean() : false
if (SHOULD_WRAP) {
    wrapAssemble()
}
def start() {
    println "Start"
}
def end() {
    println "end"
}
def wrapAssemble() {
    assembleRelease.doFirst {
        start()
    }
    assembleRelease.doLast {
        end()
    }
}

With this configuration, calling ./gradlew assembleRelease -PshouldWrap=true will execute the assemble task with your prepended and appended work. Calling ./gradlew assembleRelease -PshouldWrap=false or simple ./gradlew assembleRelease will execute the assemble task normally.

Old answer

Just set your task as a dependency for assembleRelease:

task myTask() << { println "Start" println "end" } assembleRelease.dependsOn "myTask"

Now your task will always be executed when assembleRelease is.

Upvotes: 1

Related Questions