Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does Microsoft.Common.props do

Tags:

c#

msbuild

csproj

I noticed that when I create a project using Class Library template the .csproj contains import of Microsoft.Common.props

<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />

However when I create a project using Unit Test Project template it is not present.

So what does Microsoft.Common.props do? How does it benefit the project?

like image 917
user1121956 Avatar asked Apr 27 '18 20:04

user1121956


People also ask

What is MSBuild used for?

The Microsoft Build Engine is a platform for building applications. This engine, which is also known as MSBuild, provides an XML schema for a project file that controls how the build platform processes and builds software. Visual Studio uses MSBuild, but MSBuild doesn't depend on Visual Studio.

What does Directory build props do?

Directory. Build. props is a user-defined file that provides customizations to projects under a directory.

What is MSBuild target?

A target element can have both Inputs and Outputs attributes, indicating what items the target expects as input, and what items it produces as output. If all output items are up-to-date, MSBuild skips the target, which significantly improves the build speed. This is called an incremental build of the target.

What is UseAppHost?

UseAppHost. The UseAppHost property controls whether or not a native executable is created for a deployment. A native executable is required for self-contained deployments. In . NET Core 3.0 and later versions, a framework-dependent executable is created by default.


1 Answers

It does a lot of things and brings in a part of the build logic.

In fact, there are two important imports for "standard" .NET projects: Microsoft.Common.props and Microsoft.Common.targets (latter one may be implicitly included from a project type specific import file).

The basic idea is to split build logic into two parts, one that is imported before your project's contents (which will be the .props) and one that is included afterwards (.targets).

The Microsoft.Common.props will define some properties based on conventions - e.g. set defaults for the current configuration (e.g. build for Debug if no configuration was specified when building from the command line).

It also imports other files installed as extensions to the msbuild/vs tools installation or the project - e.g. NuGet 4+ makes uses of this for PackageReference style projects.

Once the Microsoft.Common.props import has set up all the defaults, it is your project's turn to change the defaults according to user choices (and project templates) as well as define some other properties and items needed for the build process.

Your project must then import a .targets file that defines the msbuild logic needed to perform a build of the project. This is done through Microsoft.Common.targets (and the files it chooses to import). Anything that needs to override logic coming from this file needs to be specified after this import - this is why VS' project templates have a commented out area for custom AfterBuild targets. Since AfterBuild is already defined via the common targets, you will need to override it after this import (or use a custom name and add AfterTargets="AfterBuild" which is preferred in newer MSBuild versions).

The .props/.targets split for defaults and logic is also used a lot in custom build extensions and a naming convention for msbuild files that should be imported at the top (.props) and the bottom (.targets) of projects.

In "SDK-based" projects which have been introduced for .NET Core but can also be used with .NET Framework, this concept is expanded upon by importing Sdk.props and Sdk.targets files - before and after the project contents respectively. A set of convention helps locating these files and even allows you to omit <Import> elements by specifying an attribute on the project file: <Project Sdk="Microsoft.NET.Sdk">...</Project>. These imports will define even more defaults than Microsoft.Common.props, allowing for very small and more human readable project files. (currently, only .NET Core / .NET Standard and ASP.NET Core project templates use this format since VS 2017 uses a different project system for it than for classic csproj files)

like image 71
Martin Ullrich Avatar answered Oct 23 '22 03:10

Martin Ullrich