Is there a way to get targetCompatiblity in RuleSource/Model space?


(Appu Goundan) #1

I’d like to make a decision in a task based on target compatibility. Is there a way to extract this information out during my RuleSource configuration?

Right now, I’m looking at pulling this information from JavaPluginConvention which I don’t think I can get to via the ExtensionContainer. So I’m passing it from “project” space to “model” space via a custom extension.

would ideally like something like

public void setParameterBasedOnJavaTargetVersion(MyModel model, JavaPluginConvention javaConvention) {

Is something like this possible?

(Mark Vieira) #2

Technically you can get at project convention objects through the extension container via some nasty casting. This is all dependent on some implementation details of how dynamic properties work on the Project object so proceed with caution. No guarantees this will continue to work in future versions of Gradle. While a little hacky, bridging from Project to the model space through a custom extension object is probably a “safer” implementation.

apply plugin: 'java'
apply plugin: Rules

targetCompatibility = '1.6'

class Rules extends RuleSource {
    void addTask(ModelMap<Task> tasks, ExtensionContainer extensions) {
        Convention convention = (Convention) extensions
        tasks.create("printTargetCompatibility") {
            doLast {
                println convention.getPlugin(JavaPluginConvention).targetCompatibility

(Appu Goundan) #3

Gotcha, went with custom extension.