2014-02-15 47 views
0

我开始与现有的Grails项目工作,这是一个与外部配置文件涉及的配置文件(Config.groovy中)的部分:Grails的外部配置文件

grails.config.locations = []; 

def defaultConfigFile = "${basedir}/configs/BombayConfig.groovy" 
def defaultDatasourceFile = "${basedir}/configs/BombayDataSource.groovy" 

if(File.separator == "\\") { 
    defaultConfigFile = defaultConfigFile.replace('/', '\\') 
    defaultDatasourceFile = defaultDatasourceFile.replace('/', '\\') 
} 

String PROJECT_NAME = "BOMBAY" 
String CONFIG_ENV = "${PROJECT_NAME}_CONFIG_LOCATION" 
String DATASOURCE_ENV = "${PROJECT_NAME}_DATASOURCE_LOCATION" 

def externalConfig = System.getenv(CONFIG_ENV) 
def externalDataSource = System.getenv(DATASOURCE_ENV) 

if (externalConfig && new File(externalConfig).isFile()) { 
    grails.config.locations << "file:" + externalConfig 
} 
else { 
    grails.config.locations << "file:" + defaultConfigFile 
} 

if (externalDataSource && new File(externalDataSource).isFile()) { 
    grails.config.locations << "file:" + externalDataSource 
} 
else { 
    grails.config.locations << "file:" + defaultDatasourceFile 
} 

我在CONFIGS一些默认的文件文件夹,但是在服务器正在使用的文件驻留在:

/home/someusername/configuration/ 

它看起来并不像默认路径,并且没有指向该路径作为配置建议的环境变量。

我也试着寻找一个链接到其他配置文件夹的configs文件夹,但没有找到任何东西。

我迷失在这里;如何将配置文件指定给Grails?

编辑: 为了澄清事情,服务器正在运行并运行,我只想弄清楚它是如何从上述指定路径中获取配置文件的。

+2

您确定没有将这些环境变量传递给server_(Tomcat?)上的相关进程,即使它们没有设置在您检查的任何shell中。通常当我需要设置这样的东西时,我将环境设置放入Tomcat启动脚本中。 –

+0

这就是我想念伊恩,他们被设置在剧本中。谢谢! –

回答

1

我认为目的是使用grails.config.locations []条目。以下是我在Config.groovy中注释掉

// grails.config.locations = [ "classpath:${appName}-config.properties",  
//        "classpath:${appName}-config.groovy", 
//        "file:${userHome}/.grails/${appName}-config.properties", 
//        "file:${userHome}/.grails/${appName}-config.groovy"] 

所以你不能指定这样的东西:

grails.config.locations = [ "file:${userHome}/configuration" ] 

皮卡该文件夹中的文件?

+0

对,但当前配置文件处理它的方式不是这样。看到我更新的帖子。 –

1
if(File.separator == "\\") { 
    defaultConfigFile = defaultConfigFile.replace('/', '\\') 
    defaultDatasourceFile = defaultDatasourceFile.replace('/', '\\') 
} 

这可能会导致问题 - grails.config.locations网址,而不是本机文件路径列表,所以你绝对要向前看,而不是向后斜杠。相反,尝试类似

File defaultConfigFile = new File(basedir, "configs/BombayConfig.groovy") 
// and the same for datasource 

// ... 
File externalConfig = System.getenv(CONFIG_ENV) ? new File(System.getenv(CONFIG_ENV)) : null 

if (externalConfig?.isFile()) { 
    grails.config.locations << externalConfig.toURI().toString() 
} 
else { 
    grails.config.locations << defaultConfigFile.toURI().toString() 
} 
1

如果你正在用外部CONFIGS挣扎,你 有规定外CONFIGS像这样Config.groovy中

grails.config.locations = [] 

if(System.properties["${appName}.config.location"]) { 
    grails.config.locations << "file:" + System.properties["${appName}.config.location"] 
} 

if(System.properties["${appName}.datasource.config.location"]) { 
    grails.config.locations << "file:" + System.properties["${appName}.datasource.config.location"] 
} 

和您所设定的环境是这样的

export GRAILS_OPTS="$GRAILS_OPTS -DAppName.config.location=dev-config/Config.groovy" 
export GRAILS_OPTS="$GRAILS_OPTS -DAppName.datasource.config.location=dev-config/DataSource.groovy" 

和开发配置没有被拿起,那么你需要看看你的叉子设置在BuildConfig.groovy并关闭他们喜欢所以

grails.project.fork = [ 
    test: false, 
    run: false, 
] 

这样,当应用程序分叉时,您的系统属性不会丢失。