Grails - Understanding Plugin Load Orders



Description

Usually, plugins depend on other plugins and can they adapt depending on the presence of others. There are two ways to implement this, the first way is dependsOn. The below code taken from the Hibernate plugin will demonstrate this:

class HibernateGrailsPlugin {

    def version = "1.0"

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

The above snipet shows that the Hibernate plugin is dependent on the dataSource, domainClass, i18n and core plugins. The Hibernate plugin will not load, if all dependencies are not loaded. The dependsOn property supports version ranges, so that user can mention version of particular plugin, the syntax is shown below:

def dependsOn = [pluginname: "* > 1.0"]
def dependsOn = [pluginname: "1.0 > 1.1"]
def dependsOn = [pluginname: "1.0 > *"]

The disadvantage of dependsOn property is, if any one of the dependent plugin is not loaded the plugin will give up and won’t load. To avoid this you can use loadAfter and loadBefore properties as shown below:

def loadAfter = ['controllers']

Similarly, you can use the loadBefore property to load you plugin before specifed plugins as shown below:

def loadBefore = ['rabbitmq']

The user can select the environments in which the plugin should be loaded and also stages of the build. The user can achieve this by declaring one or both of these properties in the plugin descriptor as shown in below syntax:

def environments = ['development', 'test']
def scopes = [excludes:'war']

From the above syntax, it is clear that the plugin will be loaded in development and test environments, and it will be excluded from the war phase.

Below is the list of available scopes:

  • test 

    It is used in/when running tests.

  • functional-test 

    It is used when running functional tests.

  • run  

    It is used for run-app and run-war.

  • war 

    It is used when packaging the application as a WAR file.

  • all 

    It is used for plugin applied to all scopes (default).

The value of environments and scopes properties can be a string, a list or a map.

Advertisements