Yaroslav
Yaroslav

Reputation: 4659

Gradle couldn't find method on root project, but I'm using subprojects

I have this simple build.gradle

subprojects {
    test {}
}

project(':module1') {
    apply plugin: 'java'
}

and Gradle complains that it could not find method test() on root project. But why does it try to find it on the root project? I read here that subprojects don't include root project. And if I put println name to subprojects I also don't see root project in the output.

Upvotes: 0

Views: 1260

Answers (2)

TobiSH
TobiSH

Reputation: 2921

@highstakes is right. But assuming that you have only one subproject (module1) it's just a question of timing. You first need to apply the java plugin before you can configure the tasks of this plugin. Taking you buildfile and changing the order you'll get:

project(':module1') {
    apply plugin: 'java'
}

subprojects {
   test {}
}

That will work as long as you have exactly one subproject.

The error message you get is that the gradle interprets test {} as a function call passing an empty closure and is looking for a defintion of this test function in the root project. Due that function was not added (by the java plugin) at this place you get this error messages.

Upvotes: 1

highstakes
highstakes

Reputation: 1519

  1. You are only applying the java plugin for one subproject so the others wont have the test closure available.

  2. You can try explicitly applying the plugin on the given subproject like this:

    project(':module1') { project -> project.apply plugin: 'java' }

Upvotes: 1

Related Questions