0% found this document useful (0 votes)
150 views

How To Write A Simple Msbuild Project

This document explains how to write a simple MSBuild project file that compiles source code using tasks. It covers adding elements like Project, Properties, Items, Targets, Tasks, and Messages. Examples are provided for compiling C# and VB source files.

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
150 views

How To Write A Simple Msbuild Project

This document explains how to write a simple MSBuild project file that compiles source code using tasks. It covers adding elements like Project, Properties, Items, Targets, Tasks, and Messages. Examples are provided for compiling C# and VB source files.

Uploaded by

Manoj Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

8/3/2014

How to: Write a Simple MSBuild Project

How to: Write a Simple MSBuild Project


Visual Studio 2008

11 out of 153 rated this helpful

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.

Adding the Project Element


The Project element is the root element of an MSBuild project file and. It can specify the default set of targets for the
project as well as the initial target to run first every time the project is built. For more information, see Project Element
(MSBuild).

To add a Project element


Add XML similar to the following:

<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

How to: Write a Simple MSBuild Project

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

How to: Write a Simple MSBuild Project

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>

Adding Task Outputs


Some task attributes can be defined for the task outputs so that those outputs can be referenced later in the project file.
The Output element is used to specify task outputs, and can assign the output to either an item collection or a property.
For more information on task outputs, see MSBuild Tasks and Output Element (MSBuild).

To add task outputs


Add an Output element as a child of a Task element.
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx

3/8

8/3/2014

How to: Write a Simple MSBuild Project

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.

<Message Text="The output file is @(EXEFile)"/>

http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx

4/8

8/3/2014

How to: Write a Simple MSBuild Project

Adding Comments to a Project File


MSBuild project file comments are written in the standard format for comments in an XML file.

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.

<!-- Your comment -->

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:

msbuild <file name>.proj

Example
The following example shows a project file that compiles a Visual C# application and logs a message containing the
output file name.

<Project DefaultTargets = "Compile"


xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<!-- Set the application name as a property -->
<PropertyGroup>
<appname>HelloWorldCS</appname>
</PropertyGroup>
<!-- Specify the inputs by type and file name -->
<ItemGroup>
<CSFile Include = "consolehwcs1.cs"/>
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx

5/8

8/3/2014

How to: Write a Simple MSBuild Project

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

<Project DefaultTargets = "Compile"


xmlns="http://schemas.microsoft.com/developer/msbuild/2003" >
<!-- Set the application name as a property -->
<PropertyGroup>
<appname>HelloWorldVB</appname>
</PropertyGroup>
<!-- Specify the inputs by type and file name -->
<ItemGroup>
<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>

See Also
http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx

6/8

8/3/2014

How to: Write a Simple MSBuild Project

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.

Unload the project .

3.

Edit the project.

4.

Comment the previous code and write the following code :

<?xml version="1.0" encoding="utf-8">


<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Compile Include="Form1.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Form1.Designer.cs">
<DependentUpon>Form1.cs</DependentUpon>
</Compile>
<Compile Include="Program.cs" />
</ItemGroup>
<Target Name = "Build">
<Csc Sources ="@(Compile)"/>

<Message Text="The output file is @(EXEFile)"/>


</Target>
</Project>
5.

Save the project .

6.

In the visual studio command prompt , run the project using :

http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx

7/8

8/3/2014

How to: Write a Simple MSBuild Project

Msbuild/target:build
This will give us the message that Build succeeded.
7.

But the build file is not coming.


Kindly help me with this problem and send me some relevant documents related to the same.
nehabefrenz
4/19/2011

2014 Microsoft. All rights reserved.

http://msdn.microsoft.com/en-us/library/ms171479(d=printer).aspx

8/8

You might also like