【问题标题】:Load an external properties file in grails在 grails 中加载外部属性文件
【发布时间】:2013-01-28 15:01:26
【问题描述】:

我正在尝试通过设置系统环境从外部加载属性文件。

在我的 config.groovy 文件中,

  println "Config file location --->" + System.getenv("SAM_ENV")
  grails.config.locations = ["file:"+ System.getenv("SAM_ENV")]

我已将系统环境 SAM_ENV 值设置为 C:\test\config.properties。

当我尝试运行应用程序时,我得到的打印值是

     Config file location ---> C:\test\config.properties prints properly.

问题是当我尝试以

身份访问控制器中的属性文件时
        print "PAGINATION1"+grailsApplication.config.PAGINATION1

PAGINATION1 的值未正确打印。

谁能帮助我访问 grails 应用程序中的外部属性文件需要做哪些配置。

【问题讨论】:

    标签: grails properties groovy


    【解决方案1】:
    Add the below line in config.groovy
    
    grails.config.locations = [ "classpath:grails-app-config.properties"]
    
    environments {
        development {   
            grails.logging.jul.usebridge = true
            grails.config.locations = ["file:C:\\conf\\externalfile.groovy"]
        }
        production {
            grails.logging.jul.usebridge = false
            grails.config.locations = ["file:/opt/config/externalfile.groovy"]
            // TODO: grails.serverURL = "http://www.changeme.com"
        }
    }
    
    If you want to access any property from external configuration(config.groovy) then just declare the property like 
    
    property = property value eg:(ImagePath = "C:\\Users\\Saved Pictures")
    
    access it like grailsApplication.config."property"
    
    eg:(grailsApplication.config.ImagePath)
    
    NOTE: dont use def just a property and its value.
    

    【讨论】:

      【解决方案2】:

      您正在寻找的是扩展类路径,您可以通过在 _Events.groovy 中添加编译后事件来实现。试试这个:

      eventCompileEnd = {
      ant.copy(todir:classesDirPath) {
           fileset(file:"C:\test\config.properties")
      }}
      

      您可以找到更多帮助here

      【讨论】:

        猜你喜欢
        • 2011-08-27
        • 1970-01-01
        • 1970-01-01
        • 2014-12-30
        • 1970-01-01
        • 2012-03-18
        • 2014-01-09
        • 2016-06-18
        • 1970-01-01
        相关资源
        最近更新 更多