Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a custom Sdk in .Net Core

Tags:

In .Net core projects, there is a .csproj file and inside it the first line specifies the SDK you are targeting in that project, similar to:

<Project Sdk="Microsoft.NET.Sdk.Web">

My question is whether it is possible to create a whole new SDK (maybe by extending the Microsoft.NET.Sdk.Web) and then using this new custom SDK in some other projects, in a way that the new project would have something like this in its .csproj file:

<Project Sdk="My.Own.Sdk">

How could something like this be done?

like image 377
Behrooz Avatar asked May 16 '19 20:05

Behrooz


2 Answers

Yes, you can do it for fun, but there is unlikely to be an official support for publishing.

How to: Use MSBuild project SDKs

It shows the how msbuild loads the props and targets, and you can find the SDK.props and SDK.targets file in the .NET SDK installing folder (represented as dotnet, for example C:\Program Files\dotnet on Windows)

What you need to do to create your own sdk is to make a folder inside dotnet/sdk similarly to any other SDKs look like.

And make the SDK.props and SDK.targets files with path dotnet/sdk/<SDK_VERSION>/Sdks/<YOUR_SDK_NAME>/Sdk, as the entry that reference your props and targets to customize your build steps,

If there is any other file needed by convention, create your new one to mimic the existings.

But still, the officially supported way to customize the callers' build steps is to create a nuget package with a convention subfolder build and the props and targets files would be automatically into the project that referenced the package.

like image 136
Alsein Avatar answered Nov 15 '22 05:11

Alsein


Yes, this is definitely possible, and yes, it can be even published. You can find inspiration at Microsoft.Build.Traversal, for example.

SDK is, basically, nuget package, that has two files, Sdk\Sdk.props and Sdk\Sdk.targets.

If you publish NuGet package with such files to NuGet.org, you may then reference it as

<Project Sdk="YourNugetName/1.0.0">
</Project>

Where the 1.0.0 is your version.

For testing, the Sdk may also contain (fully qualified) path to directory that contains the same content as the nuget package, and just running dotnet build consumes the defined SDK, and runs target Build on it.

like image 36
nothrow Avatar answered Nov 15 '22 05:11

nothrow