Sunday, 19 May 2019

Adding license details to NuGet packages

I've always tried to make sure I add license details to my open source projects, especially when publishing them to NuGet. Previously, this was done by adding a <licenseUrl> element to your .nuspec file, which would allow users to see license details when downloading packages.

When using the csproj 2017 format (so all .NET Core projects), you could have the dotnet pack command automatically build your nuget package. To populate the license url, you just had to add the following to your project file:

<PropertyGroup>
  <PackageLicenseUrl>https://opensource.org/licenses/MIT</PackageLicenseUrl>
</PropertyGroup>

Starting sometime in 2018, I noticed that my builds started throwing a new warning:

warning NU5125: The 'licenseUrl' element will be deprecated. Consider using the 'license' element instead.

I just assumed that the nuspec format was altered, and the dotnet tool will eventually catch up. But I should have known better - of course the change is deeper than that. Half a year later, the warning is still there, so I decided to check it out, and found this issue on GitHub discussing it: https://github.com/NuGet/Home/issues/7509

Which led me to their wiki, describing the changes to the nuspec/nupkg files regarding adding license details: https://github.com/NuGet/Home/wiki/Packaging-License-within-the-nupkg

Instead of just a single URL, you could either add a license file and point the package to that, or you could add a license expression, where you could describe a combination rule using several well known licenses. In the same article, they also described how to update your csproj file to use the new license field.

In my case, to link to the MIT license, I had to replace the PackageLicenseUrl field with the following:

<PropertyGroup>
  <PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

Moral of the story? Never assume, and check the docs more thoroughly