Gradle task is never up-to-date

2.1k Views Asked by At

In the configuration phase of the task I register some dir as builtBy: thisTask. I expect gradle to automatically detect if sources were changed, but the task always being executed.
Here is the task:

subprojects {

    def srcMainMirah = file('src/main/mirah')
    if (srcMainMirah.exists()) {

        idea.module.sourceDirs += srcMainMirah

        task compileMirah {

            def classesMirahMain = file("$buildDir/classes-mirah/main")

            inputs.sourceDir srcMainMirah
            def thisTask = delegate
            sourceSets.main {
                output.dir(classesMirahMain, builtBy: thisTask)
                java.srcDir srcMainMirah
            }
            dependsOn tasks.compileJava

            doFirst {
                def classpath = files("$buildDir/classes/main").plus(configurations.compile)
                mirahc(srcMainMirah, classesMirahMain, classpath)
            }
        }
    }
}

It is for compiling sources in mirah language, which produces *.class files just like java compiler does.

1

There are 1 best solutions below

2
On BEST ANSWER

Declaring inputs alone for a task is insufficient to determine if the task is up-to-date. You are required to also declared task.outputs

A task with no defined outputs will never be considered up-to-date. For scenarios where the outputs of a task are not files, or for more complex scenarios, the TaskOutputs.upToDateWhen() method allows you to calculate programmatically if the tasks outputs should be considered up to date.

A task with only outputs defined will be considered up-to-date if those outputs are unchanged since the previous build.

From section 17.9.1 here.