Grails 从插件中修改项目配置的最佳方法是什么?
在我尝试编写Grails插件时,我偶然发现了两个问题:Grails 从插件中修改项目配置的最佳方法是什么?,grails,plugins,Grails,Plugins,在我尝试编写Grails插件时,我偶然发现了两个问题: 如何在\u install.groovy脚本中修改WIT中的一个配置文件,如Config.groovy或DataSource.groovy?在这些文件中添加一些内容很容易,但是如何以干净的方式修改它呢text.replaceAll()?或者我应该创建一个新的配置文件 如何获取将安装插件的当前应用程序的名称?我尝试使用app.name和appName,但两者都不起作用 是否有关于创建我还没有找到的插件的好教程?要修改配置文件,您应该使用:
- 如何在
脚本中修改WIT中的一个配置文件,如\u install.groovy
或Config.groovy
?在这些文件中添加一些内容很容易,但是如何以干净的方式修改它呢DataSource.groovy
?或者我应该创建一个新的配置文件text.replaceAll()
- 如何获取将安装插件的当前应用程序的名称?我尝试使用
和app.name
,但两者都不起作用appName
是否有关于创建我还没有找到的插件的好教程?要修改配置文件,您应该使用: 如果需要从脚本获取应用程序名称,请尝试:
metadata.'app.name'
您可以尝试更改MyNiftyPlugin.groovy文件中的配置(假设您的插件名为my nifty)。我发现我可以在doWithApplicationContext闭包中更改配置值。这里有一个例子
def doWithApplicationContext = { applicationContext ->
def config = application.config;
config.edu.mycollege.server.name = 'http://localhost:8080'
config.edu.mycollege.server.instance = 'pprd'
}
在这里输入的值在运行时会显示在grailsApplication.config变量中。如果它适合您,那么它将是一个更整洁的解决方案,因为它不需要更改客户机项目
我必须证明这一点,因为我不能用这种技术让SpringSecurity工作。我相信我的插件(取决于Spring安全性)是在安全性初始化之后加载的。我决定向grails app/conf目录添加一个额外的文件
HTH.下面是一个从
脚本/\u 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获取新的配置文件。为此,我必须编辑应用程序的grailsapp/conf/Config.groovy
。我将在类路径上添加两个配置文件
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());
Ed.hm.听起来不错-这样我可以在主配置中插入另一个配置…slurper的问题是,如果我不想在安装时添加配置属性,我将不得不再次插入配置,修改它并将其序列化到磁盘。但是序列化将删除所有注释:-(这是添加新配置文件的好代码。我仍然想知道更改的最佳方式是什么(例如)默认的数据源配置…也许我只是在旧文件的顶部添加一条注释,然后在新的外部文件中覆盖配置…谢谢。我认为默认的
dataSource.groovy
不是很有价值,也不容易替换,所以我只是用我自己的来覆盖它。哇。一个有1000个视图的问题,而不是一次投票。。。这难道不值得再拿一枚徽章吗?
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());
eventCompileEnd = {
ant.copy(todir:classesDirPath) {
fileset(file:"${basedir}/grails-app/conf/SecurityConfig.groovy")
}
}