以下是从scripts/_Install.groovy
编辑配置文件的示例。
我的插件将三个文件复制到目标目录。
.hgignore
用于版本控制,
DataSource.groovy
替换默认版本,
SecurityConfig.groovy
包含额外的设置。
我宁愿编辑应用程序的文件尽可能少的,尤其是因为我希望改变安全设置了几年的道路。我还需要使用为我们的系统中的每个应用程序服务器定制的jcc-server-config.properties
文件中的属性。
复制文件很容易。
println ('* copying .hgignore ')
ant.copy(file: "${pluginBasedir}/src/samples/.hgignore",
todir: "${basedir}")
println ('* copying SecurityConfig.groovy')
ant.copy(file: "${pluginBasedir}/src/samples/SecurityConfig.groovy",
todir: "${basedir}/grails-app/conf")
println ('* copying DataSource.groovy')
ant.copy(file: "${pluginBasedir}/src/samples/DataSource.groovy",
todir: "${basedir}/grails-app/conf")
难的是让Grails的拿起新的配置文件。要做到这一点,我必须编辑应用程序的grails-app/conf/Config.groovy
。我将在classpath中添加两个配置文件。
println ('* Adding configuration files to grails.config.locations');
// Add configuration files to grails.config.locations.
def newConfigFiles = ["classpath:jcc-server-config.properties",
"classpath:SecurityConfig.groovy"]
// Get the application's Config.groovy file
def cfg = new File("${basedir}/grails-app/conf/Config.groovy");
def cfgText = cfg.text
def appendedText = new StringWriter()
appendedText.println ""
appendedText.println ("// Added by edu-sunyjcc-addons plugin");
// Slurp the configuration so we can look at grails.config.locations.
def config = new ConfigSlurper().parse(cfg.toURL());
// If it isn't defined, create it as a list.
if (config.grails.config.locations.getClass() == groovy.util.ConfigObject) {
appendedText.println('grails.config.locations = []');
} else {
// Don't add configuration files that are already on the list.
newConfigFiles = newConfigFiles.grep {
!config.grails.config.locations.contains(it)
};
}
// Add each surviving location to the list.
newConfigFiles.each {
// The name will have quotes around it...
appendedText.println "grails.config.locations << \"$it\"";
}
// Write the new configuration code to the end of Config.groovy.
cfg.append(appendedText.toString());
唯一的问题是增加SecurityConfig.groovy
到类路径。我发现你可以通过在插件的/scripts/Events.groovy
中创建以下事件来实现。
eventCompileEnd = {
ant.copy(todir:classesDirPath) {
fileset(file:"${basedir}/grails-app/conf/SecurityConfig.groovy")
}
}
Ed。
哇。一个有1000个观点的问题,而不是单一的upvote ......这是否值得另一个徽章? – rdmueller 2013-08-13 13:31:03