How To Write A Simple Msbuild Project
How To Write A Simple Msbuild Project
This topic explains how to write a simple MSBuild project file that uses a task to compile a project containing one file. This
example covers two project files, one that compiles a Visual Basic source file, and one that compiles a Visual C# source file.
The complete source code for each project file is available at the end of the topic.
<Project DefaultTargets="Compile"
xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
...
</Project>
This Project element specifies that the Compiletarget is the default target.
Adding Properties
The PropertyGroup element is used to group Property elements that contain values that are referenced several times in
the project file or to set the values for properties that are used in several configurations. For more information on
properties, see MSBuild Properties and Property Element (MSBuild).
To add properties
1. Add a PropertyGroup element. Property elements must be children of a PropertyGroup element.
2. Add one or more properties as children of the PropertyGroup element.
Property elements do not use the word "Property" as their element name. Rather, the element name is based on
the name of the property, and the value of the property is the text value of the element. For example, the following
XML sets the value of the appnameproperty to HelloWorldCS.
<PropertyGroup>
<appname>HelloWorldCS</appname>
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx
1/8
8/3/2014
</PropertyGroup>
Adding Items
The ItemGroup element is used to group Item elements, which define inputs into the build system. For more information
on items, see MSBuild Items and Item Element (MSBuild).
To add items
1. Add an ItemGroup element. Item elements must be children of an ItemGroup element.
2. Add one or more items as children of the ItemGroup element. Use the Include attribute to specify a file to include
as an input to the build system.
Item elements do not use the word "Item" as their element name. Rather, the element name is based on the item
collection that contains the individual item. For example, if a project contains several source files and several
resource files, you may want to put all source files into an item collection named SourceFileand all resource files
into a collection named ResourceFile. This makes it easier to pass groups of items into tasks.
For example, the following XML includes the file consolehwcs1.csin the CSFileitem collection.
<ItemGroup>
<CSFile Include="consolehwcs1.cs"/>
</ItemGroup>
For a VB project, you may want to name the item collection something more descriptive, such as VBFile.
<ItemGroup>
<VBFile Include="consolehwvb1.vb"/>
</ItemGroup>
Adding Targets
Targets group tasks together in a particular order and expose sections of the project file as entry points into the build
process. The Target element is used to define targets, and contains a set of tasks that MSBuild executes sequentially. For
more information on targets, see MSBuild Targets and Target Element (MSBuild).
To add a target
Add a Target element, with a name specified by the Name attribute. For example, the following target is named
Compile.
<Target Name="Compile">...</Target>
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx
2/8
8/3/2014
Adding Tasks
Tasks are units of executable code used by MSBuild to perform build operations. The Task element specifies the task to
run and the parameters to pass to the task. A library of common tasks is provided with MSBuild, and you can also write
tasks yourself. For more information on tasks, see MSBuild Tasks and Task Element (MSBuild). For more information on
the common tasks provided with MSBuild, see MSBuild Task Reference.
To compile a Visual C# project, use the Csc Task, and to compile a Visual Basic project, use the Vbc Task. Both of these
tasks are part of the library of common tasks provided with MSBuild.
To add a task
1. Add a task as a child of a Target element.
Task elements do not use the word "Task" as their element name. Rather, the element name is the name of the
task itself.
2. Add the necessary attributes for the task.
Some tasks accept parameters, which are passed to the task through attributes on the Task element.
The following example compiles a Visual C# project, passing the CSFileitem collection to the Sourcesparameter
of the task.
Note:
Use the @()notation to specify an entire item collection as a parameter.
<CSC Sources="@(CSFile)">...</CSC>
The following example compiles a Visual Basic project, passing the VBFileitem collection to the Sources
parameter of the task.
<VBC Sources="@(VBFile)">...</VBC>
3/8
8/3/2014
Set the TaskParameter attribute of the Output element equal to the attribute that you want to use as output, and
the ItemName or PropertyName attribute that you want to store the value in.
The following example compiles a Visual C# project, passing the CSFileitem collection to the Sourcesparameter
of the task, and stores the value of the OutputAssembly parameter in the EXEFile item collection.
Note:
Use the $()notation to specify a property as a parameter.
<CSC
Sources="@(CSFile)"
WarningLevel="$(WarningLevel)">
<Output
TaskParameter="OutputAssembly"
ItemName="EXEFile"/>
</CSC>
The following example compiles a Visual Basic project, passing the VBFileitem collection to the Sources
parameter of the task, and stores the value of the OutputAssemblyparameter in the EXEFileitem collection.
<VBC
Sources="@(VBFile)"
WarningLevel="$(WarningLevel)">
<Output
TaskParameter="OutputAssembly"
ItemName="EXEFile"/>
</VBC>
Adding Messages
Some status information, such as the current target and task, is automatically logged by MSBuild as a build progresses.
In addition, you can use the Message task to provide additional information.
To add a message
1. Add a Message task as a child of a Target element.
2. Add the message text to the Text parameter of the Message task.
3. The following example creates a message that writes the name of the item collection created with the Output
element in the previous example.
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx
4/8
8/3/2014
To add comments
1. Type <!-- to begin a comment.
2. Write the text of the comment.
3. Type --> to end a comment.
The following example adds a comment in an XML file.
Building a Project
MSBuild project files are built using MSBuild.exe. For more information on MSBuild.exe, see MSBuild Command Line
Reference.
To build a project
Navigate to the directory that contains the project file and type the following:
Example
The following example shows a project file that compiles a Visual C# application and logs a message containing the
output file name.
5/8
8/3/2014
</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>
The following example shows a project file that compiles a Visual Basic application and logs a message containing the
output file name.
See Also
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx
6/8
8/3/2014
Concepts
MSBuild Overview
MSBuild Project File Schema Reference
MSBuild
Other Resources
MSBuild Concepts
MSBuild Reference
Community Additions
MS Build Automation
Hello ,
This is regarding the MS Build Automation .
The problem which we are facing here is that we are not able to generate the build file with the code mentioned below.
When we are running the project in command prompt , its giving the message that Build Succeeded but the build file is not coming .
I am sending you the procedure along with the code .
Steps carried out in Automation process:
1.
Create a new project.
2.
3.
4.
6.
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx
7/8
8/3/2014
Msbuild/target:build
This will give us the message that Build succeeded.
7.
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx
8/8