[Solved] groovy.lang.MissingPropertyException: Could not get unknown property ‘A’ for task ‘:B’ of type org.gradle.api.DefaultTask.

groovy.lang.MissingPropertyException this exception occurred when gradle not able to find a property. Gradle check these properties in sequence as below if not find out throw this exception.

  • gradle.properties
  • System environment variables
  • command-line arguments to execute the build script
  • build.gradle

Example

Here is one example where defining two dependent tasks detail and hello. Where a detail task is dependent on hello task the same declared is by using dependsOn.

See Also: How to define Gradle tasks?

build.gradle


task detail {
    dependsOn hello
    doLast {
        println "You are in 'Facing  Issues on IT'"
    }
}

task hello {
    doLast {
        println 'Hello!'
    }
}

Exception Stackrace

Here executing Gradle task detail and using –stacktrace to print the complete stack trace of exception in the console to find out the root cause.

F:\Workspace-Gradle\MyGradleJavaProject>gradlew detail -q --stacktrace

FAILURE: Build failed with an exception.

* Where:
Build file 'F:\Workspace-Gradle\MyGradleJavaProject\build.gradle' line: 40

* What went wrong:
A problem occurred evaluating root project 'MyGradleJavaProject'.
> Could not get unknown property 'hello' for task ':detail' of type org.gradle.api.DefaultTask.

* Try:
Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Exception is:
org.gradle.api.GradleScriptException: A problem occurred evaluating root project 'MyGradleJavaProject'.
        at org.gradle.groovy.scripts.internal.DefaultScriptRunnerFactory$ScriptRunnerImpl.run(DefaultScriptRunnerFactory.java:93)
        at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl$2.run(DefaultScriptPluginFactory.java:237)
        at org.gradle.configuration.ProjectScriptTarget.addConfiguration(ProjectScriptTarget.java:77)
        at org.gradle.configuration.DefaultScriptPluginFactory$ScriptPluginImpl.apply(DefaultScriptPluginFactory.java:242)
        at org.gradle.configuration.BuildOperationScriptPlugin$1$1.run(BuildOperationScriptPlugin.java:69)
..
...
...
Caused by: groovy.lang.MissingPropertyException: Could not get unknown property 'hello' for task ':detail' of type org.gradle.api.DefaultTask.
        at org.gradle.internal.metaobject.AbstractDynamicObject.getMissingProperty(AbstractDynamicObject.java:85)
        at org.gradle.internal.metaobject.ConfigureDelegate.getProperty(ConfigureDelegate.java:130)
        at build_ys26dll53lljjzslm0aousl9$_run_closure1.doCall(F:\Workspace-Gradle\MyGradleJavaProject\build.gradle:40)
...

* Get more help at https://help.gradle.org

BUILD FAILED in 1s

Reason of Exception

Here is the problem statement as given in cause is “Could not get unknown property ‘hello’ for task ‘:detail’ of type org.gradle.api.DefaultTask.”. It means statement dependOn hello inside the task detail not able to find task hello. Because hello task definition is after detail task and execution of the script is from Top to Down so while execution hello task not recognized and throw an exception as MissingPropertyException.

Solution

Always define your tasks before going to use it. For the above example define hello task before detail task as below.

task hello {
    doLast {
        println 'Hello!'
    }
}
task detail {
    dependsOn hello
    doLast {
        println "You are in 'Facing  Issues on IT'"
    }
}

In this example when run task detail then it’s dependent task hello will also execute.
Output

-------->gradlew detail -q

Hello!
You are in 'Facing  Issues on IT'

If this solution helps you please write comments and for any Gradle related issue post it here.