我会假设你的属性文件的高达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