Allan Lewis wrote:
I have a simple test project that has 2 modules: 'subA' and 'subB'. I'd like
to add a task 'generateSources' that will run for each project before the
'resources' task. Here's what I have in my main build.gradle:
allprojects {
usePlugin('groovy')
sourceCompatibility = 1.5
targetCompatibility = 1.5
group = 'com.irunninglog'
version = '1.0'
}
subprojects {
task('resources').dependsOn('generateSources')
createTask('generateSources') {
logger.error('My name is: ' + project.name);
}
}
Interesting. When a task action closure executes, we set its (groovy)
resolve strategy to OWNER_FIRST. In your example, the owner is the
subprojects { } closure. This closure has its resolve strategy set to
DELEGATE_FIRST, and the delegate will be set to the last project
configured, ie project 'subB'. What this means is that whenever the task
action closure executes, 'project.name' will be resolved against project
'subB', regardless of which project the executing task is actually in.
We should probably be using DELEGATE_FIRST as the resolve strategy for
task actions. Until we fix this, you can use something like:
subprojects {
...
createTask('generateSources') {
logger.error('My name is: ' + *delegate*.project.name)
}
}
And the following in settings.gradle: include 'subA', 'subB'
I expected that if I ran this, I'd see different output for each subproject
- instead, I see:
C:\Temp>gradle clean resources
:clean
:subA:clean
:subB:clean
:init
:resources
:subA:generateSources
My name is: subB
:subA:init
:subA:resources
:subB:generateSources
My name is: subB
:subB:init
:subB:resources
Why does the first call to generateSources think that the project is subB?