我會假設你的屬性文件的高達95%名稱=值雙是在所有部署環境相同的製造和名稱=值中的5%會從一個部署環境更改爲另一個部署環境。
如果這個假設是正確的,那麼你可以嘗試像下面的僞代碼。
void generateRuntimeConfigFiles(int deploymentMode)
{
String[] searchAndReplacePairs;
if (deploymentMode == Constants.PRODUCTION) {
searchAndReplacePairs = ...
} else if (deploymentMode == Constants.STAGING) {
searchAndReplacePairs = ...
} else { // Constants.DEVELOPMENT
searchAndReplacePairs = ...
}
String[] filePairs = new String[] {
"log4j-template.properties", "log4j.properties",
"jdbc-template.properties", "jdbc.properties",
"foo-template.xml", "foo.xml",
...
};
for (int i = 0; i < filePairs.length; i += 2) {
String inFile = filePairs[i + 0];
String ouFile = filePairs[i + 1];
searchAndReplaceInFile(inFile, outFile,
searchAndReplacePairs);
}
}
你的應用程序調用generateRuntimeConfigFiles()
之前初始化其他任何可能依靠性能/ XML文件。
現在你要處理的唯一問題是如何存儲和檢索searchAndReplacePairs
不同的設置。也許你可以從名稱如production.properties
,staging.properties
和development.properties
的文件中獲得它們。
如果上面的方法吸引你,然後給我發電子郵件的searchAndReplaceInFile()
的源代碼,以節省您不必重新發明輪子。您可以在我的Stackoverflow配置文件的「信息」框中找到我的電子郵件地址。
我肯定會想到這一點,我認爲這是一個很好的解決方案。我唯一的問題是,現在我沒有使用構建工具,只是Eclipse(壞,我知道)。 – 2011-03-31 23:21:43