[原创]Cruisecontrol利用plugin进行预设置,精简config.xml配置文件
Cruisecontrol利用plugin进行预设置,精简config.xml配置文件来源:配置管理之路(http://bbs.scmroad.com) 作者:laofo
我们先看看plugin的介绍
<plugin>
<cruisecontrol>
<plugin>
<project>
<plugin>
A <plugin> element registers a classname with an alias for use within the configuration file.
Plugins can also be pre-configured at registration time. This can greatly reduce the configuration file size.The plugins page contains a discussion of the plugin architecture used with CruiseControl.
Attributes
name Yes The alias used to refer to the plugin elsewhere in the configuration file.
classname Yes The class that implements the plugin.
利用plugin可以注册新的类名这一点我们先不讲,我们着重看这一段
Plugins can also be pre-configured at registration time. This can greatly reduce the configuration file size.
Plugin可以在注册时期内进行预设置,这一点可以很好的减少配置文件的大小.今天我们就要说这一点.然后,我们点击pre-configured会得到下面的信息
Plugin Preconfiguration
On registering plugins, you can also define default values for some of the properties and nested elements of the plugins. You can do this by simply using the same attributes and child nodes as you would when using the plugin itself.
Defaults can be overridden when you actually use the plugin within a project. Note: when nested elements have a multiple cardinality, overriding an already pre-configured child element might accumulate them.
If you want to preconfigure an already registered plugin (whether default plugin, or plugin registered at the root of the config), you may leave out the classname attribute of the plugin element.
An example: to define some default values for the htmlemail publisher, you can specify something like this in your config file:
<plugin name="htmlemail"
mailhost="smtp.example.com"
returnaddress="buildmaster@example.com"
subjectprefix=""
xsldir="C:\java\cruisecontrol-2.2.1\report\jsp\webcontent\xsl"
css="C:\java\cruisecontrol-2.2.1\report\jsp\webcontent\css\cruisecontrol.css">
<always address="project-dev@domain.com"/>
<plugin/>
Combined with ant style properties this can greatly reduce the size of your config files. In the extreme case you can create a template project where all that needs to be supplied is the project name:
<cruisecontrol>
<plugin name="project">
<bootstrappers>
<cvsbootstrapper localworkingcopy="projects/${project.name}"/>
</bootstrappers>
...
</plugin>
<project name="foo" />
<project name="bar" />
...
</cruisecontrol>
请详细阅读上边的内容, 我们就可以了解到plugin插件在减少配置文件大小方面的作用.
简单的描述下它的思想,
[*]我们可以plugin插件里,可以创建一个模版,在这个模版中我们可以对一些属性(properties )或者内嵌元素(nested elements)进行一些默认设置[*]在项目中,当我们利用到这些属性的时候, 这些在这个模版中定义的内容就不用重新再定义了.除非我们想覆盖掉(overridden )这些默认值.我们只需要在项目的配置部分写上每个项目不同的部分就可以了.
上边以htmlemail讲了一个例子, 那么下面我们以email为例.
首先,我们来看下不利用这一特性,我们的每个项目的email部分该怎么写
<email mailhost="${mail.mailhost}"
defaultsuffix="@company.com"
buildresultsurl="http://${cruisecontrol.server}:8080/buildresults/${project.name}"
mailhost="${mail.mailhost}"
username="${mail.username}"
password="${mail.password}"
reportsuccess="always"
returnaddress="${buildmasters}"
skipusers="false"
subjectprefix="CI BUILD" >
<always address="${buildmasters}" />
<failure address="developers" />
<map alias="developer1" address="${developer1}" />
<map alias="developer2" address="${developer2}" />
<map alias="buildmasters" address="${buildmasters}" />
<map alias="developers" address="buildmasters, developer1, developer2" />
</email>
我们就要在每个项目的email部分都需要复制上边的内容.上边这段配置内容,每个项目开发人员的的email可能不同,需要针对具体项目进行更改,但是我们会发现其它大部分内容各个项目都是一样的.而且这些内容也是每个项目都共用的,比如mail.mailhost, cruisecontrol.server等等属性.
假设,如果我们把配置文件写成上边这个样子,那么因为服务器变更,比如IP地址,域名等变化,我们就需要针对整个配置文件进行更改,找到每个相关的属性进行更改.这便会造成,
1. 更改麻烦,要每个文件或者每个项目的相应部分都去改
2. 容易出错
3. 不利于维护
然后我们再看看我们利用Plugin Preconfiguration这个性质我们怎么做的
首先我们需要在<cruisecontrol></cruisecontrol>标签下, <project>标签前设置这些属性.(如果只在某个<project>下边设置,那只针对这个项目)
在项目标签之前对属性或者内嵌元素进行预设置
<cruisecontrol>
....
<plugin name="email"
mailhost="${mail.mailhost}"
username="${mail.username}"
password="${mail.password}"
reportsuccess="always"
returnaddress="${buildmasters}"
skipusers="false"
subjectprefix="CI BUILD"
defaultsuffix="@company.com"
buildresultsurl="http://${cruisecontrol.server}:8080/buildresults/${project.name}"
/>
......
<project name="connectfour-svn">
</project>
<project name="connectfour-perforce">
</project>
<project name="connectfour-clearcase">
</project>
<cruisecontrol>
其次,我们在项目中使用这些默认的属性
<project name="connectfour-svn">
......
<publishers>
......
<email>
<always address="${buildmasters}" />
<failure address="developers" />
<map alias="developer1" address="${developer1}" />
<map alias="developer2" address="${developer2}" />
<map alias="buildmasters" address="${buildmasters}" />
<map alias="developers" address="buildmasters, developer1, developer2" />
</email>
......
</publishers>
....
</project>
通过上面我们可以看到,如果需要对服务器进行配置那么我们只要针对<plugin name="email" />这段进行修改就可以了.免去了找到每个项目,针对这部分都要修改.既容易出错,还麻烦.
如果需要对某个项目进行配置,那么我们只要找到相应的项目段,对<email>这部分进行修改了.(事实上,我们可以在cruisecontrl的配置文件中只声明和预配置所有的标签,剩下的都放到各个项目的config.xml中去,这一点我的后续文章中还会进行介绍)
这样做不但降低了配置管理员(CM)的工作量,也避免了一些没有必要的错误.
转载请注目出处,谢谢合作.欢迎大家来一起讨论软件配置管理的方方面面.谢谢.
页:
[1]