Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Gradle copy task fails silently

Tags:

gradle

I have a copy task for 1 file

task myCopyTask(type: Copy) {
    copy {
        from "/path/to/my/file"
        into "path/to/out/dir"
    }
}

How to do, so the task fails if the copy fails? Right now if the file does not exist, it does not give an error.


Fail Gradle Copy task if source directory not exist gives a solution. This does not work, because if everything is not inside of

copy { ... }

the task does not work at all.


I tried also

task myCopyTask(type: Copy) {
    copy {
        from "/path/to/my/file"
        into "path/to/out/dir"
        inputs.sourceFiles.stopExecutionIfEmpty()
    }
}

The above would fail, as inputs.sourceFiles would be empty.

like image 251
Sogartar Avatar asked Dec 06 '16 14:12

Sogartar


2 Answers

Why don't you specify your task as:

task myCopyTask(type: Copy) {
    from "/path/to/my/file"
    into "path/to/out/dir"
    inputs.sourceFiles.stopExecutionIfEmpty()
}

This would work as expected during execution phase, while your solution would try to copy something during configuration phase of the build every time you call any task.

like image 125
Stanislav Avatar answered Nov 15 '22 10:11

Stanislav


The very first definition of the tasks actually doesn't do what you expect from the task:

task myCopyTask(type: Copy) {
copy {
    from "/path/to/my/file"
    into "path/to/out/dir"
}

is acutually same as

task myCopyTask(type: Copy) {
    project.copy {
        from "/path/to/my/file"
        into "path/to/out/dir"
    }
}

And it will execute copy action during task configuration, no matter if the task is called or not.

What you need is:

task myCopyTask(type: Copy) {
    from "/path/to/my/file"
    into "path/to/out/dir"

    doFirst {
        if(inputs.empty) throw new GradleException("Input source for myCopyTask doesn't exist")
    }
}
like image 3
Ivan Frolov Avatar answered Nov 15 '22 11:11

Ivan Frolov