2
votes

Is there way in Gradle how to call parent function from subproject? If I have parent project and function defined as:

def testMethod() {
    println("TestMethod called from directory ${project.projectDir}")
}

and subproject as:

task subTest << {
    println("I'm subTest task from ${project.projectDir}")
    testMethod()
}

calling this from commandline results in:

Could not find method testMethod() for arguments [] on root project 'subsys1'
1
it works for me: $ gradle subTest :web-shop-war:subTest I'm subTest task from C:\...\web-shop\web-shop-war TestMethod called from directory C:\...\web-shop have you defined your subproject as a subproject in settings.gradle? - huzi
Yes, it works when you invoke it from root project directory as >gradle subsys1:subTest But I need to invoke >gradle subTest // from subsys1 directory, then it's somehow problem to construct project tree for Gradle. It's opposite tree direction when you invoke task from subproject. I would need to set parent project into subsystem settings.gradle so it knows, where to look for testMethod() - Matthew Lowe
What do you mean with "in subsystem settings.gradle"? Gradle only supports one settings.gradle for the entire project tree, which is located in the root project. - Hiery Nomus

1 Answers

1
votes
Could not find method testMethod() for arguments [] on root project 'subsys1'

Means that Gradle thinks that subsy1 is the root project, whereas your parent project should be the root project. This means that you have more than 1 settings.gradle, and it thinks that subsys1 is the start of the project hierarchy. There should only be 1 settings.gradle file, which should reside in the root project. There you configure each of the subprojects. They don't get their own settings.gradle files.