CI学习 CCNET Config 第一天

CCNet的整体结构就是一个Xml文档,根元素就是cruisecontrol,具体的代码块如下所示:

  1. <cruisecontrol xmlns:cb="urn:ccnet.config.builder">  
  2.      <project name="P1">  
  3.          <other settings />  
  4.      </project>  
  5.      <project name="P2">  
  6.          <other settings />  
  7.      </project>  
  8. </cruisecontrol>  
<cruisecontrol xmlns:cb="urn:ccnet.config.builder">
     <project name="P1">
         <other settings />
     </project>
     <project name="P2">
         <other settings />
     </project>
</cruisecontrol>

其中,命名空间标定了CCNet里面的预处理过程(Preprocessor)。

一、预处理:Preprocessor

    预处理里面包含三个主要元素:define、include和scope,define用于定义以后扩展的常量、include用于包含其他文件的类容,而scope用于封装现存的常量的值。

   1、define元素

    define元素可以用来定义一个预处理(Preprocessor)常量。如下所示:

  1. <cb:define foo="bar" />  
 <cb:define foo="bar" />

   也可以在一行中定义多个常量:

  1. <cb:define a="1" b="2" c="3"/>  
<cb:define a="1" b="2" c="3"/>


   还可以定义一个xml段:

 

 

  1. <cb:define name="baz">  
  2.   <some_element>  
  3.     <some_inner_element/>  
  4.   </some_element>  
  5. </cb:define>  
<cb:define name="baz">
  <some_element>
    <some_inner_element/>
  </some_element>
</cb:define>

任何有效的xml代码元素都可以包含其中,包括元素、属性、文本节点和注释等。

 

    有两种方式来使用定义的常量:文本引用和xml引用

    1)、文本引用的方式如下:$(const_name)

如果常量未定义,系统将搜索系统变量来替换;如果系统变量也不存在,将引发错误。

 

  1. <pre><code class="xml syntaxhl"><span class="CodeRay"><span class="tag"><cb:define</span<span class="attribute-name">foo</span>=<span class="string"><span class="delimiter">"</span><span class="content">bar</span><span class="delimiter">"</span></span><span class="tag">/></span></span></code>  
<pre><code class="xml syntaxhl"><span class="CodeRay"><span class="tag"><cb:define</span> <span class="attribute-name">foo</span>=<span class="string"><span class="delimiter">"</span><span class="content">bar</span><span class="delimiter">"</span></span><span class="tag">/></span></span></code>

<somexml attr1="$(foo)"/>

 

<somexml>$(foo)</somexml>
<env dir="$(PATH)"/>

    2)、xml引用的方式如下:

 

如果常量未定义,系统将搜索系统变量来替换;如果系统变量也不存在,将引发错误。

 

  1. <cb:define foo="bar"/>  
  2. <sample>  
  3.   <cb:foo/>  
  4. </sample>  
<cb:define foo="bar"/>
<sample>
  <cb:foo/>
</sample>


其效果是直接替换被引用的值,上面结果如下:

 

 

  1. <sample>  
  2.   bar  
  3. </sample>  
<sample>
  bar
</sample>

   3)、常量的嵌套以及参数

 

常量引用可以嵌套使用,如下所示:

 

  1. <cb:define alpha="alphaval"/>  
  2. <cb:define zed="zedval/$(alpha)"/>  
  3.   
  4. <z>$(zed)</z>  
<cb:define alpha="alphaval"/>
<cb:define zed="zedval/$(alpha)"/>

<z>$(zed)</z>

在高层次的嵌套时,在cb:varname这种语法中,常量值是可以传递到调用的元素中去的,如下面的例子:

 

 

  1. <cb:define name="beta">  
  2.   <hello>  
  3.     <cb:gamma/>  
  4.     <hi attr1="$(delta)"/>  
  5.   </hello>  
  6. </cb:define>  
<cb:define name="beta">
  <hello>
    <cb:gamma/>
    <hi attr1="$(delta)"/>
  </hello>
</cb:define>


此时的gamma和delta都还没有定义

 

 

  1. <cb:beta delta="deltaval">  
  2.   <cb:define name="gamma">  
  3.     <gamma_element>hi</gamma_element>  
  4.   </cb:define>  
  5. </cb:beta>  
<cb:beta delta="deltaval">
  <cb:define name="gamma">
    <gamma_element>hi</gamma_element>
  </cb:define>
</cb:beta>

在定义了如下xml段之后,gamma就可以正常被替换了,而且上面没有定义的delta常量,也会通过cb:deta定义的delta常量所替换。最终结果如下:

 

 

  1. <hello>  
  2.   <gamma_element>hi</gamma_element>  
  3.   <hi attr1="deltaval" />  
  4. </hello>  
<hello>
  <gamma_element>hi</gamma_element>
  <hi attr1="deltaval" />
</hello>


    2、Scope元素

 

Scope用于控制预处理定义中的范围,在同一个范围里面不可以定义相同的常量,但是在嵌套的范围里面,可以覆盖外层定义的常量,这一点和程序里面的代码段很像。

如下定义:

 

  1. <cb:scope a="a_val" b="b_val">  
  2.   <test attr="$(a)" attr2="$(b)"/>  
  3.   <cb:scope a="a_val_redefined">  
  4.     <test attr="$(a)" attr2="$(b)"/>  
  5.   </cb:scope>  
  6. </cb:scope>  
<cb:scope a="a_val" b="b_val">
  <test attr="$(a)" attr2="$(b)"/>
  <cb:scope a="a_val_redefined">
    <test attr="$(a)" attr2="$(b)"/>
  </cb:scope>
</cb:scope>

其结果为:

 

 

  1. <test attr="a_val" att2="b_val"/>  
  2. <test attr="a_val_redefined" att2="b_val"/>  
<test attr="a_val" att2="b_val"/>
<test attr="a_val_redefined" att2="b_val"/>

其中就覆盖了外层的a常量。

 

可以将scope应用于CCNet配置文件的Project元素,示例如下:

 

  1. <cruisecontrol xmlns:cb="urn:ccnet.config.builder">  
  2.    <cb:define WorkingMainDir="C:\Integration\"/>  
  3.    <cb:define WorkingDir="\WorkingDirectory"/>  
  4.    <cb:define ArtifactsDir="\Artifacts"/>  
  5.    
  6.    <cb:scope ProjectName="Alpha">  
  7.      <project name="$(ProjectName)" queue="Q1" queuePriority="1">  
  8.        <workingDirectory>$(WorkingMainDir)$(ProjectName)$(WorkingDir)</workingDirectory>  
  9.        <artifactDirectory>$(WorkingMainDir)$(ProjectName)$(ArtifactsDir)</artifactDirectory>  
  10.     </project>  
  11.   </cb:scope>  
  12.   
  13.   <cb:scope ProjectName="Beta">  
  14.     <project name="$(ProjectName)" queue="Q1" queuePriority="1">  
  15.       <workingDirectory>$(WorkingMainDir)$(ProjectName)$(WorkingDir)</workingDirectory>  
  16.       <artifactDirectory>$(WorkingMainDir)$(ProjectName)$(ArtifactsDir)</artifactDirectory>  
  17.     </project>  
  18.   </cb:scope>  
  19.   
  20. </cruisecontrol>  
<cruisecontrol xmlns:cb="urn:ccnet.config.builder">
   <cb:define WorkingMainDir="C:\Integration\"/>
   <cb:define WorkingDir="\WorkingDirectory"/>
   <cb:define ArtifactsDir="\Artifacts"/>
 
   <cb:scope ProjectName="Alpha">
     <project name="$(ProjectName)" queue="Q1" queuePriority="1">
       <workingDirectory>$(WorkingMainDir)$(ProjectName)$(WorkingDir)</workingDirectory>
       <artifactDirectory>$(WorkingMainDir)$(ProjectName)$(ArtifactsDir)</artifactDirectory>
    </project>
  </cb:scope>

  <cb:scope ProjectName="Beta">
    <project name="$(ProjectName)" queue="Q1" queuePriority="1">
      <workingDirectory>$(WorkingMainDir)$(ProjectName)$(WorkingDir)</workingDirectory>
      <artifactDirectory>$(WorkingMainDir)$(ProjectName)$(ArtifactsDir)</artifactDirectory>
    </project>
  </cb:scope>

</cruisecontrol>

而其结果则为:

 

 

  1. <cruisecontrol>  
  2.    
  3.      <project name="Alpha" queue="Q1" queuePriority="1">  
  4.        <workingDirectory>C:\Integration\Alpha\WorkingDirectory</workingDirectory>  
  5.        <artifactDirectory>C:\Integration\Alpha\Artifacts</artifactDirectory>  
  6.      </project>  
  7.    
  8.      <project name="Beta" queue="Q1" queuePriority="1">  
  9.       <workingDirectory>C:\Integration\Beta\WorkingDirectory</workingDirectory>  
  10.       <artifactDirectory>C:\Integration\Beta\Artifacts</artifactDirectory>  
  11.     </project>  
  12.   
  13. </cruisecontrol>  
<cruisecontrol>
 
     <project name="Alpha" queue="Q1" queuePriority="1">
       <workingDirectory>C:\Integration\Alpha\WorkingDirectory</workingDirectory>
       <artifactDirectory>C:\Integration\Alpha\Artifacts</artifactDirectory>
     </project>
 
     <project name="Beta" queue="Q1" queuePriority="1">
      <workingDirectory>C:\Integration\Beta\WorkingDirectory</workingDirectory>
      <artifactDirectory>C:\Integration\Beta\Artifacts</artifactDirectory>
    </project>

</cruisecontrol>

 

通过scope,一个变通的定义方式如下:

 

  1. <cruisecontrol xmlns:cb="urn:ccnet.config.builder">  
  2.   <cb:define WorkingMainDir="C:\Integration\"/>  
  3.   <cb:define WorkingDir="\WorkingDirectory"/>  
  4.   <cb:define ArtifactsDir="\Artifacts"/>  
  5.    
  6.   <cb:define name="OurProject">  
  7.     <project name="$(ProjectName)" queue="Q1" queuePriority="1">  
  8.       <workingDirectory>$(WorkingMainDir)$(ProjectName)$(WorkingDir)</workingDirectory>  
  9.       <artifactDirectory>$(WorkingMainDir)$(ProjectName)$(ArtifactsDir)</artifactDirectory>  
  10.    </project>  
  11.  </cb:define>  
  12.   
  13.  <cb:scope ProjectName="Alpha">  
  14.    <cb:OurProject/>  
  15.  </cb:scope>  
  16.   
  17.  <cb:scope ProjectName="Beta">  
  18.    <cb:OurProject/>  
  19.  </cb:scope>  
  20. </cruisecontrol>  
<cruisecontrol xmlns:cb="urn:ccnet.config.builder">
  <cb:define WorkingMainDir="C:\Integration\"/>
  <cb:define WorkingDir="\WorkingDirectory"/>
  <cb:define ArtifactsDir="\Artifacts"/>
 
  <cb:define name="OurProject">
    <project name="$(ProjectName)" queue="Q1" queuePriority="1">
      <workingDirectory>$(WorkingMainDir)$(ProjectName)$(WorkingDir)</workingDirectory>
      <artifactDirectory>$(WorkingMainDir)$(ProjectName)$(ArtifactsDir)</artifactDirectory>
   </project>
 </cb:define>

 <cb:scope ProjectName="Alpha">
   <cb:OurProject/>
 </cb:scope>

 <cb:scope ProjectName="Beta">
   <cb:OurProject/>
 </cb:scope>
</cruisecontrol>

这样则较大的提高了配置的可维护性。

 

    3、include元素

    include元素用来包含其他的文件内容,include中根据ccnet.config文件为基准路径进行相对定位,这样就可以在ccnet.config文件里面调用所有其他文件定义的部分,以提高可维护性。如下所示:

 

  1. <cruisecontrol xmlns:cb="urn:ccnet.config.builder">  
  2.    
  3.    <cb:include href="Definitions.xml" xmlns:cb="urn:ccnet.config.builder"/>  
  4.    
  5.    <cb:include href="CI_Projects.xml" xmlns:cb="urn:ccnet.config.builder"/>  
  6.    
  7.    <cb:include href="QA_Projects.xml" xmlns:cb="urn:ccnet.config.builder"/>  
  8.    
  9.  </cruisecontrol>  
<cruisecontrol xmlns:cb="urn:ccnet.config.builder">
 
   <cb:include href="Definitions.xml" xmlns:cb="urn:ccnet.config.builder"/>
 
   <cb:include href="CI_Projects.xml" xmlns:cb="urn:ccnet.config.builder"/>
 
   <cb:include href="QA_Projects.xml" xmlns:cb="urn:ccnet.config.builder"/>
 
 </cruisecontrol>

其中Definitions.xml文件定义如下:

 

 

  1. <cb:config-template xmlns:cb="urn:ccnet.config.builder">  
  2.   
  3.   <queue name="Q1" duplicates="UseFirst" lockqueues="Q2, Q4" />  
  4.   
  5.   <cb:define name="EmailPublisher">  
  6.     <email from="buildmaster@mycompany.com"   
  7.           mailhost="localhost"   
  8.           mailhostUsername="TheMailer"   
  9.           mailhostPassword="JohnWayne"   
  10.           includeDetails="TRUE">  
  11.     <users />  
  12.         <groups />  
  13.   
  14.         <modifierNotificationTypes>  
  15.           <NotificationType>Failed</NotificationType>  
  16.           <NotificationType>Fixed</NotificationType>  
  17.         </modifierNotificationTypes>  
  18.   
  19.     </email>  
  20.   </cb:define>  
  21.   
  22.   <cb:define name="common_publishers">  
  23.     <artifactcleanup cleanUpMethod="KeepMaximumXHistoryDataEntries" cleanUpValue="500" />  
  24.     <xmllogger />  
  25.     <statistics />  
  26.     <modificationHistory  onlyLogWhenChangesFound="true" />  
  27.     <rss/>  
  28.   </cb:define>  
  29.   
  30.   <cb:define name="common_nant">  
  31.        <executable>c:\nant\nant.exe</executable>  
  32.        <nologo>true</nologo>  
  33.     <buildTimeoutSeconds>240</buildTimeoutSeconds>  
  34.   </cb:define>  
  35.   
  36.   <cb:define name="nant_args_CI">  
  37.       <buildArgs>clean compile</buildArgs>  
  38.   </cb:define>  
  39.   
  40. </cb:config-template>  
<cb:config-template xmlns:cb="urn:ccnet.config.builder">

  <queue name="Q1" duplicates="UseFirst" lockqueues="Q2, Q4" />

  <cb:define name="EmailPublisher">
    <email from="buildmaster@mycompany.com" 
          mailhost="localhost" 
          mailhostUsername="TheMailer" 
          mailhostPassword="JohnWayne" 
          includeDetails="TRUE">
    <users />
        <groups />

        <modifierNotificationTypes>
          <NotificationType>Failed</NotificationType>
          <NotificationType>Fixed</NotificationType>
        </modifierNotificationTypes>

    </email>
  </cb:define>

  <cb:define name="common_publishers">
    <artifactcleanup cleanUpMethod="KeepMaximumXHistoryDataEntries" cleanUpValue="500" />
    <xmllogger />
    <statistics />
    <modificationHistory  onlyLogWhenChangesFound="true" />
    <rss/>
  </cb:define>

  <cb:define name="common_nant">
       <executable>c:\nant\nant.exe</executable>
       <nologo>true</nologo>
    <buildTimeoutSeconds>240</buildTimeoutSeconds>
  </cb:define>

  <cb:define name="nant_args_CI">
      <buildArgs>clean compile</buildArgs>
  </cb:define>

</cb:config-template>


在Definitions.xml文件里面,需要使用db:config-template作为根元素使用。

本文转载自:http://blog.csdn.net/yant255/article/details/43306227

posted @ 2017-12-11 18:37  行者马  阅读(277)  评论(0编辑  收藏  举报