2014-02-15 101 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, 
] 

這樣,當應用程序分叉時,您的系統屬性不會丟失。