Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 771
Ciantic Avatar asked Mar 17 '17 16:03

Ciantic


People also ask

How do I get files from a NuGet package?

on the toolbar of the Assembly Explorer window or choose File | Open from NuGet Packages Cache in the main menu . This will open the Open from NuGet Packages Cache dialog. The dialog lists packages from all NuGet cache locations on your machine. Use the search field in the dialog to find the desired package.


1 Answers

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.

like image 138
Rick Strahl Avatar answered Sep 21 '22 09:09

Rick Strahl