首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将多个项目打包到带有所有外部依赖项的单个Nuget包中

将多个项目打包到带有所有外部依赖项的单个Nuget包中
EN

Stack Overflow用户
提问于 2022-07-14 17:53:49
回答 1查看 350关注 0票数 2

我期待创建一个单一的nuget包的多个.csproj。

我有两个.csproj,其中一个依赖于其他外部包,比如NewtonSoft。

代码语言:javascript
运行
复制
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包:-

代码语言:javascript
运行
复制
dotnet new classlib -o dummyNuget
dotnet add reference ClassLibrary1.csproj
dotnet add reference ClassLibrary2.csproj
nuget pack dummyNuget.csproj -IncludeReferencedProjects

它在.nuspec文件下面生成,缺少NewtonSoft依赖项。

代码语言:javascript
运行
复制
<?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发布单个包,但让我们说,由于跨团队依赖关系,这个选项并不容易使用。

EN

回答 1

Stack Overflow用户

发布于 2022-07-19 21:33:48

您可以通过"dotnet“和一些黑客来实现这一点。

对于这个场景,在PowerShell中,我运行了以下命令来创建(3)项目、添加项目引用以及向NewtonSoft.Json添加Nuget引用。

代码语言:javascript
运行
复制
# 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”。

代码语言:javascript
运行
复制
  <!-- 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。

代码语言:javascript
运行
复制
<!-- 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文件夹中的本地依赖项:

代码语言:javascript
运行
复制
dotnet pack dummyNuget\dummyNuget.csproj

我用这两个链接作为指南,顺便说一句:

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72984765

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档