Write a Simple Project
A project file for
MSBuild has the following core XML elements, which are used in this example:
Project,
ItemGroup,
PropertyGroup,
Target,
Task, and
Output.
To get you started with project files for
MSBuild, this example builds a "Hello World" console application.
Project Element
The Project element is the root element of a project file. It can contain version information for
MSBuild and specify the default set of build tasks (the default targets) for the project. For example, the following Project element specifies that the Compile target is the default:
<Project DefaultTargets="Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
</Project>
*PropertyGroup* Element
The
*PropertyGroup* element can be used to group variable
Properties that are referenced several times in the project file (for example, the location of a temporary directory) or to set the values for properties that are used in several configurations (for example, a Debug build and a Release build). In the sample file, the first use is illustrated with the appname property. The appname property is declared:
<PropertyGroup> <appname>HelloWorldCS</appname> </PropertyGroup> then referenced in the Target element by placing the property key in parentheses after $:
<Target Name="Build" Inputs="@(Source)" Outputs="$(appname).exe"/>
*ItemGroup* Element
The
*ItemGroup* element can be used to group Items. Items are the inputs to a build. Each Item element must have a name, which is used to group items, for example
<CSFile ... /> identifies Visual C# files and
<VBFile ... /> identifies Visual Basic files. The individual items are declared using the
Include attribute, for example:
<CSFile Include="consolehwcs1.cs"/>
or
<VBFile Include="consolehwvb1.vb"/>
If an item is not in the same directory as the project file, you must specify the full path to the file or the relative path, for example,
..\..\AssemblyInfo.cs.
Target Element
The
*Target* element defines how the project is built. It contains a set of tasks that
MSBuild runs sequentially. One target can depend on another target. For example, if a project contains a target for compiling and a target for deployment, the deployment target is dependent on the compilation target because the project must be compiled before it can be deployed. A target must have a
Name attribute.
<Target Name="Compile">...</Target>
Task Element
The
Task element specifies the code that will run during the build process. It must contain the name of the task that you want to run. A library of common tasks is provided with
MSBuild and you can also write tasks yourself. The two tasks from the library that are used in this example are the Visual C# compiler (Csc) and the Visual Basic compiler (Vbc). These tasks have attributes that you can set in the project file: in this example, the source files for each task are specified as the collection of files listed in the
Item element. You use the @ notation to specify an array of all the files contained within an
Item as inputs for a build. This notation can be used whether you list all files separately or use wildcards.
<CSC Sources="@(CSFile)">...</CSC>
<VBC Sources="@(VBFile)">...</VBC>
Output Element
Some task attributes can be defined for the task outputs so that those outputs can be referenced later in the project file. You can assign the output to a collection of items or a single property. In the same way that inputs to a build can be identified as, for example,
CSFile, the outputs can be identified as, for example,
EXEFile. In this example, the
*OutputAssembly* attribute of the Csc and Vbc tasks is set to the name of the executable file that is created and the file type is [EXEFile:]
<CSC OutputAssembly="$(appname).exe" >
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</CSC>
Message Element
Some status information is automatically logged by
MSBuild as a build progresses, for example, the current target and task. In addition, you can use the
Message element to define other information that you want to log. In this example, the name of the output file is logged by listing the output item or items of the file type [EXEFile:]
<Message Text="The output file is @(EXEFile)"/>
Adding Comments to a Project File
To add comments to your project files, use the standard format for comments in an XML file:
<!-- Your comment -->
Building a Project
To build one of the projects from the command line, navigate to the directory that contains the project file and type:
msbuild <file name>.proj
Project Files
Files
Download Code - C#Download Code - VB
Visual C# example
<Project
DefaultTargets = "Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup> <!-- Set the application name as a property -->
<appname>HelloWorldCS</appname> </PropertyGroup> <!-- Specify the inputs by type and file name -->
<ItemGroup> <CSFile Include = "consolehwcs1.cs"/>
</ItemGroup> <Target Name = "Compile">
<!-- Run the Visual C# compilation using input files of type
CSFile -->
<CSC Sources = "@(CSFile)"
OutputAssembly = "$(appname).exe">
<!-- Set the
OutputAssembly attribute of the CSC task to the name of the executable file that is created -->
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</CSC>
<!-- Log the file name of the output file -->
<Message Text="The output file is @(EXEFile)"/>
</Target>
</Project>
Visual Basic example
<Project
DefaultTargets = "Compile" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<PropertyGroup> <!-- Set the application name as a property -->
<appname>HelloWorldVB</appname> </PropertyGroup> <ItemGroup> <!-- Specify the inputs by type and file name -->
<VBFile Include = "consolehwvb1.vb"/>
</ItemGroup> <Target Name = "Compile">
<!-- Run the Visual Basic compilation using input files of type
VBFile -->
<VBC Sources = "@(VBFile)"
OutputAssembly = "$(appname).exe">
<!-- Set the
OutputAssembly attribute of the VBC task to the name of the executable file that is created -->
<Output
TaskParameter = "OutputAssembly"
ItemName = "EXEFile" />
</VBC>
<!-- Log the file name of the output file -->
<Message Text="The output file is @(EXEFile)"/>
</Target>
</Project>