'How to copy files to output directory from a referenced NuGet package in .NET Core csproj?

I'm trying to use PhantomJS NuGet package in .NET core csproj application. But I think it is not possible using new PackageReference syntax for NuGet.

When I reference the PhantomJS package like this:

<PackageReference Include="PhantomJS" Version="2.1.1">
  <IncludeAssets>all</IncludeAssets>
</PackageReference>

It does not do anything when I run dotnet build.

I'd expect it to copy the files inside PhantomJS package to the output directory (or anywhere in the project) so I could use the binary file provided by the PhantomJS package.

Is there another way to copy the contents of PhantomJS NuGet package to the output directory with MSBuild?



Solution 1:[1]

I think you want to use:

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>

in the main <PropertyGroup>, which causes all dependencies to be copied to the output folder. This means every single dependency gets copied though so this can be quite a mess in some situations.

If you then want to exclude specific assemblies or packages:

<ItemGroup>
    <-- won't copy to output folder -->
    <PackageReference Include="MahApps.Metro" version="1.6.5">
      <IncludeAssets>compile</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Dragablz" version="0.0.3.203">
      <IncludeAssets>compile</IncludeAssets>
    </PackageReference>
    ... 

    <-- normal will copy to output folder -->
    <PackageReference Include="xmlrpcnet" version="3.0.0.266" />
    <PackageReference Include="YamlDotNet" version="6.0.0" />
</ItemGroup>


<ItemGroup>
    <!-- keep assembly reference from copying to output -->
    <Reference Include="$(SolutionDir)MarkdownMonster\bin\$(Configuration)\$(TargetFramework)\MarkdownMonster.exe">
      <Private>false</Private>
    </Reference> 
</ItemGroup>

compile in this context means they are available for compilation, but aren't copied to the output folder.

Solution 2:[2]

There are two solutions:

1:

  <ItemGroup>
    <PackageReference Include="PhantomJS" Version="1.0.8" GeneratePathProperty="true" />
  </ItemGroup>

  <Target Name="CopyPdfExe" AfterTargets="Build">
    <Copy SourceFiles="(PkgPhantomJS)\tools\phantomjs\phantomjs.exe" DestinationFolder="$(OutDir)" />
  </Target>

2:

  <ItemGroup>
    <PackageReference Include="PhantomJS" Version="1.0.8" GeneratePathProperty="true" />
  </ItemGroup>

  <ItemGroup>
    <None Include="$(PkgPhantomJS)\tools\phantomjs\phantomjs.exe" CopyToOutputDirectory="PreserveNewest" />
  </ItemGroup>

prefer the #2 since if this project is referenced by another one, the .exe can also be copied to the output folder

Solution 3:[3]

The <PackageReference> syntax in NuGet uses transitive dependencies, just like the project.json syntax. As such, the same rules apply. See this NuGet v3 which talks about what does and doesn't work between packages.config and the newer syntax. Specifically

You cannot rely on install.ps1 or uninstall.ps1 to function. These files will execute when using packages.config, but will be ignored in v3. So your package needs to be usable without them running. Init.ps1 will still run on NuGet 3.

To get files to copy to the output directory, the PhantomJS NuGet package needs to be changed to use contentFiles.

Solution 4:[4]

The tag names are misleading. Try

<PackageReference Include="PhantomJS" Version="2.1.1">
  <IncludeAssets>none</IncludeAssets>
</PackageReference>

or

<PackageReference Include="PhantomJS" Version="2.1.1">
  <ExcludeAssets>all</ExcludeAssets>
</PackageReference>

instead in order to get the referenced assemblies and other files copied to the build output. See https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files

Solution 5:[5]

Try dotnet publish

dotnet publish [<PROJECT>] [-c|--configuration] [-f|--framework] [--force] [--manifest] [--no-dependencies] [--no-restore] [-o|--output] [-r|--runtime] [--self-contained] [-v|--verbosity] [--version-suffix]
dotnet publish [-h|--help]

See https://docs.microsoft.com/en-us/dotnet/core/tools/dotnet-publish?tabs=netcore2x

Solution 6:[6]

I know this is an old question, but what worked for me was to set IncludeAssets to "runtime"

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rick Strahl
Solution 2 Community
Solution 3 Eric Erhardt
Solution 4 abatishchev
Solution 5 Samuel Liew
Solution 6 Frank Hagenson