Pregunta

Say I have a grails-master plugin that itself contains a (chained) "sub-plugin" called grails-widget. The master plugin pulls in the chained plugin like so:

// grails-master's BuildConfig.groovy:
plugins {
    compile ':widget:1.0.5' // or whatever
}

This way, anytime a Grails app includes grails-master as a dependency, both master and chained plugins get pulled in.

Now let's say that the chained grails-widget plugin requires configuration. Let's say that if an app requires grails-widget directly (sans the master), then one must configure the plugin from inside the Grails app's Config.groovy like so:

grails.plugin.widget.fizz=1
grails.plugin.widget.buzz='YES'
... etc.

Here's what I'd like to accomplish, if at all possible:

  • If a Grails app just lists grails-master as a dependency, then the master pulls in grails-widget but provides default values for its fizz and buzz properties. This way, when the app declares grails-master, they do not need to define the above properties in its Config.groovy.
  • Again, if a Grails app just lists grails-master as a dependency, then although grails-master will (somehow, automagically) provide defaults for fizz and buzz, the app developer can still choose to define them in their app's Config.groovy. In this case, the app-defined values for fizz and buzz will override the defaults set back in grails-master.
  • Finally if the developer only lists grails-master as a dep, it would be nice to allow the app developer to disable the use of the (chained) grails-widget plugin altogether. Hence, anything the plugin does at app startup would not occur.

Are these items possible? If so, how could I accomplish them? If not, why?

¿Fue útil?

Solución

  1. Yes you can accomplish that. Default configs can be added when the master plugin is installed. Manually you can do as a post initialization configuration in doWithApplicationContext. But there is a convenient plugin-config plugin which can do that for you. Add this plugin to master plugin and set up default values for the widget plugin. The doc for plugin is self-explanatory. If not, I can add an example.

  2. Any config provided by the plugin can be overridden in application's Config.groovy. Application's config is initialized in the end (after all plugins are initialized). So you can definitely override the default config which you would have setup from step 1 above.

  3. You can exclude widget plugin from the master plugin when required, in application's BuildConfig.

Example:

//BuildConfig in application
plugins {
    compile(':master:0.1') {
        //exclusion based on some logic at build/compile time
        if(some logic satisfied) {
            excludes 'widget'
        }
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top