# How to access Model from a Plugin

**URL:** <https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047>\
**Category:** Help/Discuss\
**Created:** [January 30, 2016, 6:04pm UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047 "2016-01-30T18:04:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![amilenovic](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@amilenovic](https://discuss.gradle.org/u/amilenovic)\
**Post date:** [January 30, 2016, 6:04pm UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/1 "2016-01-30T18:04:56Z")

</div>

Hi,

I am new to Gradle and have just written a Plugin. I also have read about the Model concept, which I really like. I wanted to access Model information from a Plugin.

I tried to get it from Project since my plugin takes Project as input (Plugin). The closest model related property is Model Schema Store. However this looks to be from ‘internal’ package.

Is it possible to access Model from a Plugin? How I can do so? Apologies if I am missing the obvious.

Following topic ([https://discuss.gradle.org/t/how-would-you-create-the-model-component-closure-in-a-custom-plugin-since-the-2-3-native-component-changes/4247](https://discuss.gradle.org/t/how-would-you-create-the-model-component-closure-in-a-custom-plugin-since-the-2-3-native-component-changes/4247)) looks related.

Thanks,  
Aleksandar

---

<div class="post-metadata">

**Author:** ![axl](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/axl/32/834_2.png) [@axl](https://discuss.gradle.org/u/axl)\
**Post date:** [February 1, 2016, 8:30am UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/2 "2016-02-01T08:30:07Z")

</div>

I suggest you start looking at the documentation and samples here: [https://docs.gradle.org/current/userguide/software\_model.html](https://docs.gradle.org/current/userguide/software_model.html), and the sample code in those sections

As far as I’ve seen, you don’t really need to access the model from a plugin directly, you just provide rules that take model elements as inputs, and it’s all automagic from there.

---

<div class="post-metadata">

**Author:** ![pledbrook](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/pledbrook/32/1101_2.png) [@pledbrook](https://discuss.gradle.org/u/pledbrook)\
**Post date:** [February 1, 2016, 2:08pm UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/3 "2016-02-01T14:08:38Z")

</div>

Following up on what Andreas said, the new software model is a parallel configuration mechanism to the old plugin approach. They’re not mutually exclusive, but there are only limited ways to link between them.

To use the new model, create one or more classes that implement `RuleSource`. I tried to provide an introduction to the new structure in a presentation. If you have a spare hour, [it may help](https://skillsmatter.com/skillscasts/6998-gradle-s-new-model).

If you want to have your `RuleSource` processed by Gradle automatically when a user applies your plugin, then you can simply make that `RuleSource` a static inner class of the plugin, [as demonstrated here](https://github.com/pledbrook/asciidoctor-gradle-plugin/blob/patch-01/src/main/groovy/org/asciidoctor/gradle/AsciidoctorPlugin.groovy#L21).

---

<div class="post-metadata">

**Author:** ![amilenovic](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@amilenovic](https://discuss.gradle.org/u/amilenovic)\
**Post date:** [February 1, 2016, 7:20pm UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/4 "2016-02-01T19:20:35Z")

</div>

Thanks for that.

Maybe I wasn’t clear enough with what I mean by ‘accessing the model’.

I am working on a PlantUml plugin, which can generate diagrams like Component or Use Case. I would like to be able to read the model (builtin entities like Components but also my own entities like UseCase) and then to generate a diagram.

In essence, I need same information as you can get with ‘gradle model’ command, but programmatically with my own Plugin.

I couldn’t understand how I can do that via RuleSets…

How I can achieve this?

Thanks in advance,  
Aleksandar

---

<div class="post-metadata">

**Author:** ![Adrian\_Kelly](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/adrian_kelly/32/106_2.png) [@Adrian\_Kelly](https://discuss.gradle.org/u/Adrian_Kelly)\
**Post date:** [February 2, 2016, 1:50am UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/5 "2016-02-02T01:50:59Z")

</div>

@amilenovic there is no public API available to inspect the entire model graph. The [model task](https://github.com/gradle/gradle/blob/0c1de84ffccfe7c8d48990d917f90bc5f18bbc88/subprojects/diagnostics/src/main/groovy/org/gradle/api/reporting/model/ModelReport.java#L39-L39) uses the internal class `org.gradle.model.internal.registry.ModelRegistry` to cycle through the nodes of the model. Classes like `ModelRegistry` are under active development and are intentionally not made public. It is not recommended for external plugins or builds to rely on and use such classes so use at your own peril!

---

<div class="post-metadata">

**Author:** ![pledbrook](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/pledbrook/32/1101_2.png) [@pledbrook](https://discuss.gradle.org/u/pledbrook)\
**Post date:** [February 2, 2016, 8:54am UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/6 "2016-02-02T08:54:07Z")

</div>

Rules take the form:

```
void ruleName(<subject>, <input>, ...)

```

The return type and significance of the rule name both depend on the type of rule, but for now I want to focus on `<input>, ...`. These inputs represent other elements of the model, provided by your own plugin, core Gradle, or some other plugin. Gradle automatically injects the appropriate object of the required type into your rule method. You can then extract the information you need and do something with it.

For example, if you want to create some tasks based on the configured use case, you can do this:

```
@Mutate
void createUseCaseTasks(ModelMap<Task> tasks, UseCase useCase) {
    tasks.create("useCaseTask") {
        // Configure the task using information from `useCase`
        ...
    }
}

```

What if you have more than one `UseCase` in your model? In that case, you either need to use a collection of them (via `ModelMap` for example) or specify a specific one based on its model path (via the `@Path` annotation).

If not much of this is making sense, then you’ll need to learn more about the new software model. I used the [videos of the relevant workshop from the Gradle Summit](https://gradle.org/rule-based-model-configuration-workshop-part-1/). That’s a link to part 1. Just change the 1 for a 2 in the URL to get the second part, although I feel all the important stuff is in the first one.

---

<div class="post-metadata">

**Author:** ![amilenovic](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@amilenovic](https://discuss.gradle.org/u/amilenovic)\
**Post date:** [February 2, 2016, 6:34pm UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/7 "2016-02-02T18:34:25Z")

</div>

Got it, thanks @Adrian_Kelly. I hope you will eventually make such API public - I see it as an excellent base for ecosystem of tooling on top of Gradle.

Regards,  
Aleksandar

---

<div class="post-metadata">

**Author:** ![amilenovic](https://avatars.discourse-cdn.com/v4/letter/a/7cd45c/32.png) [@amilenovic](https://discuss.gradle.org/u/amilenovic)\
**Post date:** [February 2, 2016, 6:41pm UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/8 "2016-02-02T18:41:41Z")

</div>

Thanks @pledbrook, I will definitively try it out.

---

<div class="post-metadata">

**Author:** ![remmeier](https://avatars.discourse-cdn.com/v4/letter/r/779978/32.png) [@remmeier](https://discuss.gradle.org/u/remmeier)\
**Post date:** [May 3, 2016, 3:35pm UTC](https://discuss.gradle.org/t/how-to-access-model-from-a-plugin/14047/9 "2016-05-03T15:35:28Z")

</div>

@Mutate  
void createUseCaseTasks(ModelMap tasks, UseCase useCase)

creates a tasks by first evaluating “UseCase”. is there a way to achieve this the other way around? Like

@TaskAction  
public void doSomething(UseCase useCase)

Meaning that I would like to evaluate the model if a given task is executed (because the task needs this part of the model, but not other parts of the model that are potentially expensive to evaluate)? The given example results in a gradle error.
