Welcome!

Microsoft Cloud Authors: Janakiram MSV, Yeshim Deniz, David H Deans, Andreas Grabner, Stackify Blog

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
SYS-CON Events announced today that Interface Corporation will exhibit at the Japan External Trade Organization (JETRO) Pavilion 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. Interface Corporation is a company developing, manufacturing and marketing high quality and wide variety of industrial computers and interface modules such as PCIs and PCI express. For more information, visit http://www.i...
SYS-CON Events announced today that Fusic will exhibit at the Japan External Trade Organization (JETRO) Pavilion 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. Fusic Co. provides mocks as virtual IoT devices. You can customize mocks, and get any amount of data at any time in your test. For more information, visit https://fusic.co.jp/english/.
SYS-CON Events announced today that TMC has been named “Media Sponsor” of SYS-CON's 21st International Cloud Expo and Big Data at Cloud Expo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Global buyers rely on TMC’s content-driven marketplaces to make purchase decisions and navigate markets. Learn how we can help you reach your marketing goals.
SYS-CON Events announced today that Enroute Lab will exhibit at the Japan External Trade Organization (JETRO) Pavilion 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. Enroute Lab is an industrial design, research and development company of unmanned robotic vehicle system. For more information, please visit http://elab.co.jp/.
There is huge complexity in implementing a successful digital business that requires efficient on-premise and cloud back-end infrastructure, IT and Internet of Things (IoT) data, analytics, Machine Learning, Artificial Intelligence (AI) and Digital Applications. In the data center alone, there are physical and virtual infrastructures, multiple operating systems, multiple applications and new and emerging business and technological paradigms such as cloud computing and XaaS. And then there are pe...
SYS-CON Events announced today that Daiya Industry will exhibit at the Japan External Trade Organization (JETRO) Pavilion 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. Daiya Industry specializes in orthotic support systems and assistive devices with pneumatic artificial muscles in order to contribute to an extended healthy life expectancy. For more information, please visit https://www.daiyak...
Real IoT production deployments running at scale are collecting sensor data from hundreds / thousands / millions of devices. The goal is to take business-critical actions on the real-time data and find insights from stored datasets. In his session at @ThingsExpo, John Walicki, Watson IoT Developer Advocate at IBM Cloud, will provide a fast-paced developer journey that follows the IoT sensor data from generation, to edge gateway, to edge analytics, to encryption, to the IBM Bluemix cloud, to Wa...
In his session at @ThingsExpo, Greg Gorman is the Director, IoT Developer Ecosystem, Watson IoT, will provide a short tutorial on Node-RED, a Node.js-based programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using a wide range of nodes in the palette that can be deployed to its runtime in a single-click. There is a large library of contributed nodes that help so...
With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
SYS-CON Events announced today that App2Cloud 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. App2Cloud is an online Platform, specializing in migrating legacy applications to any Cloud Providers (AWS, Azure, Google Cloud).
Agile has finally jumped the technology shark, expanding outside the software world. Enterprises are now increasingly adopting Agile practices across their organizations in order to successfully navigate the disruptive waters that threaten to drown them. In our quest for establishing change as a core competency in our organizations, this business-centric notion of Agile is an essential component of Agile Digital Transformation. In the years since the publication of the Agile Manifesto, the conn...
WebRTC is great technology to build your own communication tools. It will be even more exciting experience it with advanced devices, such as a 360 Camera, 360 microphone, and a depth sensor camera. In his session at @ThingsExpo, Masashi Ganeko, a manager at INFOCOM Corporation, will introduce two experimental projects from his team and what they learned from them. "Shotoku Tamago" uses the robot audition software HARK to track speakers in 360 video of a remote party. "Virtual Teleport" uses a mu...
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...
Mobile device usage has increased exponentially during the past several years, as consumers rely on handhelds for everything from news and weather to banking and purchases. What can we expect in the next few years? The way in which we interact with our devices will fundamentally change, as businesses leverage Artificial Intelligence. We already see this taking shape as businesses leverage AI for cost savings and customer responsiveness. This trend will continue, as AI is used for more sophistica...
SYS-CON Events announced today that SourceForge 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. SourceForge is the largest, most trusted destination for Open Source Software development, collaboration, discovery and download on the web serving over 32 million viewers, 150 million downloads and over 460,000 active development projects each and every month.
SYS-CON Events announced today that Massive Networks 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. Massive Networks mission is simple. To help your business operate seamlessly with fast, reliable, and secure internet and network solutions. Improve your customer's experience with outstanding connections to your cloud.
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 WineSOFT 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. Based in Seoul and Irvine, WineSOFT is an innovative software house focusing on internet infrastructure solutions. The venture started as a bootstrap start-up in 2010 by focusing on making the internet faster and more powerful. WineSOFT’s knowledge is based on the expertise of TCP/IP, VPN, SS...
SYS-CON Events announced today that Akvelon 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. Akvelon is a business and technology consulting firm that specializes in applying cutting-edge technology to problems in fields as diverse as mobile technology, sports technology, finance, and healthcare.
SYS-CON Events announced today that TechTarget 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. TechTarget storage websites are the best online information resource for news, tips and expert advice for the storage, backup and disaster recovery markets.