从插件中修改项目配置的最佳方法是什么? [英] What is the best way to modify a project configuration from within a plugin?

查看:121
本文介绍了从插件中修改项目配置的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




  • 我该如何修改其中一个配置文件像 Config.groovy DataSource.groovy 来自 _install.groovy 脚本?向这些文件添加内容很容易,但是如何以干净的方式修改它? text.replaceAll()?或者我应该创建一个新的配置文件?

  • 如何获取插件安装到的当前应用程序的名称?我尝试使用 app.name appName ,但两者都不起作用。


有没有什么地方可以创建一个很好的教程来创建我还没找到的插件?

解决方案

以下是从 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('* copy DataSource。 groovy')
ant.copy(文件:$ {pluginBasedir} /src/samples/DataSource.groovy,
todir:$ {basedir} / grails-app / conf)

最难的部分是让Grails拿起新的配置文件。为此,我必须编辑应用程序的 grails-app / conf / Config.groovy 。我将在classpath中添加两个配置文件。

  println('*将配置文件添加到grails.config.locations ); 
//将配置文件添加到grails.config.locations。
def newConfigFiles = [classpath:jcc-server-config.properties,
classpath:SecurityConfig.groovy]
//获取应用程序的Config.groovy文件
def cfg = new File($ {basedir} /grails-app/conf/Config.groovy);
def cfgText = cfg.text
def additionalText = new StringWriter()
appendText.println
appendText.println(//由edu-sunyjcc-addons插件添加 );
//啜食配置,以便我们查看grails.config.locations。
def config = new ConfigSlurper()。parse(cfg.toURL());
//如果未定义,请将其创建为列表。
if(config.grails.config.locations.getClass()== groovy.util.ConfigObject){
appendText.println('grails.config.locations = []');
} else {
//不要添加已经在列表中的配置文件。
newConfigFiles = newConfigFiles.grep {
!config.grails.config.locations.contains(it)
};
}
//将每个存活位置添加到列表中。
newConfigFiles.each {
//该名称将在其周围引用...
attachedText.printlngrails.config.locations< \$ it \ ;
}
//将新的配置代码写入Config.groovy的末尾。
cfg.append(attachedText.toString());

唯一的问题是添加 SecurityConfig.groovy 到类路径。我发现你可以通过在插件的 /scripts/Events.groovy 中创建以下事件来实现这一点。

  eventCompileEnd = {
ant.copy(todir:classesDirPath){
fileset(file:$ {basedir} /grails-app/conf/SecurityConfig.groovy)
}
}

Ed。


As I am trying to write a Grails Plugin, I stumbled upon two problems:

  • how do I modify one of the configuration files like Config.groovy or DataSource.groovy from witin the _install.groovy script? It is easy to append something to those files, but how do I modify it in a clean way? text.replaceAll()? Or should I create a new config file?
  • how do I get the name of the current application into which the plugin will be installed? I tried to use app.name and appName but both do not work.

Is there maybe somewhere a good tutorial on creating plugins which I haven't found yet?

解决方案

Here is an example of editing configuration files from scripts/_Install.groovy.
My plugin copies three files to the target directory.

  • .hgignore is used for version control,
  • DataSource.groovy replaces the default version, and
  • SecurityConfig.groovy contains extra settings.

I prefer to edit the application's files as little as possible, especially because I expect to change the security setup a few years down the road. I also need to use properties from a jcc-server-config.properties file which is customized for each application server in our system.

Copying the files is easy.

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

The hard part is getting Grails to pick up the new configuration file. To do this, I have to edit the application's grails-app/conf/Config.groovy. I will add two configuration files to be found on the 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());

The only problem is adding SecurityConfig.groovy to the classpath. I found that you can do that by creating the following event in the plugin's /scripts/Events.groovy.

eventCompileEnd = {
    ant.copy(todir:classesDirPath) {
      fileset(file:"${basedir}/grails-app/conf/SecurityConfig.groovy")
    }
}

Ed.

这篇关于从插件中修改项目配置的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆