如何使用 msbuild 15 从生成的 nuget 包中的 nuspec 文件中删除依赖项部分

How to remove the dependencies section from nuspec file in the generated nuget package using msbuild 15

我使用 msbuild v15.8.168.64424(对比 2017.8)命令行为控制台应用程序构建 nuget 包。我用ILMerge合并了所有的依赖,所以只得到一个没有依赖的exe文件

运行命令时

        msbuild project1.csproj /t:build /p:configuration=release /p:IsTool=true

我得到了 nuget 包,但是在检查它时,我发现 dependencies section

     <dependencies>
           <group targetFramework=".NETFramework4.5">
            <dependency id="lib1" version="1.3.0" exclude="Build,Analyzers" />
            <dependency id="lib2" version="2.3.0" exclude="Build,Analyzers" />              
          </group>
        </dependencies>

我希望 `dependencies 部分为空(因为我合并了所有依赖项)。

我可以使用 nuspec 文件生成没有依赖项的包,但缺点是手动传递 csproj 中包含的所有元数据。 我没有在 pack target inputs

中找到依赖项的 属性

问题:

如何从包含在生成的 nuget 包中的 nuspec 文件中删除依赖项部分?

您可以在 csproj 文件中的包或项目引用 (<PackageReference> / <ProjectReference>) 上设置元数据 PrivateAssets="All" 以将它们排除为 NuGet 依赖项。

请注意,此更改需要还原,因此在两者之间执行 NuGet 还原或将 -restore 添加到 MSBuild 调用。