如何从.NET Core csproj中引用的NuGet包中将文件复制到输出目录?

时间:2017-03-17 16:34:16

标签: msbuild .net-core csproj

我试图在.NET核心csproj应用程序中使用PhantomJS NuGet package。但我认为使用NuGet的新PackageReference语法是不可能的。

当我像这样引用PhantomJS包时:

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

我运行dotnet build时没有做任何事情。

我希望它将PhantomJS包中的文件复制到输出目录(或项目中的任何地方),这样我就可以使用PhantomJS包提供的二进制文件。

有没有其他方法可以将PhantomJS NuGet包的内容复制到带有MSBuild的输出目录?

5 个答案:

答案 0 :(得分:4)

有两种解决方案:

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>

首选#2,因为如果该项目被另一个项目引用,则.exe也可以复制到输出文件夹

答案 1 :(得分:3)

NuGet中的<PackageReference>语法使用传递依赖,就像project.json语法一样。因此,适用相同的规则。请参阅此NuGet v3,其中讨论packages.config与较新语法之间的作用和不作用。具体地

  

您不能依赖install.ps1或uninstall.ps1来运行。这些文件将在使用packages.config时执行,但在v3中将被忽略。所以你的包需要在没有它们运行的​​情况下使用。 Init.ps1仍将在NuGet 3上运行。

要将文件复制到输出目录,需要将PhantomJS NuGet包更改为使用contentFiles

答案 2 :(得分:0)

尝试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]

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

答案 3 :(得分:0)

我认为您想使用:

<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PropertyGroup>中的

,这会将所有依赖项复制到输出文件夹。这意味着每个单一依赖项都会被复制,因此在某些情况下这很混乱。

如果您想排除特定的程序集或程序包:

<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在这种情况下意味着它们可用于编译,但不会复制到输出文件夹。

答案 4 :(得分:0)

标签名称具有误导性。尝试

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

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

而是为了获取引用的程序集和其他文件复制到构建输出。看到 https://docs.microsoft.com/en-us/nuget/consume-packages/package-references-in-project-files