Richard Tingle
Richard Tingle

Reputation: 17226

Why is my custom Gradle task running even when the input is unchanged

I've created a custom Gradle task in Java (just put within buildSrc, so a local custom task)

public class ImageMergerTask extends DefaultTask{
    public File directory;

    @TaskAction
    public void greet() {
        System.out.println("hello from ImageMergerTask " + directory.toString());
    }

    @InputDirectory
    public File getDirectory(){
        return directory;
    }

    public void setDirectory(File directory){
        this.directory = directory;
    }
}

Used in the build.gradle like

task imageMerger(type : ImageMergerTask) {
    directory file('src/main/resources/someFolder')
}

processResources.dependsOn(imageMerger);

My impression is that by annotating getDirectory() with @InputDirectory, it ought to only run this task if the contents of src/main/resources/someFolder changes, but it seems to run every time, always including in the console:

> Task :imageMerger
hello from ImageMergerTask C:\Users\richa\Documents\Development\starlight\src\main\resources\someFolder

How should a custom task declare its inputs to properly take advantage of up-to-date detection?

Upvotes: 1

Views: 240

Answers (1)

Richard Tingle
Richard Tingle

Reputation: 17226

It seems that gradle doesn't make the up-to-date detection unless there is also an output directory. Adding this to the task made it correctly only run the task when the contents of the input directory changed

@OutputDirectory
public File getOutDirectory(){
    return new File(getProject().getBuildDir(), "someFolder");
}

As found by @ThomasKläger the Gradle documentation confirms this

Also note that incremental build won’t work unless a task has at least one task output

Upvotes: 0

Related Questions