This page last changed on Nov 01, 2010 by williams.

The CCNet configuration preprocessor acts on the ccnet.config file. Preprocessor directives are specified in the XML namespace "urn:ccnet.config.builder" to distinguish them from ordinary configuration markup. Any markup not in the preprocessor's namespace are passed through unchanged.

Preparing to Use the Preprocessor

The configuration preprocessor is invoked by declaring an XML Namespace of "urn:ccnet.config.builder" on the root configuration element ("<cruisecontrol>"). The namespace abbreviation you choose must be specified on any preprocessor directives you use. For the rest of this explanation, we will specify it as "cb", a mnemonic for "configuration builder".

<cruisecontrol xmlns:cb="urn:ccnet.config.builder">

Preprocessor Elements

The configuration preprocessor has several elements that control its processing of your configuration.

  • The <define> element is used to define constants to be expanded later.
  • The <include> element is used to include the contents of another file.
  • The <scope> element is used to encapsulate sections that change the value of an existing constant.

Defining Preprocessor Constants

The <define> element is used to define a preprocessor constant. It can be used in several ways:

Text constants (values are strings):

Define a constant named "foo" with a value of "bar":

<cb:define foo="bar"/>

You can define more than one constant in the same define element:

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

Nodeset constants (values are XML fragments):

Define a constant named "baz" with a value of an xml fragment:

<cb:define name="baz">
  <some_element>
    <some_inner_element/>
  </some_element>
</cb:define>

Any valid XML inside the define element is considered to be the constant's value. This includes elements, attributes, text nodes, and comments.

Expanding Preprocessor Constant Values

Once defined, preprocessor constants can be expanded in two ways: as text references or as XML references.

Text References

References of the form "$(const_name)" which are found in attribute values or text nodes will be expanded such that the text value replaces the reference. If no constant exists with the given name and there exists a windows environment variable with the same name, that environment variable's value will be used.

Examples

Use in an attribute:

<cb:define foo="bar"/>
<somexml attr1="$(foo)"/>

expands to:

<somexml attr1="bar"/>

Use as text of an element:

<cb:define foo="bar"/>
<somexml>$(foo)</somexml>

expands to:

<somexml>bar</somexml>

Use of Windows environment variables:

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

expands to:

<env dir="... your PATH environment value ..."/>

XML References

When the preprocessor encounters an element in the preprocessor namespace, and the element name is not one of the predefined keywords (define,scope,config-template, etc), the element will be replaced by the constant value associated with the element name.

Examples

Use as text of an element:

<cb:define foo="bar"/>
<sample>
  <cb:foo/>
</sample>

expands to:

<sample>
  bar
</sample>

Uses as sub-element:

<cb:define name="baz">
  <some_element>
    <some_inner_element/>
  </some_element>
</cb:define>
<sample>
  <cb:baz/>
</sample>

expands to:

<sample>
  <some_element>
    <some_inner_element/>
  </some_element>
</sample>

If a constant reference refers to a constant which has not been defined and which does not exist as an OS environment variable, an error will occur.

Nested Expansions and Parameters

Constant references can be nested, i.e., the value of constant "zed" can contain a reference to constant "alpha".

<cb:define alpha="alphaval"/>
<cb:define zed="zedval/$(alpha)"/>

<z>$(zed)</z>

expands to:

<z>zedval/alphaval</z>

In addition, using the cb:varname call syntax outlined above, constant values can be passed as part of the call element. Consider the following definition, in which neither "gamma" nor "delta" have yet been defined:

<cb:define name="beta">
  <hello>
    <cb:gamma/>
    <hi attr1="$(delta)"/>
  </hello>
</cb:define>

Since gamma and delta have not been defined, they must be passed in with the reference to beta. This is done as follows:

<cb:beta delta="deltaval">
  <cb:define name="gamma">
    <gamma_element>hi</gamma_element>
  </cb:define>
</cb:beta>

This will expand to:

<hello>
  <gamma_element>hi</gamma_element>
  <hi attr1="deltaval" />
</hello>

Scopes

The <scope> element can be used to control the scope of a preprocessor definition. You may not define the same constant twice within the same scope, however you may introduce a nested scope which redefines a particular value in an outer scope. A scope is semantically equivalent to a stack frame in traditional programming terms.

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

expands to:

<test attr="a_val" att2="b_val"/>
<test attr="a_val_redefined" att2="b_val"/>

The scope can be used to set the projectname in a variable, making copy and paste of projects less error prone.
Changing a certain folder is also more easy / safe.

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

expands to :

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

Or an even more advanced one (thanks to Arieh Schneier) :

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

Comments

XML Comments whose first character is '#' will not be copied to the output. All other comments will.

<!-- This comment will appear in the output file-->
<!--# This comment will not -->

Including files

The <include> element is used to include the contents of another file. The element is replaced with the contents of that file, which must be a valid XML document. The file is specified as a URL, relative to the file that contains the <include> element.

Assuming that the CCNet configuration file "ccnet.config" includes the file "projects\project.config":

... lines before ...
<cb:include href="projects/project.config"/>
... lines after ...

and that the "projects\project.config" file includes a file called "EmailConfig.xml" from the same directory as "ccnet.config":

<project>
  ... project definition ...
  <publishers>
    <cb:include href="../EMailConfig.xml"/>
    ... more publishers ...
  </publishers>
</project>

and that the "EMailConfig.xml" file contains an Email Publisher definition:

<email>
  ... email configuration info ...
</email>

the results would be:

... lines before ...
<project>
  ... project definition ...
  <publishers>
    <email>
      ... email configuration info ...
    </email>
    ... more publishers ...
  </publishers>
</project>
... lines after ...

Including files with multiple elements

A main ccnet.config file can easily contain dozens of project definitions, and to reduce the file size and so keeping the overview, it may be easy to group certain projects in separate files.

  • the main ccnet.config file grouping all includes, and small projects
  • a definition file, containing the global definitions
  • a CI_Projects file, containing all projects for the CI process
  • a DeployToQA file, containing all projects that will go to the QA environment,
  • ....

Setting this up :

The main ccnet.config file

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

The Definitions.xml file

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

The CI_Projects.xml file

<cb:config-template xmlns:cb="urn:ccnet.config.builder">

  <project name="Preprocesser2" queue="Q1" queuePriority="1">
      <workingDirectory>C:\Integration\Preprocesser2\WorkingDirectory</workingDirectory>
      <artifactDirectory>C:\Integration\Preprocesser2\Artifacts</artifactDirectory>

      <triggers>
        <intervalTrigger name="continuous" seconds="60" buildCondition="IfModificationExists" />
      </triggers>

      <sourcecontrol type="nullSourceControl" />

      <tasks>
         <cb:nant_local/>
      </tasks>

      <publishers>
         <cb:common_publishers/>
      </publishers>
  </project>


  <project name="FtpTransfer2" queue="Q1" queuePriority="102">
     <workingDirectory>C:\Integration\FtpTransfer2\WorkingDirectory</workingDirectory>
     <artifactDirectory>C:\Integration\FtpTransfer2\Artifacts</artifactDirectory>

     <triggers>
	<intervalTrigger name="continuous" seconds="60" buildCondition="IfModificationExists" />
     </triggers>

     <sourcecontrol type="nullSourceControl" />

     <tasks>
	<exec>
	   <executable>c:\tools\consolenok</executable>
	   <buildArgs>all</buildArgs>
	   <buildTimeoutSeconds>10</buildTimeoutSeconds>
       </exec>

    </tasks>

    <publishers>
       <cb:common_publishers/>
    </publishers>
  </project>

</cb:config-template>

The same goes for the other included files, things to remember :

  • define a <cb:config-template xmlns:cb="urn:ccnet.config.builder"> root element in every included file
  • first include all definitions, and later the files containing the projects using them
Version 1.6 and higher only from now on

The configuration options below are for CCNet 1.6 and higher only.

Conditional statements :

IFDEF / IFNDEF directives

The <cb:ifdef> / <cb:ifndef> constructs are used during evaluation to conditionally include processing of their contained elements based on the existence/absence of a defined name.

<cb:define some_symbol_name="some_symbol_value" />


  <cb:ifdef name="some_symbol_name">
    <output>Value of some_symbol_name is $(some_symbol_name)</output>
  </cb:ifdef>
  <cb:else>
    <output>This will not appear because the symbol is defined</output>
  </cb:else>

  <cb:ifndef name="some_symbol_name">
    <output>This will not appear in the output since some_symbol_name is defined</output>
  </cb:ifndef>
  <cb:else>
    <output>This will appear because it is in the (optional) else clause</output>
  </cb:else>

IF / ELSE directives

The <cb:if> / <cb:else> constructs evaluate a JSCRIPT.NET boolean expression, and perform branching depending on the value of the expression.

<cb:define testval="gibberish" testval2="40"/>

  <cb:if expr="'$(testval)'=='gobbeldygook'">
    This will not appear, since the expression evaluates to false
  </cb:if>
  <cb:else>
    This will appear in the output.
  </cb:else>

  <cb:if expr="$(testval2) &lt; 50">
    This will appear, since testval2 is less than 50
  </cb:if>
  <cb:else>
    This will not appear in the output.
  </cb:else>

EVAL directive

Evaluates the given JSCRIPT.NET expression (of any type). The pp:eval tag is replaced with the result of the evaluation.

<!-- outputs 54-->
  <cb:eval expr="9*6"/>


  <!-- outputs 6. When referencing preprocessor variables in an expression, the value of
  the variables is substituted into the expression before it is submitted for evaluation.
  Therefore, the below expression is submitted to the JSCRIPT.NET runtime as "18/3"
  -->
  <cb:define a="3" b="18"/>
  <cb:eval expr="$(b)/$(a)"/>


  <!-- outputs the current date/time -->
  <cb:eval expr="new Date()"/>


  <!--
   Since the preprocessor replaces variable references with their values before submitting the expression
   to the JSCRIPT.NET engine, it is necessary take special steps to treat variables as strings. In the
   example below, the preprocessor will expand the value of $(str) before submitting the expression, therefore
   it is necessary to do two things:
    1. Enclose the variable reference in single quotes, making it a string literal in the JSCRIPT.NET language.
       You need to do this yourself, as in the example below.
    2. Escape any funny characters (such as embedded single quotes) so that the string literal is properly-formed.
       You achieve this by adding a :js (for JSCRIPT) format specifier after the variable name. This causes
       the preprocessor to do the proper escaping for you.

   This example outputs the value of str, converted to upper case. Literally, the JSCRIPT.NET expression
   which is evaluated is:
        'a string with \'things\' which may need to be "escaped"'.ToUpper()

    Attributes:
    expr: A JSCRIPT.NET expression
  -->

  <cb:define str="a string with 'things' which may need to be &quot;escaped&quot;"/>

  <cb:eval expr="'$(str:js)'.ToUpper()"/>

FOR loop

Loops over a range of values and emits the loop body on each iteration.

Attributes:

counter-name: The name for the counter variable. Within the body of the loop, this variable can be referenced just like any other preprocessor variable.
init-expr: JSCRIPT.NET expression whose value will be assigned to the counter variable before the first iteration.
test-expr: JSCRIPT.NET boolean expression which will determine whether the loop should terminate.

Note in the example below that it is necessary to refer to the counter variable as $ rather than just i.

count-expr: JSCRIPT.NET expression that is evaluated after every loop iteration. The value is assigned to the counter variable. In a typical counting loop, this would increment the counter by 1.

Note that in the example below, it is necessary to refer to the counter variable as $ rather than just i. Note also that the value of count-expr is only the RIGHT HAND SIDE of
of the assignment (i.e. it does not say $ =(i+1))

Scope:An implicit scope is introduced in the body of the loop, therefore any local definitions will only exist within the loop body.

This example will emit:
<hello attr="0"/>
<hello attr="1"/>
<hello attr="2"/>

<cb:for counter-name="i" init-expr="0" test-expr="$(i)&lt;3" count-expr="$(i)+1">
    <hello attr="$(i)"/>
  </cb:for>

ELEMENT and ATTRIBUTE directives

These emit XML elements, with (optional) attributes. This is just like the XSLT xsl:element/xsl:attribute mechanism. The only reason to use it rather than literal XML markup is if you wish for your element or attribute NAMES to be calculated.
Both <cb:element> and <cb:attribute> have two attributes:
name: Name of emitted element/attribute
namespace (optional): XML namespace of emitted element/attribute

This example will emit:
<myel myattr1="myattrval1" myattr2="myattrval2">myelval</myel>

<cb:define elname="myel" elval="myelval" attrname="myattr" attrval="myattrval"/>

  <cb:element name="$(elname)">

    <cb:attribute name="$(attrname)1">$(attrval)1</pp:attribute>

    <cb:attribute name="$(attrname)2">$(attrval)2</pp:attribute>

    $(elval)

  </cb:element>

Common Problems

XmlException: "There are multiple root elements" when including another file

The <include> directive requires the included file to be a valid XML document. All XML documents must have exactly one "root element" - i.e., there must be an outermost pair of start/end XML tags. The XML file being included in this case has more than one of these root elements (e.g., <project name="A">...</project> <project name="B">...</project>). The file must either be split apart so that each root element is in a separate file, or the group of root elements must be enclosed in a single outer element.

Preprocessor directives don't work in an included file

XML requires that all "namespace" definitions (i.e., xmlns:xxx attributes) must be declared in every file where they're used. Every included file must have an xmlns:cb="urn:ccnet.config.builder" attribute on its root element, even if the namespace was defined in the file that included it.

Document generated by Confluence on Jan 23, 2011 08:52