Can Gradle plugin add static import or method body to the applying script?

plugins

(Crazyjavahacking) #1

Hi,

I have implement the following functionality:

main(NativeExecutableSpec) {
    sources {
        c {
            lib externalNativeDependency(group: '...', name: '...', version:'...')
        }
    }
}

Right now the externalNativeDependency is a local method defined in the Gradle script:

def apolloExternalNativeDependency(Map<String, ?> args) { ... }

Now I want to extract the functionality in a separate Gradle plugin. I was hoping to achieve the same syntax, so I need either:

  • add static import to the applying script, so that the apolloExternalNativeDependency would be resolved as method call
  • or add method body itself to the applying Gradle script

Does it make sense for you? Would that be somehow possible? If no, what would you propose?

Thanks,
Martin


(Crazyjavahacking) #2

So I figure out how to do it.

I created a plugin that modifies the extension container by adding a closure. CLosure takes one argument (Map) and will be lazily evaluated. In that case it looks like first class method.

apply plugin: MyPlugin

main(NativeExecutableSpec) {
    sources {
        c {
            lib externalNativeDependency(group: '...', name: '...', version:'...')
        }
    }
}

class MyPlugin implements Plugin<Project> {

...
    private void addExternalNativeDependencyExtClosure(Project project) {
        project.ext.externalNativeDependency = { args ->
            ...
        }
    }
}