Grails - Plugin Dependencies

Plugin Dependencies

Top: Plugin Dev Guide

Understanding Plugin Dependencies and Load order

Plugins often depend on the presence of other plugins and can also adapt depending on the presence of others. To cover this, a plugin can define two properties. The first is called dependsOn . For example, take a look at this snippet from the Grails Hibernate plugin:

class HibernateGrailsPlugin {

def version = 1.0 def dependsOn = [dataSource:1.0, domainClass:1.0, i18n:1.0, core: 1.0]

}

As the above example demonstrates the Hibernate plugin is dependent on the presence of 4 plugins: The dataSource plugin (DataSourceGrailsPlugin), The domainClass plugin (DomainClassGrailsPlugin), the i18n plugin and the core plugin.

Essentially the dependencies will be loaded first and then the Hibernate plugin. If all dependencies do not load, then the plugin will not load.

However, this is a "hard" dependency in that if the dependency is not resolved, the plugin will give up and won't load. It is possible though to have a "weaker" dependency using the loadAfter property:

def loadAfter = ['controllers']
Here the plugin will be loaded after the "controllers" plugin if it exists, otherwise it will just be loaded. The plugin can then adapt to the presence of the other plugin, for example the Hibernate plugin has this code in the doWithSpring closure:
if(manager?.hasGrailsPlugin("controllers")) {
	openSessionInViewInterceptor(OpenSessionInViewInterceptor) {
        	flushMode = HibernateAccessor.FLUSH_AUTO
	        sessionFactory = sessionFactory
	}
        grailsUrlHandlerMapping.interceptors << openSessionInViewInterceptor
  }
Here the Hibernate plugin will only register an OpenSessionInViewInterceptor if the "controllers" plugin has been loaded.

Interacting with the GrailsPluginManager

The previous snippet presented an example of how to interact with the PluginManager. The manager variable is an instance of the GrailsPluginManager interface and it provides methods to interact with other plugins and the PluginManager itself from any plugin.

Top: Plugin Dev Guide