Unable to run 'gradle model' but 'gradle <task>' runs okay

software-model

(Seoras Ray) #1

When I run ‘gradle model’ in Gradle 2.12 I get the following error:
Execution failed for task ‘:model’.
> Exception thrown while executing model rule: defaults { … } @ build.gradle line 34, column 5
> Cannot set value for model element ‘defaults.value’ as this element is not mutable.

However I can run ‘gradle print_stuff’ without error.

I am trying to add a defaults element to a ModelMap that can be overridden via the DSL or left out entirely. If there is an easier way to do this I would love to know.

class testPlugin extends RuleSource {
  @Model
  void defaults(Thing defaults) {}

  @Model
  void things(ModelMap<Thing> things) {}

  @Defaults
  void setDefaults(Thing defaults) {
    defaults.value = 'Default'
  }

  @Mutate
  void addDefaults(ModelMap<Thing> things, Thing defaults) {
    things.put('defaults', defaults)
  }
}

@Managed
interface Thing extends Named {
  String getValue()
  void setValue(String value)
}

apply plugin: testPlugin

model {

//  defaults {
//    value = 'This works'
//  }

  things {
    defaults {
      value = 'This does not work'
    }

    thing1(Thing) {
      value = 'Thing 1'
    }

    thing2(Thing) {
      value = 'Thing 2'
    }
  }
}

model {
  tasks {
    print_stuff(Task) {
      doLast {
        println "Thing 1 Value: ${$.things.thing1.value}"
        println "Thing 2 Value: ${$.things.thing2.value}"
        println "Defaults Value: ${$.things.defaults.value}"
      }
    }
  }
}

(Seoras Ray) #2

Solved it myself:

class testPlugin extends RuleSource {
  @Model
  void defaults(Thing defaults) {}

  @Model
  void things(ModelMap<Thing> things) {}

  @Mutate
  void addDefaults(ModelMap<Thing> things, Thing defaults) {
    things.create('defaults') {
      value = 'Default'
    }
  }
}

@Managed
interface Thing extends Named {
  String getValue()
  void setValue(String value)
}

apply plugin: testPlugin

model {
  things {
    defaults {
      value = 'This works'
    }

    thing1(Thing) {
      value = 'Thing 1'
    }

    thing2(Thing) {
      value = 'Thing 2'
    }
  }
}

model {
  tasks {
    print_stuff(Task) {
      doLast {
        println "Thing 1 Value: ${$.things.thing1.value}"
        println "Thing 2 Value: ${$.things.thing2.value}"
        println "Defaults Value: ${$.things.defaults.value}"
      }
    }
  }
}