# \[rule based model configuration \] project properties

**URL:** <https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699>\
**Category:** Help/Discuss\
**Created:** [May 19, 2015, 7:22pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699 "2015-05-19T19:22:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 19, 2015, 7:22pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/1 "2015-05-19T19:22:56Z")

</div>

Hi  
I’d like to know if it is planned to add a construct to use project properties as rule inputs, e.g.  
@Mutate  
Void foo(rule subject, Map projectProps){  
If(projectProps.contains(‘aProp’)) {  
//do some stuff on the subject  
}  
}

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [May 20, 2015, 8:26am UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/2 "2015-05-20T08:26:07Z")

</div>

Not at the moment. An alternative would be to define a `@Model` and use the model DSL to assign it’s value to that of a project property then declare the model as an input to your rule.

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [May 20, 2015, 2:07pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/3 "2015-05-20T14:07:41Z")

</div>

Hey Mark  
Don’t you think it would be a valid thing to add in a future release?  
(having project properties as rule inputs and maybe subjects)

---

<div class="post-metadata">

**Author:** ![Francois\_Guillot](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/francois_guillot/32/8566_2.png) [@Francois\_Guillot](https://discuss.gradle.org/u/Francois_Guillot)\
**Post date:** [October 20, 2015, 3:11pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/4 "2015-10-20T15:11:33Z")

</div>

@mark_vieira As of today, I don’t think that anything has changed regarding this behavior. Can you confirm ?

> [@mark\_vieira](#):
>
> An alternative would be to define a @Model and use the model DSL to assign it’s value to that of a project property then declare the model as an input to your rule.

This is quite OK, but from inside a plugin, how can I do it ?

---

<div class="post-metadata">

**Author:** ![mark\_vieira](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/mark_vieira/32/5396_2.png) [@mark\_vieira](https://discuss.gradle.org/u/mark_vieira)\
**Post date:** [October 20, 2015, 3:47pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/5 "2015-10-20T15:47:27Z")

</div>

> [@Francois\_Guillot](#):
>
> @mark_vieira As of today, I don’t think that anything has changed regarding this behavior. Can you confirm ?

No, nothing new on this front although this is a known limitation. In general we want a lot more types of things to be available as rule inputs. Right now its mostly limited to other stuff that is part of the model.

> [@Francois\_Guillot](#):
>
> This is quite OK, but from inside a plugin, how can I do it ?

This cannot be done from within a rule. As stated, right now you are limited to using other model elements as rule inputs. If a user wants the convenience of specifying part of the model via a project property they’ll have to leverage the model DSL for that purpose (which has its own limitations).

```
model {
    myModelElement {
        myElementProperty = project.property('foo')
    }
}

```

---

<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:** [January 28, 2016, 3:43pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/6 "2016-01-28T15:43:45Z")

</div>

What about builtin properties, such as the project directory? How on earth is a RuleSource-based plugin expected to provide default values for `File` model elements if it doesn’t have access to the project directory? I know that you can add a `File` rule input with the `@Path("buildDir")` annotation, so it seems strange that other properties aren’t available.

How does the Java plugin do this for the default _main_ and _test_ source sets? The conventions for those are relative to the project directory.

---

<div class="post-metadata">

**Author:** ![lptr](https://sea1.discourse-cdn.com/gradle/user_avatar/discuss.gradle.org/lptr/32/636_2.png) [@lptr](https://discuss.gradle.org/u/lptr)\
**Post date:** [January 28, 2016, 5:47pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/7 "2016-01-28T17:47:42Z")

</div>

> What about builtin properties, such as the project directory? How on earth is a RuleSource-based plugin expected to provide default values for File model elements if it doesn’t have access to the project directory? I know that you can add a File rule input with the @Path(“buildDir”) annotation, so it seems strange that other properties aren’t available.

As of now you can access the project directory by using `ProjectIdentifier` as a rule input and calling `getProjectDir()`. This is how [the core plugins access it](https://github.com/gradle/gradle/blob/4284745c48f3ea5f359dd20a1b86ca101ca00efc/subprojects/platform-base/src/main/java/org/gradle/language/base/plugins/ComponentModelBasePlugin.java#L202-L202) as well.

---

<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:** [January 29, 2016, 8:31am UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/8 "2016-01-29T08:31:50Z")

</div>

That’s great news, thanks. Do you know which version of Gradle that was added in? I don’t remember seeing it in the docs or release notes.

---

<div class="post-metadata">

**Author:** ![Matthieu\_Leclercq](https://avatars.discourse-cdn.com/v4/letter/m/d2c977/32.png) [@Matthieu\_Leclercq](https://discuss.gradle.org/u/Matthieu_Leclercq)\
**Post date:** [March 24, 2016, 8:43pm UTC](https://discuss.gradle.org/t/rule-based-model-configuration-project-properties/9699/9 "2016-03-24T20:43:11Z")

</div>

I found a workaround to use project’s properties in a rule but it relies on internal APIs.

As of gradle 2.12 here is what it looks like:

```gradle
// in the Plugin.apply(ProjectInternal) method implementation
project.getModelRegistry().configure(ModelActionRole.Defaults,
        new AbstractModelActionWithView<YourType>(
                ModelReference.of("your model element name", YourType.class),
                new SimpleModelRuleDescriptor("a big workaround ;)"),
                Collections.<ModelReference<?>> emptyList()) {
            @Override
            protected void execute(final MutableModelNode modelNode,
                    final YourType view,
                    final List<ModelView<?>> inputs) {
                // do what you need with the project's properties here.
            }
        });

```
