z00bs
z00bs

Reputation: 7498

Access settings in BuildConfig

Is it somehow possible to access the properties specified in Config.groovy inside the BuildConfig.groovy?

I need to copy some bootstrap files during the packaging process and the target directory depends on the specified environment. Since I need to access those file during application bootstrap I'd like to define the path in the Config.groovy and rather not duplicate it.

Upvotes: 3

Views: 3454

Answers (4)

Maricel
Maricel

Reputation: 2089

If when you say the packaging process you mean when generating the WAR file then I've been able to copy files to different places using Grails scripts/events. For example, I needed to copy one file to the WEB-INF/classes folder when generating the WAR so I created an Events.groovy file under the /scripts folder with the following content:

// Copy liquibase changelog.xml to classpath folder
eventWarStart = {warName ->
  if (grailsEnv == "production") {
    println "Copying database migration files to classpath!"
    Ant.copy(toFile: "${classesDirPath}/changelog.xml", filtering: true, overwrite: true) {
      fileset(file: "${basedir}/grails-app/migrations/changelog.xml")
    }
    Ant.copy(toDir: "${classesDirPath}/releases/", filtering: true, overwrite: true) {
      fileset(dir: "${basedir}/grails-app/migrations/releases/")
    }
  }
}

As you can see in the event you can have access to the environment.

If this is not what you meant, then just ignore my answer.

Upvotes: 3

z00bs
z00bs

Reputation: 7498

The only solution I've found was reading the Config.groovy by myself (as mentioned by Don). Here my two line solution:

def directory = new File(getClass().protectionDomain.codeSource.location.path).parent
def config = new ConfigSlurper(grailsSettings.grailsEnv).parse(new File(directory + File.separator + "Config.groovy").toURI().toURL())

println config.bootstrapPath

Upvotes: 1

Dónal
Dónal

Reputation: 187499

The usual way to access a property such as bootstrap.path defined in Config.groovy is

def bootStrapPath = org.codehaus.groovy.grails.commons.ConfigurationHolder.config.bootstrap.path 

I've haven't tried this from BuildConfig.groovy, but if it doesn't work, then I guess it's because Config.groovy hasn't been read when BuildConfig.groovy is executed. In that case, you'll need to read it yourself. The implementation of ConfigurationHolder should show you how to do this.

Upvotes: 1

Rob Elsner
Rob Elsner

Reputation: 821

You can do this:

System.getProperty('grails.env')

to retrieve the current environment.

Upvotes: 1

Related Questions