Can't access configuration defined in project 'sub2' from project 'sub1'

Hello, I’m pretty new to both gradle and groovy. I’ve got a bit of trouble of accessing a configuration defined in one project from another project. I’ve got it working in the end but the solution is very confusing to me and I’m not sure if I’m really doing the right thing there. So here’s my question.

The Problem -----------------

I’ve got a very simple multi-project structure just like below:

Root project ‘gradle_test’

±-- Project ‘:sub1’

— Project ‘:sub2’

This is what the ‘build.grade’ file looks like for sub1 project:

// build.gradle of sub1 project

task testConfiguration {

println project(’:sub2’).configurations.sub2FooConfiguration

}

And finally, this is the ‘build.grade’ file of sub2 project:

// build.gradle of sub2 project

configurations {

sub2FooConfiguration

}

Very minimum. Now, if I run ‘gradle :sub1:testConfiguration’, I got the following error:

A problem occurred evaluating project ‘:sub1’.

Could not find property ‘sub2FooConfiguration’ on configuration container.

However, everything just works if the ‘testConfiguration’ task in sub1 project is modified like this:

// notice the “<<” (I believe this is calling the ‘doLast’ method on the task instance)

task testConfiguration << {

println project(’:sub2’).configurations.sub2FooConfiguration

}

The Question -------------------

What I assume the difference between the two versions of the ‘testConfiguration’ task is that in the first instance, a ‘configuration closure’ is passed to the task whereas in the modified version a ‘normal’ closure is passed to the ‘doLast’ method.

So, first of all, is my assumption correct?

Secondly, why I don’t have access to ‘sub2’ project in the first instance?

And finally, is it possible to access ‘sub2’ project in the first instance (i.e. in the configuration closure)?

The difference is as you expect, the second example using the left shift (’<<’) operator calls the ‘doLast’ method which adds a task action. The main change here is when the closure is executed. In the first example it is at configuration type (ie. when Gradle “parses” your build.gradle). The second is during execution (when Gradle runs the ‘testConfiguration’ task).

Which method to use depends on your use case. If you need the configuration information at configuration time in the other project, you can tell Gradle that ‘proj2’ needs to be evaluated before ‘proj1’ by adding ‘evalutationDepndsOn ‘:proj2’’ to the ‘proj1’ build script.

Thanks for pointing out the direction! I think that explains why I’m getting the difference.

I found [this post] (http://codespelunker.blogspot.co.uk/2013/02/gradle-configuration-vs-execution.html) provides more information to help me to understand the two phases (configuration and execution phases).