Licensing and packaging NUGET packages
When building and publishing packages in .NET you will probably sometimes see following error.
error NU5030: The license file 'license.txt' does not exist in the package.
To fix this error I usually add a single license.txt file in the root of the source (i.e.: /src) of my solution. Then I reference the license file from the package project:
<PackageLicenseFile>license.txt</PackageLicenseFile>
This line suggest the dotnet.exe build process to include the license,txt file in the package. Additionally, you have to add a following item group to the project file:
<ItemGroup>
<Content Include="..\..\license.txt"
Link="license.txt"
Pack="true"
PackagePath="license.txt"/>
</ItemGroup>
This includes the license.txt file fron the folder "../../" (which is '/src') as a linked file. Note, that Include attribute is set to the physical relative location of the file and the PackagePath points to the file in the root of your project, which appears there as a linked file.
Here is the project file with both required changes:
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AssemblyName>whatever/AssemblyName>
<PackageId>whatever</PackageId>
<Version>1.2.1</Version>
<Authors>daenet GmbH</Authors>
<Description>My best package</Description>
<Copyright>daenet GmbH</Copyright>
<PackageLicenseFile>license.txt</PackageLicenseFile>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
</PropertyGroup>
<ItemGroup>
<Content Include="..\..\license.txt"
Link="license.txt"
Pack="true"
PackagePath="license.txt"/>
</ItemGroup>