1

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?

Siguza
  • 21,155
  • 6
  • 52
  • 89
IAmYourFaja
  • 55,468
  • 181
  • 466
  • 756
  • This [solution](https://stackoverflow.com/a/40175587/2835520) worked for me, but its a Grails 3 issue. What Grails version are you using? – IgniteCoders Jun 11 '20 at 21:50

3 Answers3

3

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)
}
BenMorel
  • 34,448
  • 50
  • 182
  • 322
injecteer
  • 20,038
  • 4
  • 45
  • 89
  • Mind if we combine all our Answers into @injecteer's answer and delete ours? That way we have one solid answer to the question for future users? – Joshua Moore May 15 '14 at 15:54
2

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
  }
  ...
Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
0

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.

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
  • Mind if we combine all our Answers into @injecteer's answer and delete ours? That way we have one solid answer to the question for future users? – Joshua Moore May 15 '14 at 15:53