<patternset>

A set of patterns, mostly used to include or exclude certain files.

The individual patterns support if and unless attributes to specify that the element should only be used if or unless a given condition is met.

The NAnt.Core.Types.PatternSet.IncludesFile and NAnt.Core.Types.PatternSet.ExcludesFile elements load patterns from a file. When the file is a relative path, it will be resolved relative to the project base directory in which the patternset is defined. Each line of this file is taken to be a pattern.

The number sign (#) as the first non-blank character in a line denotes that all text following it is a comment:

EventLog.cs
   # requires Mono.Posix
   SysLogEventLogImpl.cs
   # uses the win32 eventlog API
   Win32EventLogImpl.cs

Patterns can be grouped to sets, and later be referenced by their NAnt.Core.DataTypeBase.ID.

When used as a standalone element (global type), any properties that are referenced will be resolved when the definition is processed, not when it actually used. Passing a reference to a nested build file will not cause the properties to be re-evaluated.

To improve reuse of globally defined patternsets, avoid referencing any properties altogether.

Parameters

Attribute Type Description Required
id string
The ID used to be referenced later.
False
refid string
The ID to use as the reference.
False

Nested elements

<exclude>

Defines a single pattern for files to exclude.

Parameters

Attribute Type Description Required
name string
The name pattern to include/exclude.
True
if bool
If true then the pattern will be used; otherwise, skipped. The default is true.
False
unless bool
If false then the pattern will be used; otherwise, skipped. The default is false.
False

<exclude>

<excludesfile>

Loads multiple patterns of files to exclude from a given file, set using the NAnt.Core.Types.Pattern.PatternName parameter.

Parameters

Attribute Type Description Required
name string
The name pattern to include/exclude.
True
if bool
If true then the pattern will be used; otherwise, skipped. The default is true.
False
unless bool
If false then the pattern will be used; otherwise, skipped. The default is false.
False

<excludesfile>

<include>

Defines a single pattern for files to include.

Parameters

Attribute Type Description Required
name string
The name pattern to include/exclude.
True
if bool
If true then the pattern will be used; otherwise, skipped. The default is true.
False
unless bool
If false then the pattern will be used; otherwise, skipped. The default is false.
False

<include>

<includesfile>

Loads multiple patterns of files to include from a given file, set using the NAnt.Core.Types.Pattern.PatternName parameter.

Parameters

Attribute Type Description Required
name string
The name pattern to include/exclude.
True
if bool
If true then the pattern will be used; otherwise, skipped. The default is true.
False
unless bool
If false then the pattern will be used; otherwise, skipped. The default is false.
False

<includesfile>

Examples

Define a set of patterns that matches all .cs files that do not contain the text Test in their name.

<patternset id="non.test.sources">
        <include name="**/*.cs" />
        <exclude name="**/*Test*" />
    </patternset>

Define two sets. One holding C# sources, and one holding VB sources. Both sets only include test sources when the test property is set. A third set combines both C# and VB sources.

<patternset id="cs.sources">
        <include name="src/**/*.cs" />
        <include name="test/**/*.cs" if=${property::exist('test')}" />
    </patternset>
    
    <patternset id="vb.sources">
        <include name="src/**/*.vb" />
        <include name="test/**/*.vb" if=${property::exist('test')}" />
    </patternset>
    
    <patternset id="all.sources">
        <patternset refid="cs.sources" />
        <patternset refid="vb.sources" />
    </patternset>

Define a set from patterns in a file.

<patternset id="sources">
        <includesfile name="test.sources" />
        <includesfile name="non.test.sources" />
    </patternset>

Defines a patternset with patterns that are loaded from an external file, and shows the behavior when that patternset is passed as a reference to a nested build script.

External file "c:\foo\build\service.lst" holding patterns of source files to include for the Foo.Service assembly:

AssemblyInfo.cs
    *Channel.cs
    ServiceFactory.cs

Main build script located in "c:\foo\default.build":

<project name="main" default="build">
        <property name="build.debug" value="true" />
    
        <patternset id="service.sources">
            <include name="TraceListener.cs" if="${build.debug}" />
            <includesfile name="build/service.lst" />
        </patternset>
        
        <property name="build.debug" value="false" />
        
        <target name="build">
            <nant buildfile="service/default.build" inheritrefs="true" />
        </target>
    </project>

Nested build script located in "c:\foo\services\default.build" which uses the patternset to feed sources files to the C# compiler:

<project name="service" default="build">
        <target name="build">
            <csc output="../bin/Foo.Service.dll" target="library">
                <fileset basedir="src">
                    <patternset refid="service.sources" />
                </fileset>
            </csc>
        </target>
    </project>

At the time when the patternset is used in the "service" build script, the following source files in "c:\foo\services\src" match the defined patterns:

AssemblyInfo.cs
    MsmqChannel.cs
    SmtpChannel.cs
    ServiceFactory.cs
    TraceListener.cs

You should have observed that:

  • although the patternset is used from the "service" build script, the path to the external file is resolved relative to the base directory of the "main" build script in which the patternset is defined.
  • the "TraceListener.cs" file is included, even though the "build.debug" property was changed to false after the patternset was defined (but before it was passed to the nested build, and used).

See Also

  • NAnt.Core.Types.FileSet

Requirements

Assembly
NAnt.Core.dll
Namespace
NAnt.Core.Types