使用dotnet pack包含所有依赖项

时间:2016-11-03 07:42:15

标签: asp.net-core nuget .net-core

有没有办法强制dotnet pack包含所有引用的程序集(project.json中的所有依赖项)?

我相信这是相关的:

6 个答案:

答案 0 :(得分:11)

截至2020年,尚无官方支持的方法。但是,各种各样的人已经想出了实现它的方法,而当前最好的方法是安装由令人惊叹的NuGet package准备的Teroneko。然后,您所需要做的就是编辑.csproj,以更新要标记为PrivateAssets="all"as per the package README的所有项目。


如果您无法安装上述的NuGet软件包,则可以通过编辑by editing your .csproj to include the following来达到相同的效果(再次,这是Teroneko发现的-本质上就是他创建的NuGet软件包所做的事情):

<Project>
  <PropertyGroup>
    <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CopyProjectReferencesToPackage</TargetsForTfmSpecificBuildOutput>
  </PropertyGroup>
  
  <Target Name="CopyProjectReferencesToPackage" DependsOnTargets="BuildOnlySettings;ResolveReferences">
    <ItemGroup>
      <!-- Filter out unnecessary files -->
      <_ReferenceCopyLocalPaths Include="@(ReferenceCopyLocalPaths->WithMetadataValue('ReferenceSourceTarget', 'ProjectReference')->WithMetadataValue('PrivateAssets', 'All'))"/>
    </ItemGroup>

    <!-- Print batches for debug purposes -->
    <Message Text="Batch for .nupkg: ReferenceCopyLocalPaths = @(_ReferenceCopyLocalPaths), ReferenceCopyLocalPaths.DestinationSubDirectory = %(_ReferenceCopyLocalPaths.DestinationSubDirectory) Filename = %(_ReferenceCopyLocalPaths.Filename) Extension = %(_ReferenceCopyLocalPaths.Extension)" Importance="High" Condition="'@(_ReferenceCopyLocalPaths)' != ''" />

    <ItemGroup>
      <!-- Add file to package with consideration of sub folder. If empty, the root folder is chosen. -->
      <BuildOutputInPackage Include="@(_ReferenceCopyLocalPaths)" TargetPath="%(_ReferenceCopyLocalPaths.DestinationSubDirectory)"/>
    </ItemGroup>
  </Target>
</Project>

与该程序包一样,然后在您的.csproj中使用PrivateAssets="all"标记依赖项引用,并且Just Works(tm)。

答案 1 :(得分:4)

该问题的另一个解决方案是创建一个自定义 .targets 文件以包含在您的项目中。您可以添加一些 msbuild 指令以在包中包含您需要的文件。有一些关于如何做的documentation here,这里有一个简短的例子

<PropertyGroup Condition="$(PackAsComponent) != ''">
  <TargetsForTfmSpecificBuildOutput>$(TargetsForTfmSpecificBuildOutput);CustomBuildOutput</TargetsForTfmSpecificBuildOutput>
  <TargetsForTfmSpecificContentInPackage>$(TargetsForTfmSpecificContentInPackage);CustomContentInPackage</TargetsForTfmSpecificContentInPackage>
</PropertyGroup>

<Target Name="CustomBuildOutput">
  <ItemGroup>
    <BuildOutputInPackage Include="$(OutputPath)*.dll" Exclude="$(TargetPath)" />
    <BuildOutputInPackage Include="$(OutputPath)*.pdb" />
    <BuildOutputInPackage Include="$(OutputPath)*.exe" Exclude="$(TargetPath)" />
  </ItemGroup>
</Target>

<Target Name="CustomContentInPackage">
  <ItemGroup>
    <TfmSpecificPackageFile Include="abc.txt">
    <PackagePath>mycontent/$(TargetFramework)</PackagePath>
    </TfmSpecificPackageFile>
  </ItemGroup>
</Target>

基本上我在我的项目中设置 PackAsComponent 属性时激活它。 这将 100% 保留“dotnet pack”功能,而无需指定任何参数。

答案 2 :(得分:0)

根据设计,每个依赖项都需要单独进行dotnet打包。

有些人将其发布文件夹中的dll作为其包选项中的文件包含在设计中。

但是,为每个单独的依赖项创建包会导致代码更容易进行版本,维护,更新等...

答案 3 :(得分:0)

我希望这会对您有所帮助。

nuget pack yournuspecfile.nuspec -properties Configuration=Release -IncludeReferencedProjects

或您的命令。

答案 4 :(得分:0)

我一直在寻找这个答案,但是当我找不到一个明显的答案时,我很生气。最适合我的解决方案是创建一个nuspec,将nupkg中所需的DLL列表添加到该规范中,然后使用dotnet pack进行构建。我在这里创建了一个简单的示例和自述文件-nuget sample app

答案 5 :(得分:0)

因为我已经在我的构建系统上安装了 Octopus 构建工具,所以我使用 octo pack 来创建包。虽然这与调用老式 nuget.exe 基本上是一样的。

https://octopus.com/docs/packaging-applications/create-packages/octopus-cli