Access settings in BuildConfig

2019-05-14 06:07发布

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.

4条回答
老娘就宠你
2楼-- · 2019-05-14 06:35

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
查看更多
The star\"
3楼-- · 2019-05-14 06:42

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.

查看更多
ら.Afraid
4楼-- · 2019-05-14 06:42

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.

查看更多
Melony?
5楼-- · 2019-05-14 06:43

You can do this:

System.getProperty('grails.env')

to retrieve the current environment.

查看更多
登录 后发表回答