How to set a property on a subproject


(Hoobajoob) #1

Not sure I understand whether/how I can set a property on a subproject from the root project prior to task execution. When I try to run the doSomething task below on the root project, I get the error below. What am I missing?

FAILURE: Build failed with an exception.

* Where:
Build file 'C:\Users\hoobajoob\project-x\build.gradle' line: 3

* What went wrong:
Execution failed for task ':doSomething'.
> No such property: destination for class: org.gradle.api.internal.project.DefaultProject_Decorated

Here are the corresponding build scripts:

root/build.gradle

task doSomething {
   doFirst {
      project(':subprojectA').someProperty = 'something'
   }
}

root/settings.gradle

include 'subprojectA'

root/subprojectA/build.gradle

ext.someProperty = null
task doSomething {
   doLast {
      if ( someProperty == null ) {
         throw new IllegalStateException("Must set someProperty")
      }
   }
}

(Mark Vieira) #2

No such property: destination

The error mentions a property “destination” but you example lists “someProperty”. I expect the snippet your are posting is just an example? From that I can tell this should work.


(Hoobajoob) #3

Drat - I did paste these snippets together. Let me reproduce it using these snippets and update.


(Hoobajoob) #4

Ok - using these snippets as-is, I don’t have a problem resolving the property name.

Turns out the project from which I created this snippet was using a different build script name for the subproject, and I had two statements out of order in settings.gradle, as below:

rootProject.children.each { project -> project.buildFileName = "test.gradle" }
include 'subprojectA'

Everything’s ok now. Thanks!


(Hoobajoob) #5

@mark_vieira: A followup question: is there a way to set a subproject property from the root project during the configuration phase? I can’t seem to simply move the property set in the root project outside the task definition.

@mark_vieira: cancel that followup - just discovered the afterEvaluate property, and this does what I need.