[英]Nuget Pack multiple projects into single Nuget package with all external dependencies
I am looking to create a single nuget package out of multiple .csproj.我希望从多个 .csproj 中创建一个单一的 nuget 包。
Ex: I have two .csproj, one of them having dependency on other external packages such as NewtonSoft.例如:我有两个 .csproj,其中一个依赖于其他外部包,例如 NewtonSoft。
dotnet new classlib -o ClassLibrary1
dotnet new classlib -o ClassLibrary2
cd ClassLibrary1
dotnet add package Newtonsoft.Json
I am looking to publish a single nuget package which contains both ClassLibrary1.dll and ClassLibrary2.dll and resulting .nuspec have dependency Newtonsoft.Json listed.我希望发布一个包含 ClassLibrary1.dll 和 ClassLibrary2.dll 的单个 nuget 包,并且生成的 .nuspec 列出了依赖项 Newtonsoft.Json。
I tried doing something like this: Created a dummy .csproj and added references to ClassLibrary1 & 2 and created a nuget package with -IncludeReferencedProjects: Ex:-我尝试做这样的事情:创建了一个虚拟 .csproj 并添加了对 ClassLibrary1 和 2 的引用,并使用 -IncludeReferencedProjects 创建了一个 nuget 包:例如:-
dotnet new classlib -o dummyNuget
dotnet add reference ClassLibrary1.csproj
dotnet add reference ClassLibrary2.csproj
nuget pack dummyNuget.csproj -IncludeReferencedProjects
it produces below .nuspec file , missing the NewtonSoft Dependency.它产生以下 .nuspec 文件,缺少 NewtonSoft 依赖项。
<?xml version="1.0" encoding="utf-8"?>
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
<id>dummynuget</id>
<version>1.0.0</version>
<authors></authors>
<description>Description</description>
<dependencies />
</metadata>
</package>
Any ideas?有任何想法吗? how to create a single nuget package with -IncludeReferencedProjects and all the dependencies listed.
如何使用 -IncludeReferencedProjects 和列出的所有依赖项创建单个 nuget 包。
Sample Github Repo to quickly reproduce the issue : https://github.com/vivekscripts/TestNugetPack/tree/main示例 Github Repo 以快速重现该问题: https ://github.com/vivekscripts/TestNugetPack/tree/main
I know common approach would be to first combine the source files into a single project and thus use dotnet pack to publish a single package but lets say that option is not available easily due to cross team dependencies.我知道常见的方法是首先将源文件组合到一个项目中,然后使用 dotnet pack 发布一个包,但可以说由于跨团队依赖关系,该选项不容易使用。
You can achieve this with "dotnet pack" with a bit of hackery.您可以使用“dotnet pack”来实现这一点,但需要一点技巧。
For this scenario, in PowerShell, I ran the following to create the (3) projects, add project references, and to also add a Nuget reference to NewtonSoft.Json.对于这种情况,在 PowerShell 中,我运行以下命令来创建 (3) 个项目、添加项目引用以及添加对 NewtonSoft.Json 的 Nuget 引用。
# Create projects
dotnet new classlib -o ClassLibrary1
dotnet new classlib -o ClassLibrary2
dotnet new classlib -o dummyNuget
# Add references
dotnet add dummyNuget reference ClassLibrary1
dotnet add dummyNuget reference ClassLibrary2
dotnet add dummyNuget package Newtonsoft.Json
dotnet add dummyNuget package Nuget.Build.Tasks.Pack
# Create nuget package
dotnet build dummyNuget
After creating these projects, you have to modify the *.csproj ProjectReference entries to include "PrivateAssets."创建这些项目后,您必须修改 *.csproj ProjectReference 条目以包含“PrivateAssets”。
<!-- To ensure project refernece DLLs are included, must set PrivateAssets="All" -->
<ItemGroup>
<ProjectReference Include="..\ClassLibrary1\ClassLibrary1.csproj" PrivateAssets="All" />
<ProjectReference Include="..\ClassLibrary2\ClassLibrary2.csproj" PrivateAssets="All" />
</ItemGroup>
And next comes the hackery bit.接下来是骇客位。 This section can be added to the project to ensure the DLLs from the ProjectReferences get included int he lib/net6.0 folder of the nuget package.
可以将此部分添加到项目中,以确保 ProjectReferences 中的 DLL 包含在 nuget 包的 lib/net6.0 文件夹中。
<!-- Fix for dotnet pack not including private (project) references -->
<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>
<!-- End of dotnet pack fix -->
With those changes in place, we can run dotnet pack to get the nuget package with proper dependencies referenced and with local dependencies in the lib folder:有了这些更改,我们可以运行 dotnet pack 来获取 nuget 包,其中引用了正确的依赖项并在 lib 文件夹中具有本地依赖项:
dotnet pack dummyNuget\dummyNuget.csproj
I used these two links as guides, btw:我使用这两个链接作为指南,顺便说一句:
https://josef.codes/dotnet-pack-include-referenced-projects/https://josef.codes/dotnet-pack-include-referenced-projects/
https://dev.to/yerac/include-both-nuget-package-references-and-project-reference-dll-using-dotnet-pack-2d8p https://dev.to/yerac/include-both-nuget-package-references-and-project-reference-dll-using-dotnet-pack-2d8p
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.