Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force task execution in Gradle

A certain amount of Gradle tasks I wrote, don't need any in- or output. Because of that, these tasks always get the status UP-TO-DATE when I call them. An example:

task backupFile(type: Copy) << {
    //Both parameters are read from the gradle.properties file
    from file(adjusting_file.replaceAll("\"", "")) 
    into file(backupDestinationDirectory + "/main/")

    println "[INFO] Main file backed up"
}

Which results in the following output:

:gradle backupFile
:backupFile UP-TO-DATE

Is there a way to force a(ny) task to execute, regardless of anything? If there is, is it also possible to toggle task execution (e.g. telling the build script which tasks to run and which tasks to ignore)?

I can't omit the << tags, as that would make the tasks to always execute, which isn't what I desire.

Many thanks in advance for your input.

like image 808
Pieter VDE Avatar asked May 03 '13 11:05

Pieter VDE


People also ask

Does Gradle build run all tasks?

You can execute multiple tasks from a single build file. Gradle can handle the build file using gradle command. This command will compile each task in such an order that they are listed and execute each task along with the dependencies using different options.

How do I run a default task in Gradle?

Gradle allows you to define one or more default tasks that are executed if no other tasks are specified. defaultTasks 'clean', 'run' tasks. register('clean') { doLast { println 'Default Cleaning! ' } } tasks.

How do I skip a task in Gradle?

To skip any task from the Gradle build, we can use the -x or –exclude-task option. In this case, we'll use “-x test” to skip tests from the build. As a result, the test sources aren't compiled, and therefore, aren't executed.


1 Answers

Tasks have to be configured in the configuration phase. However, you are configuring it in a task action (<< { ... }), which runs in the execution phase. Because you are configuring the task too late, Gradle determines that it has nothing to do and prints UP-TO-DATE.

Below is a correct solution. Again, I recommend to use doLast instead of << because it leads to a more regular syntax and is less likely added/omitted accidentally.

task backupFile(type: Copy) {
    from file(adjusting_file.replaceAll("\"", "")) 
    into file(backupDestinationDirectory + "/main/")
    doLast {
        println "[INFO] Main file backed up"
    }
}    
like image 121
Peter Niederwieser Avatar answered Sep 23 '22 10:09

Peter Niederwieser