【问题标题】:What is the best way to modify a project configuration from within a plugin?从插件中修改项目配置的最佳方法是什么?
【发布时间】:2012-04-02 04:05:20
【问题描述】:

在尝试编写 Grails 插件时,我偶然发现了两个问题:

  • 如何通过_install.groovy 脚本修改Config.groovyDataSource.groovy 等配置文件之一?向这些文件附加一些内容很容易,但是如何以干净的方式对其进行修改? text.replaceAll()?还是我应该创建一个新的配置文件?
  • 如何获取要安装插件的当前应用程序的名称?我尝试使用app.nameappName,但都不起作用。

是否有关于创建我还没有找到的插件的好教程?

【问题讨论】:

  • 哇。一个有 1000 次浏览但没有一个点赞的问题……这难道不值得另一个徽章吗?

标签: grails plugins


【解决方案1】:

这是一个从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。我将在类路径中添加两个配置文件。

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")
    }
}

编。

【讨论】:

  • 这是添加新配置文件的好代码。我仍然想知道更改(例如)默认数据源配置的最佳方法是什么...也许我只是在旧文件的顶部添加注释并覆盖新外部文件中的配置...
  • 谢谢。我认为默认的DataSource.groovy 不是很有价值且易于替换,所以我只是用自己的覆盖它。
【解决方案2】:

您可以尝试更改 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 变量中。如果它适合您,它将是一个更简洁的解决方案,因为它不需要更改客户端项目。

我必须以我无法让 Spring Security 通过这种技术工作这一事实来证明这一点。我相信我的插件(依赖于 Spring Security)是在安全初始化后加载的。我决定在 grails-app/conf 目录中添加一个额外的文件。

HTH。

【讨论】:

  • 嗯。听起来不错 - 这样我可以将另一个配置添加到主配置中......
【解决方案3】:

修改配置文件,请使用ConfigSlurper:

def configParser = new ConfigSlurper(grailsSettings.grailsEnv)
configParser.binding = [userHome: userHome]
def config = configParser.parse(new URL("file:./grails-app/conf/Config.groovy"))

如果您需要从脚本中获取应用程序名称,请尝试:

metadata.'app.name'

【讨论】:

  • slurper 的问题是,如果我想在安装时添加配置属性,我将不得不 slurp 配置,修改它并再次将其序列化到磁盘。但是序列化会删除所有 cmets :-(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-10-23
  • 2020-08-17
  • 1970-01-01
  • 2011-06-07
相关资源
最近更新 更多