An MSBuild SDK package that provides package reference support and dependency management to the projects.
It adds a Restore
target which facilitates downloading and referencing shared libraries from a remote server.
It also adds a Pack
target which facilitates creation of such packages using project outputs with pre-determined layouts.
Furthermore, it can be used either directly on your project (via the <Sdk/>
element) or in-directly (via other SDKs which refer to it internally).
It's basically an SDK wrapper around Restore targets (NuGet.Build.Tasks
) and Pack targets (NuGet.Build.Tasks.Pack
) optimized for SDK-style projects.
Visual Studio v15.6+ includes support for SDK's resolved from NuGet. That makes using the custom SDKs much easier.
See Using MSBuild project SDKs guide on Microsoft Docs for more information on how project SDKs work and how project SDKs are resolved.
-
Create a new project
- from
dotnet new
templates. - With your existing SDK-style project.
- from
-
Either keep the default or replace
Microsoft.NET.Sdk
withMSBuild.NET.Sdk
. This is usually done in the project's top-levelSdk
attribute. -
Then, Add an SDK reference to
NuGet.Build.Sdk
through<Sdk Name="..."/>
element directly under the<Project>
element. If there are any properties, needed to be set before the imports, those can be placed just above the<Sdk/>
element. -
You have to tell MSBuild that the
Sdk
should resolve from NuGet by- Adding a
global.json
containing the SDK name and version. - Adding a
Version
attribute with version info to theSdk
element.
- Adding a
-
Then you can add package specific properties and items (though some are included by default) and pack the project into a NuGet package.
The final project should look like this:
<Project Sdk="MSBuild.NET.Sdk">
<PropertyGroup>
<!-- Any Sdk-specific overridden properties -->
<ImportDirectoryPackagesProps>false</ImportDirectoryPackagesProps>
</PropertyGroup>
<Sdk Name="NuGet.Build.Sdk">
<PropertyGroup>
<PackageId>Vendor.Awesome.Package</PackageId>
<!-- Other Package-specific properties -->
</PropertyGroup>
<!-- Other properties, items and targets -->
</Project>
You can put the SDK version in the global.json
file next to your solution:
{
"msbuild-sdks": {
"MSBuild.NET.Sdk": "1.0.0",
"NuGet.Build.Sdk": "1.0.0"
}
}
Then, all of your project files, from that directory forward, uses the version from the global.json
file.
This would be a preferred solution for all the projects in your solution.
Then again, you might want to override the version for just one project OR if you have only one project in your solution (without adding global.json
), you can do so like this:
<Project Sdk="MSBuild.NET.Sdk/1.0.0">
<PropertyGroup>
<!-- Any Sdk-specific overridden properties -->
<ImportDirectoryPackagesProps>false</ImportDirectoryPackagesProps>
</PropertyGroup>
<Sdk Name="NuGet.Build.Sdk" Version="1.0.0">
<PropertyGroup>
<PackageId>Vendor.Awesome.Package</PackageId>
<!-- Other Package-specific properties -->
</PropertyGroup>
<!-- Other properties, items and targets -->
</Project>
That's it! You do not need to specify any default properties or items as they'll be automatically defined.
After that, you can use the Restore
, Build
, Pack
targets to restore packages, build the project and create NuGet packages: e.g., msbuild -t:Pack ...
.
- Please note that these targets are not exactly same as the ones that ship with Visual Studio or .NET SDK. There are few tweaks applied on top of them to ensure some long-standing issues are fixed that weren't possible before due to compatibility.
- As such there's slight behavioral differences between the official version and this one. It mostly applies to projects that didn't override the defaults.