Task declaration order and multi-projects

(michael.juergens) #1


I have a question concerning task declaration order and multi-projects.

We use gradle in a multi project environment. Common build functionality is placed in a non-project directory beside the subprojects.

This common build functionality is included in the main project gradle file via

subprojects {
 apply from: "$rootDir/include/jar.gradle"

The file jar.gradle contains for example a custom task “CollectDeploymentDescriptor” and a configured task “collectDeploymentDescriptor”:

task collectDeploymentDescriptor(type: CollectDeploymenDescriptors) {
  class CollectDeploymenDescriptors extends DefaultTask {

When an additonal tasks in a subproject “framework” of type “CollectDeploymentDescriptor” is defined, the following error occurs

Cause: Could not find property 'CollectDeploymenDescriptor' on project ':framework'.

It seems that the order of task declaration is bad for me. Do you have an idea to solve my problem?

Best regards, Michael

(Luke Daley) #2

Classloaders are isolated between scripts to prevent pollution and conflicts by plugins.

The solution is to assign the class object to a project property and reference that way…

In ‘jar.gradle’:

class CollectDeploymenDescriptors extends DefaultTask {
  project.collectDeploymentDescriptors = CollectDeploymentDescriptors

Then in your other build.gradle:

task customCollectDeploymentDescriptors(type: collectDeploymentDescriptors)

In short, classes defined in a build script are not available by name to any other build script.

(michael.juergens) #3

Works fine! Thank you.