Warning when accessing `project` in Gradle 8.12

I have upgraded to Gradle 8.12 and I have this very simple task definition that is a part of a deploy task:

tasks.register('copyResources') {
    doLast {
        copy {
            from project(':myProject').file('src/main/resources/schema.json')
            into 'build/deploy'
        }
    }
}

However, it reports a warning because project is used:

Build file 'X:\Project\myProject\build.gradle': line 44
Invocation of Task.project at execution time has been deprecated. This will fail with an error in Gradle 10.0. This API is incompatible with the configuration cache, which will become the only mode supported by Gradle in a future release. Consult the upgrading guide for further information: https://docs.gradle.org/8.12/userguide/upgrading_version_7.html#task_project
        at build_5325j12ut850wdqiloxse0sel$_run_closure4$_closure9$_closure12.doCall$original(X:\Project\myProject\build.gradle:44)
        (Run with --stacktrace to get the full stack trace of this deprecation warning.)

I think I found correct documentation but honestly I simply don’t understand what it says. Mostly because I don’t work with Gradle daily.

Could somebody tell me how to replace from project(':myProject').file('src/main/resources/schema.json') line to a correct one?

Thank you!

Those few lines are following various bad practices anyway.
But to focus on the question, the project(...) call is done on project which is what the warning complains about.
You could for example move the calculation of the argument for from to configuration time by moving it outside the doLast and storing the result in a local variable.

Is this

tasks.register('copyResources') {
    def schemaFile = project(':myProject').file('src/main/resources/schema.json')
    doLast {
        copy {
            from schemaFile
            into 'build/deploy'
        }
    }
}

what you mean? It appears it works. Thank you!

Yes, exactly that :slight_smile:

1 Like