Welcome!

Microsoft Cloud Authors: Yeshim Deniz, Janakiram MSV, Andreas Grabner, Stackify Blog, Liz McMillan

Related Topics: Microsoft Cloud

Microsoft Cloud: Article

MSBuild - What It Does and What You Can Expect in the Future

The standard customizable build platform for the .NET Framework

In Visual Studio 2003 and earlier, the build process for Visual Basic and C# projects was hard-coded, and built into Visual Studio itself. The only build scripting tool that Microsoft offered was nmake, and a companion tool called build.exe that provided some support for concurrent builds. Visual Studio users whose build systems were based on makefiles had to maintain project files in parallel. For Visual Studio 2005, we thought it would be great if it was possible to completely customize the build process, and to build Visual Studio projects on machines that didn't even have Visual Studio installed, exactly the same as they built inside Visual Studio. We also wanted to be able to plug in reuseable build loggers and build steps.

MSBuild shipped in .NET Framework 2.0, and Visual Studio 2005 was based on it. To date we've only released build processes for Visual Basic and C#, but our vision is to make this truly the common build platform for Microsoft technologies. It's fair to say that internal Microsoft needs have motivated MSBuild almost as much as the needs of our customers. We're making exciting progress towards this goal, and we'll talk about our future plans later in this article.

MSBuild File Format
If you use Visual Studio 2005, you don't necessarily have to know anything about MSBuild syntax. Visual Studio's Visual Basic and C# project files are MSBuild format, and you can go right ahead and build them on machines without Visual Studio installed.

However if you want to customize the build process or create your own from scratch, you'll need to know something about the MSBuild syntax. We tried to make the file format as simple as possible, so it's easy to learn. It's XML, so any XML tool can work with it, and there are only a few different tags to know about. Let's do a quick tour.

PROPERTIES
MSBuild properties represent key/value pairs used during the build process. A property can only take one value at a time. Properties are always inside a PropertyGroup tag. If you open a Visual Basic or C# project file you'll see it uses properties to store whether the compiler should build with optimizations (the "Optimize" property), the output path (the "OutputPath" property), and what warning level it should use (the WarningLevelproperty). Think of properties as scalars. You can get their values with an expression like "$(PropertyName)".

ITEMS
MSBuild Item lists are just ordered bags, like arrays. An item list contains multiple MSBuild items of the same type. Each has a value and optional metadata. The syntax to access their values is different than properties: "@(ItemType)." The items in an item list are often file paths, but don't have to be - MSBuild doesn't care. Take a look at a Visual Basic or C# project again and you'll see items used to store all the source files (the "Compile" items) and references (the "Reference" items) in your project. They don't have to be hard-coded though. You can have MSBuild generate a list of items using wildcards such as "*.txt".

Like properties, items have an ItemGroup tag they always sit inside. Unlike properties, items can have arbitrary "metadata" attached to each one, which you can define with sub-elements below the item's element. Here's an example of metadata used to represent a Culture value on a resource item:

<ItemGroup>
   <Resource Include="Form1.en-CA.resx ">
     <Culture>en-CA</Culture>
   </Resource>
</ItemGroup>

Metadata is accessed with yet another kind of expression that looks like this "%(MetadataName)" or "%(ItemType.MetadataName)". There's also a variety of built-in metadata such as "Filename" and "Full-Path", which are useful if the item represents a file.

One item list can be converted to another by a transform operation. For example, if you write "@(Compile -> ‘%(Filename).obj'") you'll get the collection of "Compile" items, but the extension of each item will be changed to "obj". That can be useful for specifying the outputs of a target or constructing a command line string.

TARGETS
Targets are the unit of build: it's not possible to build only part of a target. This time open up your Microsoft.CSharp.targets file, which you'll find in the .NET Framework install folder, e.g., Windows\ Microsoft.NET\Framework\v2.0.50727. (By convention, targets are stored in files that have the ‘.targets' extension.) Now look for the ‘Target' tag. A target groups a collection of tasks together in a particular order to form a unit of build. For example, the "CoreCompile" target contains the "CSC" task that will invoke the C# compiler. Like every other MSBuild tag, Target tags can have conditions on them, but you can also add Input and Output attributes, which MSBuild will compare to see if the target is up-to-date already: if the target is up-todate, MSBuild will skip the tasks within it in part or entirely. A "DependsOnTargets" attribute lets you define what targets need to run beforehand so you can organize the order in which targets run. Targets can override previous targets with the same name. Targets have some other powers: partial incremental build and target batching; you can find out more about these on MSDN.

TASKS
Tasks are reuseable build actions that define the steps in a target. Tasks are always inside a target. Each task is implemented as a .NET class, and the parameters to a task - where you pass in the items and properties it needs - are just the .NET properties on that task. MSBuild comes with a set of built-in tasks like Delete, Message, GenerateResource (which is much like resgen.exe), plus tasks for the Visual Basic and C# compilers. There's also a special MSBuild task that runs MSBuild on another project. If those aren't enough, you can write your own - start by deriving from Task or ToolTask. Use a "UsingTask" tag to help MSBuild to find the assembly that implements your task. Tasks can also emit items and properties back into the build using Output sub-elements for subsequent tasks and targets to consume.

CONDITIONS
Every element in MSBuild syntax can have a Condition attribute. These have simple syntax something like conditions in XSL: logical operators "and", "or". parentheses, and so on. You can also use Choose, When, and Otherwise elements to make decisions. Here are some examples, so you can see what they look like:

<Compile Include="MyFile.cs" Condition="'$(Confi guration)'=='debug'"/>

In this example, the item "MyFile.cs" will be created only when the project is building in the debug configuration.

<Target Name="CreateOutputDirectory" Condition="!Exists(‘$(OutputDir)')" />

In this example, the target CreateOutputDirectory and the targets it depends on will run only if the output directory doesn't exist.


More Stories By Xin Yan

Xin Yan has been a software design engineer at Microsoft for over 7 years. He works on Visual Studio developer tools platform team.

More Stories By Dan Moseley

Dan Moseley is a software developer on the MSBuild and Visual Studio Project team.

Comments (1) View Comments

Share your thoughts on this story.

Add your comment
You must be signed in to add a comment. Sign-in | Register

In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.


Most Recent Comments
Paul 11/28/07 04:17:42 PM EST

I enjoyed the article on MSBuild, well written, concise, and interesting. But I was most interested in reading about Batching. In that section, you refer to an example using the Culture metadata, but I don't see the example you're referring to. Am I missing something?

@ThingsExpo Stories
"We've been engaging with a lot of customers including Panasonic, we've been involved with Cisco and now we're working with the U.S. government - the Department of Homeland Security," explained Peter Jung, Chief Product Officer at Pulzze Systems, in this SYS-CON.tv interview at @ThingsExpo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"We provide IoT solutions. We provide the most compatible solutions for many applications. Our solutions are industry agnostic and also protocol agnostic," explained Richard Han, Head of Sales and Marketing and Engineering at Systena America, in this SYS-CON.tv interview at @ThingsExpo, held June 6-8, 2017, at the Javits Center in New York City, NY.
SYS-CON Events announced today that Calligo, an innovative cloud service provider offering mid-sized companies the highest levels of data privacy and security, has been named "Bronze Sponsor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Calligo offers unparalleled application performance guarantees, commercial flexibility and a personalised support service from its globally located cloud plat...
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
"The Striim platform is a full end-to-end streaming integration and analytics platform that is middleware that covers a lot of different use cases," explained Steve Wilkes, Founder and CTO at Striim, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
"We are focused on SAP running in the clouds, to make this super easy because we believe in the tremendous value of those powerful worlds - SAP and the cloud," explained Frank Stienhans, CTO of Ocean9, Inc., in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
DX World EXPO, LLC., a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
SYS-CON Events announced today that DXWorldExpo has been named “Global Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.
SYS-CON Events announced today that Datera, that offers a radically new data management architecture, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Datera is transforming the traditional datacenter model through modern cloud simplicity. The technology industry is at another major inflection point. The rise of mobile, the Internet of Things, data storage and Big...
"MobiDev is a Ukraine-based software development company. We do mobile development, and we're specialists in that. But we do full stack software development for entrepreneurs, for emerging companies, and for enterprise ventures," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
While the focus and objectives of IoT initiatives are many and diverse, they all share a few common attributes, and one of those is the network. Commonly, that network includes the Internet, over which there isn't any real control for performance and availability. Or is there? The current state of the art for Big Data analytics, as applied to network telemetry, offers new opportunities for improving and assuring operational integrity. In his session at @ThingsExpo, Jim Frey, Vice President of S...
SYS-CON Events announced today that DXWorldExpo has been named “Global Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Digital Transformation is the key issue driving the global enterprise IT business. Digital Transformation is most prominent among Global 2000 enterprises and government institutions.
In his opening keynote at 20th Cloud Expo, Michael Maximilien, Research Scientist, Architect, and Engineer at IBM, discussed the full potential of the cloud and social data requires artificial intelligence. By mixing Cloud Foundry and the rich set of Watson services, IBM's Bluemix is the best cloud operating system for enterprises today, providing rapid development and deployment of applications that can take advantage of the rich catalog of Watson services to help drive insights from the vast t...
SYS-CON Events announced today that EnterpriseTech has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. EnterpriseTech is a professional resource for news and intelligence covering the migration of high-end technologies into the enterprise and business-IT industry, with a special focus on high-tech solutions in new product development, workload management, increased effic...
SYS-CON Events announced today that Massive Networks, that helps your business operate seamlessly with fast, reliable, and secure internet and network solutions, has been named "Exhibitor" of SYS-CON's 21st International Cloud Expo ®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. As a premier telecommunications provider, Massive Networks is headquartered out of Louisville, Colorado. With years of experience under their belt, their team of...
SYS-CON Events announced today that Cloud Academy named "Bronze Sponsor" of 21st International Cloud Expo which will take place October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara, CA. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud com...
SYS-CON Events announced today that Cloudistics, an on-premises cloud computing company, has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Cloudistics delivers a complete public cloud experience with composable on-premises infrastructures to medium and large enterprises. Its software-defined technology natively converges network, storage, compute, virtualization, and ...
SYS-CON Events announced today that CHEETAH Training & Innovation will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct. 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CHEETAH Training & Innovation is a cloud consulting and IT training firm specializing in improving clients cloud strategies and infrastructures for medium to large companies.
SYS-CON Events announced today that Datanami has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Datanami is a communication channel dedicated to providing insight, analysis and up-to-the-minute information about emerging trends and solutions in Big Data. The publication sheds light on all cutting-edge technologies including networking, storage and applications, and thei...
The current age of digital transformation means that IT organizations must adapt their toolset to cover all digital experiences, beyond just the end users’. Today’s businesses can no longer focus solely on the digital interactions they manage with employees or customers; they must now contend with non-traditional factors. Whether it's the power of brand to make or break a company, the need to monitor across all locations 24/7, or the ability to proactively resolve issues, companies must adapt to...