Johnnie
Johnnie

Reputation: 305

Reloading Grails Bootstrap with environments

There is a good answer for reloading the Grails Bootstrap in Reloading bootstrap with Grails

But I have environments defined in my init closure and so I get the error:

groovy.lang.MissingMethodException: No signature of method: BootStrap.environments() is applicable for argument types: (BootStrap$_closure1_closure3) values: [BootStrap$_closure1_closure3@19ad0326]

Bootstrap code is basically the spring-security domain classes for role, user, user_role

import org.mine.*

class BootStrap
{
    def init =
    { servletContext ->
        environments
          {
            development
            {
            def adminRole = new DummyRole(authority: 'ROLE_ADMIN').save(flush: true)
            def userRole = new DummyRole(authority: 'ROLE_USER').save(flush: true)

            def user = new DummyUser(username: 'user1', email_address: '[email protected]', enabled: true, password: 'password')
            def user1 = new DummyUser(username: 'user2', email_address: '[email protected]', enabled: true, password: 'password')
            def user2 = new DummyUser(username: 'user3', email_address: '[email protected]', enabled: true, password: 'password')

            user.save(flush: true)
            user1.save(flush: true)
            user2.save(flush: true)

            DummyUserDummyRole.create manager, adminRole, true
            DummyUserDummyRole.create user, userRole, true
            DummyUserDummyRole.create user1, userRole, true
            DummyUserDummyRole.create user2, userRole, true

            assert DummyUser.count() >= 9
            assert DummyRole.count() >= 10
            assert DummyUserDummyRole.count() >= 9


        }   // end-development
        test {

            // bootstrap data for test environment

        }
        production {

            // bootstrap data for production environment

        }
    }
}

    def destroy =
    {
            // code here
    }
}

Upvotes: 2

Views: 1974

Answers (3)

wholenewstrain
wholenewstrain

Reputation: 319

It is much easier to just swap your syntax to:

Environment.executeForCurrentEnvironment {
    production {
        // do something in production
    }
    development {
        // do something only in development
    }
}

Upvotes: 0

Arjang
Arjang

Reputation: 729

I alway used this solution and it worked for me. Works with Grails 3.x too. Here

Upvotes: 0

Bryce
Bryce

Reputation: 401

This works for me:

def servletCtx = org.codehaus.groovy.grails.web.context.ServletContextHolder.servletContext
def myBootstrapArtefact = grailsApplication.getArtefacts('Bootstrap')[-1]
BootStrap.metaClass.environments = grails.util.Environment.&executeForCurrentEnvironment
myBootstrapArtefact.referenceInstance.init(servletCtx)

Lovingly copied (and then modified) from the answer you referenced :-)

Upvotes: 4

Related Questions