[英]Add dependencies of reference projects to NuGet package
I'm using dotnet pack
command for creating a NuGet package for the following scenario:我正在使用
dotnet pack
命令为以下场景创建 NuGet package:
The projects structure:项目结构:
Project B
|----> Project A
Project A
|----> SomePackage
I want to create a single NuGet package the contains ProjectB.dll
, ProjectA.dll
, and SomePackage
as NuGet package dependency. I want to create a single NuGet package the contains
ProjectB.dll
, ProjectA.dll
, and SomePackage
as NuGet package dependency.
In order to include ProjectA.dll
as part of the NuGet package (and not package dependency), I used the following solution that suggests here . In order to include
ProjectA.dll
as part of the NuGet package (and not package dependency), I used the following solution that suggests here .
In ProjectB.csproj
:在
ProjectB.csproj
中:
<ProjectReference Include="ProjectA.csproj" PrivateAssets="all"/>
<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>
The issue:问题:
When I run dotnet pack ProjectB.csproj
I'm getting a single package the contains ProjectA.dll and Project B.dll, but without a dependency to SomePackage
.当我运行
dotnet pack ProjectB.csproj
时,我得到一个 package 包含 ProjectA.dll 和 Project B.dll,但没有对SomePackage
的依赖。
The question: How do I add the dependency to SomePackage
to ProjectB
NuGet package?问题:如何将
SomePackage
的依赖项添加到ProjectB
NuGet package?
Possible solutions:可能的解决方案:
SomePackage
.SomePackage
。ProjectB.nuspec
file and manually add the dependency to SomePakcage
.ProjectB.nuspec
文件并手动将依赖项添加到SomePakcage
。 The disadvantage of the 2 approaches: I will need to add the dependency for every NuGet package that ProjectA
use, which is very easy to forget and breakable.这两种方法的缺点:我需要为
ProjectA
使用的每个 NuGet package 添加依赖项,这很容易忘记和破坏。
You have used PrivateAssets="all"
for Project A which means that its transitive dependencies are private and cannot be access by the main Project B .您已经为Project A使用了
PrivateAssets="all"
,这意味着它的传递依赖项是私有的,并且不能被主Project B访问。 We usually use it to disable the dependencies of referenced projects.我们通常使用它来禁用引用项目的依赖关系。
So if you use that, you cannot get the transitive dependencies from the referenced projects.因此,如果您使用它,您将无法从引用的项目中获取传递依赖项。 And it will make Project A as a assembly dll and will make it lose the ability of nuget package and its transitive dependencies.
并且它将使项目 A成为一个组件 dll 并使其失去 nuget package 及其传递依赖项的能力。
If you still want to achieve the goal, you should note these:如果您仍想实现目标,则应注意以下几点:
1) directly copy the transitive dependencies like PackageReference
node from Project A's csproj into Project B's csproj. 1)直接将项目A的csproj中的
PackageReference
节点等传递依赖复制到项目B的csproj中。
<PackageReference Include="SomePackage" Version="*.*.*" />
......
In this case , you do not have to use ProjectB.nuspec
or nuget package manager UI.在这种情况下,您不必使用
ProjectB.nuspec
或 nuget package 管理器 UI。
2) Give up using PrivateAssets="all"
for Project A and make Project A as a nuget package. 2)放弃对项目 A使用
PrivateAssets="all"
并将项目 A 设为 nuget package。 Ensure the integrity of its nuget features.确保其 nuget 功能的完整性。
Apart from this, there is no other way.除此之外,别无他法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.