Skip to content

Instantly share code, notes, and snippets.

@bash
Last active Jan 19, 2021
Embed
What would you like to do?
A Hitchhiker's Guide to MSBuild, the .NET SDK, and Everything

Resources

Additional MSBuild SDKs

Articles

Configuration

Items

ProjectReference

  • Private: When set to "false" the assembly is not copied to the output directory.

PackageReference

ContentWithTargetPath

This item allows one to customize the path in the output directory.

<ContentWithTargetPath Update="Gui\Templates\**\*.*">
	<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
	<TargetPath>Templates\%(RecursiveDir)%(Filename)%(Extension)</TargetPath>
</ContentWithTargetPath>

InternalsVisibleTo

<ItemGroup>
	<InternalsVisibleTo Include="Example.Tests" />
</ItemGroup>

Tips and Tricks when using .msbuildproj

Error: The FrameworkReference 'NETStandard.Library' was not recognized

<PropertyGroup Condition="$(MSBuildProjectExtension) == '.msbuildproj'">
    <!-- This suppresses a build error that only happens in Visual Studio. See https://github.com/dotnet/sdk/issues/3329 -->
    <GenerateErrorForMissingTargetingPacks>false</GenerateErrorForMissingTargetingPacks>
</PropertyGroup>

Customizing dotnet run

dotnet run can be customized throught MSBuild by setting one or more of the following properties:

Property Name Description Default for .NET Core*
RunCommand Overrides the commmand of the executed process. dotnet.exe
RunArguments Overrides all arguments passed to the exeuted process. -
RunWorkingDirectory Configures the working directory. Wins over StartWorkingDirectory project directory
StartArguments Appends to the arguments passed to the executed process. "<AssemblyName>.dll"
StartWorkingDirectory Configures the working directory. -

* Varies depending on your settings. See Microsoft.NET.Sdk.targets

Use Case 1: Run something completely custom

Override both RunCommand and RunArguments:

<PropertyGroup>
	<RunCommand>echo</RunCommand>
	<RunArguments>&quot;Hello World&quot;</RunArguments>
</PropertyGroup>

Use Case 2: Add CLI arguments for your executable

Override StartArguments:

<PropertyGroup>
	<StartArguments>--logging-level Verbose</RunCommand>
</PropertyGroup>

Creating a Source Code Package

Example csproj:

<!-- ... -->
<ItemGroup>
    <Content Include="Metadata\**\*.cs">
        <BuildAction>Compile</BuildAction>
    </Content>
</ItemGroup>
<!-- ... -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment