我期待创建一个单一的nuget包的多个.csproj。
我有两个.csproj,其中一个依赖于其他外部包,比如NewtonSoft。
dotnet new classlib -o ClassLibrary1
dotnet new classlib -o ClassLibrary2
cd ClassLibrary1
dotnet add package Newtonsoft.Json
我希望发布一个nuget包,其中包含ClassLibrary2.dll 1.dll和ClassLibrary2.dll 2.dll,并列出了由此产生的.nuspec的依赖项Newtonsoft.Json。
我尝试这样做:创建一个虚拟.csproj,添加对ClassLibrary1 &2的引用,并使用-IncludeReferencedProjects: Ex:-创建一个nuget包:-
dotnet new classlib -o dummyNuget
dotnet add reference ClassLibrary1.csproj
dotnet add reference ClassLibrary2.csproj
nuget pack dummyNuget.csproj -IncludeReferencedProjects
它在.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>
有什么想法吗?如何使用-IncludeReferencedProjects和列出的所有依赖项创建单个nuget包。
示例Github可以快速复制问题:https://github.com/vivekscripts/TestNugetPack/tree/main
我知道常见的方法是首先将源文件合并到单个项目中,然后使用dotnet发布单个包,但让我们说,由于跨团队依赖关系,这个选项并不容易使用。
发布于 2022-07-19 21:33:48
您可以通过"dotnet“和一些黑客来实现这一点。
对于这个场景,在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
在创建这些项目之后,您必须修改*.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>
接下来是黑客攻击。本节可以添加到项目中,以确保nuget包的int /net6.0文件夹中包含来自ProjectReferences的DLL。
<!-- 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 -->
在进行了这些更改之后,我们可以运行dotnet来获得具有适当依赖关系的nuget包,以及lib文件夹中的本地依赖项:
dotnet pack dummyNuget\dummyNuget.csproj
我用这两个链接作为指南,顺便说一句:
https://josef.codes/dotnet-pack-include-referenced-projects/
https://stackoverflow.com/questions/72984765
复制相似问题