Creating the .targets file

The .targets file is where you specify what values you want your properties to have. In the vast majority of cases this will be determined by the value of the $(Configuration) built-in variable - this is what you have selected in the little combo-box at the top of Visual Studio:

This example .targets file contains virtually everything you need to know about how MSBuild processes these files:

<PropertyGroup>
    <!-- No condition and processed first means this Property Group specifies the defaults -->
    <csConnStr1>Data Source=localhost;Initial Catalog=Db1;Integrated Security=True;</csConnStr1>
    <csConnStr2>Data Source=localhost;Initial Catalog=Db2;Integrated Security=True;</csConnStr2>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)'=='Release'">
    <csConnStr1>Data Source=PRDSQL;Initial Catalog=Db1;Integrated Security=True;</csConnStr1>
    <csConnStr2>Data Source=PRDSQL;Initial Catalog=Db2;Integrated Security=True;</csConnStr2>
</PropertyGroup>

<!-- This is a call to String.Contains. You can use this technique to create "hierarchies" of build configurations.
     See http://msdn.microsoft.com/en-us/library/dd633440.aspx -->
<PropertyGroup>
    <IsEurope>$(Configuration.Contains("Europe"))</IsEurope>
</PropertyGroup>

<PropertyGroup Condition="'$(IsEurope)'=='true'">
    <csConnStr1>Data Source=DEVSQL_EUROPE;Initial Catalog=Db1;Integrated Security=True;</csConnStr1>
    <csConnStr2>Data Source=DEVSQL_EUROPE;Initial Catalog=Db2;Integrated Security=True;</csConnStr2>
</PropertyGroup>

<PropertyGroup>
    <prop1>Hello</prop1>
    <prop2>$(prop1) World!</prop2>
</PropertyGroup>

<!-- This example is how ConfigZilla sets the default document comment -->
<PropertyGroup>
    <czComment1 Condition="'$(czComment1)'==''"> Built using configuration $(Configuration) on machine $(ComputerName) by $(UserName) at $([System.DateTime]::Now.ToString("dd-MMM-yyyy HH:mm")) </czComment1>
</PropertyGroup>


<!-- You can import other MSBuild files. Normally this would be at the
     top (recall that files are processed top-to-bottom). -->
<Import Project="$(MSBuildProjectDirectory)\CommonSettings.targets" />

Using per-project targets

Normally you can simply define all your .targets under the Transforms folder in the ConfigZilla project, and because properties such as $(MSBuildProjectName) are evaluated in the context of the including project this is normally enough flexibility to achieve everything you want, for example you can given log files a unique name for each project using a construct such as this:

<filename>C:\temp\$(MSBuildProjectName).log</filename>

However, ConfigZilla will also look for a "Transforms" folder under each project that you are compiling, and will include those .targets files after the ones from ConfigZilla\Transforms. Should you need it, this gives you even more flexibility to override properties within each project (recall that properties are evaluated from top to bottom so the ones from your project will override the ConfigZilla properties).

Hints and tips


Table Of Contents