IAmYourFaja
IAmYourFaja

Reputation: 56934

Why is Grails plugin's BootStrap not executing?

I created a Grails plugin (grails create-plugin myplugin1) and noticed that there was no myapp1/grails-app/conf/BootStrap.groovy created like you normally get when you create a Grails app.

I created one like so:

class BootStrap {
    def init = {
        println("Hello! The plugin is bootstrapping...")
    }

    def destroy = {
    }
}

I then include the plugin with a Grails app (by adding myplugin1 as a plugin inside the app's BuildConfig.groovy). When I issue a grails run-app, I don't see the above println ever executing.

Do Grails plugins not use BootStrap.groovy? If so, where should I put "bootstrap" code that needs to be executed when a plugin is loaded? Otherwise, if I was correct to do this above, why might I not be seeing the "Hello! The plugin is bootstrapping..." message print out?

Upvotes: 1

Views: 1885

Answers (3)

injecteer
injecteer

Reputation: 20709

Plugin's BootStrap is EXCLUDED from the plugin package. You have to do your init-phase in the plugin descriptor, in one or several of the following closures:

def doWithSpring = {
  def appName = application.metadata.'app.name'
}

def doWithDynamicMethods = { ctx ->
    // TODO Implement registering dynamic methods to classes (optional)
}

def doWithApplicationContext = { applicationContext ->
    // TODO Implement post initialization spring config (optional)
}

Upvotes: 3

Joshua Moore
Joshua Moore

Reputation: 24776

As always, start with the very well written and maintained documentation.

Plugins do not include Bootstrap.groovy. The following are excluded from plugins (taken from documentation).

  • grails-app/conf/BootStrap.groovy
  • grails-app/conf/BuildConfig.groovy (although it is used to generate dependencies.groovy)
  • grails-app/conf/Config.groovy
  • grails-app/conf/DataSource.groovy (and any other *DataSource.groovy)
  • grails-app/conf/UrlMappings.groovy
  • grails-app/conf/spring/resources.groovy
  • Everything within /web-app/WEB-INF
  • Everything within /web-app/plugins/**
  • Everything within /test/**
  • SCM management files within /.svn/ and /CVS/

In order to run code on startup of your plugin you need to hook into the runtime configuration of your plugin by using the doWithSpring or doWithApplicationContext (depending on what you need to do).

All of this, and more, is explained in the documentation. An example might be:

// MyFancyPlugin.groovy
  ...
  def doWithApplicationContext = { appCtx ->
      def sessionFactory = appCtx.sessionFactory
      // do something here with session factory
  }
  ...

Upvotes: 2

Ian Roberts
Ian Roberts

Reputation: 122394

Code that needs to run at startup time should go in the doWithApplicationContext closure in the plugin descriptor (MyPlugin1GrailsPlugin.groovy).

Alternatively, call it something else (e.g. MyPluginBootStrap.groovy), as it's only the specific classes BootStrap and UrlMappings that are excluded when a plugin is packaged, but any class whose name ends BootStrap is considered a bootstrap artefact.

Upvotes: 0

Related Questions