2012-03-16 63 views
5

正如我想写一个Grails插件,我偶然发现了两个问题:从插件中修改项目配置的最佳方法是什么?

  • 我怎么修改像Config.groovy或witin的_install.groovy脚本DataSource.groovy配置文件中的一个?向这些文件添加内容很容易,但是如何以干净的方式修改它? text.replaceAll()?或者我应该创建一个新的配置文件?
  • 我该如何获取插件安装到的当前应用程序的名称?我试图使用app.nameappName,但两者都不起作用。

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

+0

哇。一个有1000个观点的问题,而不是单一的upvote ......这是否值得另一个徽章? – rdmueller 2013-08-13 13:31:03

回答

5

以下是从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。

+0

这很棒添加新的配置文件的代码我仍然想知道什么是最好的方法来改变(例如)默认的dataSource配置......也许我只是添加一个注释到旧文件的顶部并覆盖我的新外部文件中的配置。 .. – rdmueller 2012-03-21 15:20:49

+1

谢谢。我认为默认的'DataSource.groovy'不是很有价值且易于替换,所以我只是用自己的代码覆盖它。 – 2012-03-21 18:09:41

1

修改配置文件,您应该使用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' 
+0

slurper的问题是,如果我想在安装时添加一个配置属性,我将不得不啜泣配置,修改它并将其序列化到磁盘。但是序列化会删除所有评论:-( – rdmueller 2012-03-16 18:35:31

+0

cool。'metadata.'app.name'' works!Thanx! – rdmueller 2012-03-16 19:01:58

3

您可以试着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。

+0

hm。听起来不错 - 这种方式我可以啜泣另一个配置进入主配置... – rdmueller 2012-03-16 18:32:52

相关问题