Search code examples
msbuildnuget

How to include a .lic file in a NuGet package?


In addition to three .NET assemblies, I have a .lic license file in a NuGet package that I need to have copied to the consuming project's root directory with its CopyToOutputDirectory property set to PreserveNewest when the package is installed.

Oddly, the assemblies are being copied but the license file isn't. I've tried putting it in both lib and content folders in the package, but the result is the same. I've also tried variations of this example, but nothing happens on package installation with that one. Also, that scenario is different from mine—I need to include the build output in the package.

I tried this as well, but no .lic file is copied. This didn't work either.

Here's my package and my project file. How can I get the .lic file to be copied to the consuming project's root folder, with the correct CopyToOutputDirectory value set?

enter image description here

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

  <PropertyGroup>
    <OutputType>Library</OutputType>
    <RootNamespace></RootNamespace>
    <TargetFramework>netstandard2.0</TargetFramework>
    <AppDesignerFolder>Properties</AppDesignerFolder>
    <Version>1.0.5</Version>
    <GeneratePackageOnBuild>True</GeneratePackageOnBuild>
  </PropertyGroup>

  <ItemGroup>
    <Folder Include="Properties\" />
  </ItemGroup>

  <ItemGroup>
    <Reference Include="HexDns.NET">
      <HintPath>Hexillion\HexDns.NET.dll</HintPath>
    </Reference>
    <Reference Include="HexNetwork.NET">
      <HintPath>Hexillion\HexNetwork.NET.dll</HintPath>
    </Reference>
    <Reference Include="HexValidEmail.NET">
      <HintPath>Hexillion\HexValidEmail.NET.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup>
    <None Include="Hexillion\HexDns.NET.dll">
      <Pack>true</Pack>
      <PackagePath>lib\netstandard2.0\HexDns.NET.dll</PackagePath>
    </None>
    <None Include="Hexillion\HexNetwork.NET.dll">
      <Pack>true</Pack>
      <PackagePath>lib\netstandard2.0\HexNetwork.NET.dll</PackagePath>
    </None>
    <None Include="Hexillion\HexValidEmail.NET.dll">
      <Pack>true</Pack>
      <PackagePath>lib\netstandard2.0\HexValidEmail.NET.dll</PackagePath>
    </None>
    <None Include="HexValidEmail.NET.lic">
      <Pack>true</Pack>
      <PackagePath>lib\HexValidEmail.NET.lic</PackagePath>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
    </None>    
  </ItemGroup>

</Project>

--EDIT--

There's been some progress: using a slight variation of this answer I've been able to copy the .lic file to the consuming project's root folder. But the CopyToOutputDirectory value still isn't being set.

  <ItemGroup>
    <Content Include="contentFiles/**/*.*">
      <Pack>true</Pack>
      <PackagePath>contentFiles\any\any;content</PackagePath>
      <IncludeInPackage>true</IncludeInPackage>
      <CopyToOutput>true</CopyToOutput>
      <BuildAction>Content</BuildAction>
      <CopyToOutput>true</CopyToOutput>
      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      <CopyToPublishDirectory>PreserveNewest</CopyToPublishDirectory>
    </Content>
  </ItemGroup>

Solution

  • My, this stuff is finicky.

    But I was able to get it working using this helpful answer. Here's my final solution:

    <ItemGroup>
      <Content Include="HexValidEmail.NET.lic">
        <Pack>true</Pack>
        <PackagePath>contentFiles\any\any;content</PackagePath>
        <PackageCopyToOutput>true</PackageCopyToOutput>
        <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
      </Content>
    </ItemGroup>