简体   繁体   English

Project生成一个nuget包,该包依赖于另一个不创建nuget包的项目

[英]Project generates a nuget package that depends on another project that does not create a nuget package

If I have a project (P1) that builds a nuget package, and I make it depend on a project (P2) that doesn't build a nuget package, the generated package will still reference P2 as a nuget package . 如果我有一个构建nuget程序包的项目(P1),并且使它依赖于没有构建nuget程序包的项目(P2),则生成的程序包仍将P2 称为nuget程序包

Steps to reproduce 重现步骤

  • Create a solution with 2 C# projects (P1 and P2) 使用2个C#项目(P1和P2)创建解决方案
  • Make P1 depend on P2. 使P1依赖于P2。
  • Add the following line to P1.csproj (to make it build a nuget package) 将以下行添加到P1.csproj(以使其构建nuget包)

    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>

  • Build the solution 建立解决方案
  • Open the generated P1.nupkg in, eg, NuGet Package Explorer 在例如NuGet Package Explorer中打开生成的P1.nupkg

Note that the NuGet package depends on another NuGet package called P2. 请注意,NuGet程序包依赖于另一个称为P2的NuGet程序包。 However, that package does not exist, and will never exist, as I have not told the P2 project to build a nuget package. 但是,该程序包不存在,也永远不会存在,因为我没有告诉P2项目构建nuget程序包。

Question

How do I force P2.dll to be included in P1.nupkg's lib folder? 如何强制P2.dll包含在P1.nupkg的lib文件夹中? Ideally it will force it only for references that don't create a nuget package themselves. 理想情况下,它将仅对本身不会创建nuget包的引用强制使用它。

Other questions 其他问题

One workaround is to mark any assets you don't want to include as <PrivateAssets>all</PrivateAssets> , and then include the below code in your project file. 一种解决方法是将您不想包含的所有资产标记为<PrivateAssets>all</PrivateAssets> ,然后将以下代码包含在项目文件中。

See https://github.com/nuget/home/issues/3891#issuecomment-459848847 for more information 有关更多信息,请参见https://github.com/nuget/home/issues/3891#issuecomment-459848847

<ItemGroup>
  <ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj">
    <PrivateAssets>all</PrivateAssets>
  </ProjectReference>
</ItemGroup>

<!--
  The following solves the problem that 'dotnet pack' does not include the DLLs from referenced projects.
  See https://github.com/NuGet/Home/issues/3891 for a description of the problem
  and for newer versions / workarounds / built-in methods.
-->
<PropertyGroup>
<TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  <!-- include PDBs in the NuGet package -->
<AllowedOutputExtensionsInPackageBuildOutputFolder>$(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb</AllowedOutputExtensionsInPackageBuildOutputFolder>
</PropertyGroup>
<Target Name="CopyProjectReferencesToPackage" DependsOnTargets="ResolveReferences">
    <ItemGroup>
        <BuildOutputInPackage Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'all'))" />
    </ItemGroup>
</Target>

If I understood correctly, the referenced project assemblies are not getting included in the nuget package. 如果我正确理解,那么引用的项目程序集不会包含在nuget包中。

You can try below command while generating nuget package by using below command: 您可以使用以下命令在生成nuget软件包时尝试以下命令:

nuget pack projectfile.csproj -IncludeReferencedProjects

This should include P2.dll in the nuget. 这应该在nuget中包含P2.dll。 Refer this MSDN page for more details. 有关更多详细信息, 请参考此MSDN页面

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM