# How to invoke methods dynamically on plugin extensions?

**URL:** <https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001>\
**Category:** Old Forum Archive\
**Created:** [April 27, 2012, 8:15am UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001 "2012-04-27T08:15:00Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![david\_koch444](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@david\_koch444](https://discuss.gradle.org/u/david_koch444)\
**Post date:** [April 27, 2012, 8:15am UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/1 "2012-04-27T08:15:00Z")

</div>

I’ve tried to use groovys dynamic method invocation with _invokeMethod(Sting name, args)_ on a plugin extension. The groovy test works fine. But when doing with gradle it always fails with:

```gradle
Could not find method testNg() for arguments ['org.testng', 'testng', '6.1'] on org.plugins.ModuleKitExtension_Decorated@5e8
56a7a.

```

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [April 27, 2012, 9:05am UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/2 "2012-04-27T09:05:00Z")

</div>

Please provide more information (source code, output of ‘gradle -v’).

What language are you using? If it’s Groovy, you can just call the dynamic method directly.

---

<div class="post-metadata">

**Author:** ![david\_koch444](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@david\_koch444](https://discuss.gradle.org/u/david_koch444)\
**Post date:** [April 27, 2012, 12:19pm UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/3 "2012-04-27T12:19:00Z")

</div>

Here is the plugin:

```gradle
class ModuleKitPlugin implements Plugin<Project> {
   @Override
 public void apply(Project project) {
  project.extensions.create("moduleKit", ModuleKitExtension)
 }
   public static void main(String[] args) {
  def kit = new ModuleKitExtension()
  assert kit.testMethod()=="INVOKED: testMethod"
 }
}
  class ModuleKitExtension {
    def invokeMethod(String name, args) {
  "INVOKED: $name"
 }
}

```

the Gradle script:

```gradle
apply plugin: 'module-kit'
println moduleKit.testNg() //expected output "INVOKED: testNg"

```

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [April 27, 2012, 2:15pm UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/4 "2012-04-27T14:15:00Z")

</div>

‘public static void main()’ in a plugin?

---

<div class="post-metadata">

**Author:** ![david\_koch444](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@david\_koch444](https://discuss.gradle.org/u/david_koch444)\
**Post date:** [May 2, 2012, 7:07am UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/5 "2012-05-02T07:07:00Z")

</div>

this main function is just a short cut in order to proof the code works. In Eclipse I can stressless run this main function by mouse clicking,

---

<div class="post-metadata">

**Author:** ![david\_koch444](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@david\_koch444](https://discuss.gradle.org/u/david_koch444)\
**Post date:** [May 2, 2012, 10:19am UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/6 "2012-05-02T10:19:00Z")

</div>

I should maybe explain a bit more what I plan to do. Perhaps there is a much simpler solution. With Maven I made quite good experience with the _dependencyManagement_ feature. Which is for defining an external dependencies version in a central location. So all sub-modules use just the dependency without declaring its version.

With Gradle I currently do it by declaring a global map like this:

```gradle
ext.modules = [
  guiLib: module('org.myorg', 'gui-lib', '1.0'),
  guiLibSources: module(''org.myorg', 'gui-lib', '1.0', 'sources'),
].asImmutable()
def module(String groupId, String artifactId, String version){
  [group: groupId, name: artifactId, version: version].asImmutable()
}
def module(String groupId, String artifactId, String version, String classifier){
 [group:groupId, name:artifactId, version:version, classifier:classifier].asImmutable()
}

```

But then I need to declare always all combinations, with/without classifier, with/without type. And we need it lots of times e.g. for GWT. So my plan is to create a plugin which takes a property file with module declarations like this:

```gradle
guiLib: org.myorg:gui-lib:1.0

```

The classifier and type are specified when using it like this:

```gradle
dependencies{
  compile(
    moduleKit.guiLib, //property provides the [group: 'org.myorg', name: 'guiLib', version: '1.0']
    moduleKit.guiLib('sources') //method provides [group: 'org.myorg', name: 'guiLib', version: '1.0', classifier: 'sources']
  }
}

```

This is what I need the _invokeMethod_ and _getProperty_ feature for.

---

<div class="post-metadata">

**Author:** ![Peter\_Niederwieser](https://avatars.discourse-cdn.com/v4/letter/p/9f8e36/32.png) [@Peter\_Niederwieser](https://discuss.gradle.org/u/Peter_Niederwieser)\
**Post date:** [May 2, 2012, 4:16pm UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/7 "2012-05-02T16:16:00Z")

</div>

What do you hope to gain from externalizing this information? How is it better than ‘ext.modules = [guiLib: “org.myorg:gui-lib:sources:1.0”]’, which works out of the box? Even if you chose to externalize this, what would you need ‘invokeMethod’ and ‘getProperty’ for?

---

<div class="post-metadata">

**Author:** ![david\_koch444](https://avatars.discourse-cdn.com/v4/letter/d/e0b2c6/32.png) [@david\_koch444](https://discuss.gradle.org/u/david_koch444)\
**Post date:** [May 3, 2012, 7:31am UTC](https://discuss.gradle.org/t/how-to-invoke-methods-dynamically-on-plugin-extensions/7001/8 "2012-05-03T07:31:00Z")

</div>

I’ve debugged Gradle and found that the CompositeDynamicObject queries my decorated extension with _hasMethod_ and _hasProperty_ methods in order to determine whether it can invoke the requested method. Do I need to inherit from some specific base class in order to answer this query as I want?

I’ve separated the modules declarations into a _modules.gradle_ because its a noticeable amount of data which in my opinion naturally qualifies for separation. One nice Gradle feature - among lots of others - is that I can structure even build files instead of heaving one quite huge xml file.

The other reason is when defining dependency versions it would be good to declare only _group_, _name_ and _version_ but when using it in subprojects I can be more specific about _classifier_ and _type_. Because the _sources_ and _zip_ secondary artifacts I always need in exactly the same version as the main artifact. What I save is not to declare all secondary artifact variants that I need e.g. _guiLib_, _guiLibSources_, _guiLibResources_ etc. Once I’ve declared just _guiLib_ I can use _guiLib(‘sources’)_, _guiLib(‘resources’)_ or just _guiLib_

Another reason is I could upload this info and use it in dependent projects to share infrastructure interface module versions.
