繁体   English   中英

如何打包 nuget package 以安装我们的项目 dll 然后分别安装第三方 nuget 包

[英]How to pack nuget package in such a way that it install our project dlls and then install 3rd party nuget packages separately

我有一个 dotnet 解决方案,其中包含许多 .NET Framework 4.8 class 库项目。 一些项目安装了其他包,如 log4net、Oracle.ManagedDataAccess、GraphQL.Client。 在此解决方案文件中,我有一个基本 NET Framework 4.8 class 库项目,我已将所有其他 class 库添加为依赖项。

现在我使用 azurepipelines.yml 构建整个解决方案并打包基础项目并将其推送为 nuget package。 现在我的 nuget package 有所有的 dll 加上它有 log4net 的 dll 和其他 nuget 包,我安装到我的项目。 所以这个package的尺寸很大。 相反,我只想要我的项目 dll 和 Microsoft dll,而不是我的 nuget package 中的第三方包。 And when I install this nuget package in my other solutions, I want the other libraries (log4net, Oracle.ManagedDataAccess, GraphQL.Client) to be installed along with that - to the "packages" folder.

这将帮助我拥有一个小的 nuget package 并且这些额外的库 dll 将单独安装。 我怎样才能做到这一点?

- task: DotNetCoreCLI@2
  displayName: 'Dotnet Pack'
  inputs:
    command: 'pack'
    arguments: '--configuration $(buildConfiguration)'
    packagesToPack: '**/MyBaseProject.csproj'
    versioningScheme: 'off'
    includeReferencedProjects: true

- task: NuGetCommand@2
  displayName: 'NuGet Push'
  inputs:
    command: 'push'
    feedsToUse: 'select'
    packagesToPush: '$(Build.ArtifactStagingDirectory)/**/XX*.nupkg'
    versioningScheme: 'off'
    allowPackageConflicts: true
    publishVstsFeed: 'feedIDHere'

我的 csproj 如下所示

<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <PackageId>MyPackage</PackageId>
    <Version>1.1.10</Version>
</PropertyGroup>

<ItemGroup>
    <ProjectReference Include="..\Business\xx1.csproj">
       <ExcludeAssets>all</ExcludeAssets>
    </ProjectReference></ProjectReference>
    <ProjectReference Include="..\Logging\xx2.csproj">
       <ExcludeAssets>all</ExcludeAssets>
    </ProjectReference></ProjectReference>
    <ProjectReference Include="..\Services\xx3.csproj">
       <ExcludeAssets>all</ExcludeAssets>
    </ProjectReference></ProjectReference>
</ItemGroup>
<ItemGroup>
   <None Include="..\Business\bin\$(Configuration)\*.dll">
      <PackagePath>lib\net48</PackagePath>
      <Pack>true</Pack>
      <Visible>false</Visible>
   </None>
</ItemGroup>
</Project>

更新:

我想要的是,我不希望任何第 3 方 dll(如 Oracle.ManagedDataAcess 或 GraphQL)进入我的 nupkg,而是希望在使用 Visual Studio 在其他项目中安装我的 nupkg 时单独安装它们Nuget Package 经理。 这可以做到吗?

答案是肯定的。

如果您不想在您的 nupkg 中包含任何第 3 方 dll,您可以将这些包安装到基础 NET Framework 4.8 class 库项目中,而不是添加 dll 文件:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net48</TargetFramework>
    <PackageId>MyPackage</PackageId>
    <Version>2.1.10</Version>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="GraphQL" Version="4.5.0" />
    <PackageReference Include="Oracle.ManagedDataAccess" Version="19.11.0" />
  </ItemGroup>

<ItemGroup>
    <ProjectReference Include="..\Business\xx1.csproj">
       <ExcludeAssets>all</ExcludeAssets>
    </ProjectReference></ProjectReference>
    <ProjectReference Include="..\Logging\xx2.csproj">
       <ExcludeAssets>all</ExcludeAssets>
    </ProjectReference></ProjectReference>
    <ProjectReference Include="..\Services\xx3.csproj">
       <ExcludeAssets>all</ExcludeAssets>
    </ProjectReference></ProjectReference>
</ItemGroup>
<ItemGroup>
   <None Include="..\Business\bin\$(Configuration)\*.dll">
      <PackagePath>lib\net48</PackagePath>
      <Pack>true</Pack>
      <Visible>false</Visible>
   </None>
</ItemGroup>
</Project>

然后我们使用 dotnet pack 这个项目,我们会得到 package 和这些包作为依赖:

在此处输入图像描述

当我使用 Visual Studio Nuget Package Manager 在其他项目中安装我的 nupkg 时,将单独安装这些依赖项

如何打包 nuget package 以安装我们的项目 dll 然后分别安装第三方 nuget 包

您可以使用.nupsec文件将这些库添加到工具文件夹中,这将在我们安装基本 NET Framework 4.8 class 库 package 后将这些库保存在包文件夹中。

我们需要使用工具约定创建 a.nupsec:

  <files>
    <file src="bin\Debug\YourBase.dll" target="lib\net48" />
    <file src="Business\bin\$(Configuration)\*.dll" target="Tools\libraries"/>
  </files> 

您可以查看此文档Create the.nuspec 文件以获取更多详细信息。

此外, <Pack>true</Pack>仅适用于 SDK-Style 项目:

<ItemGroup>
   <None Include="..\Business\bin\$(Configuration)\*.dll">
      <PackagePath>lib\net48</PackagePath>
      <Pack>true</Pack>
      <Visible>false</Visible>
   </None>
</ItemGroup>

注意:安装基本 package 后,这些库应该在 Packages 文件夹中的文件夹Tools\libraries下,而不是添加到项目中。

我通过添加 nuspec 文件并更改 dotnet pack 作业以查找 nuspec 来纠正该问题。

- task:  DotNetCoreCLI@2
  displayName: 'Dotnet Pack'
  inputs:
    command: 'pack'
    arguments: '--configuration $(buildConfiguration)'
    packagesToPack: '**/Myprjoect.nuspec'
    versioningScheme: 'off'
    nobuild: true
    includeReferencedProjects: true

nuspec 文件如下所示。

<?xml version="1.0" encoding="utf-8"?>
<package>
  <metadata>
    <id>Package Name</id>
    <version>1.1.15</version>
    <dependencies>
      <group targetFramework=".NETFramework4.8">
        <dependency id="GraphQL.Client" version="3.2.0" />
        <dependency id="iTextSharp" version="5.5.9" />
        <dependency id="log4net" version="2.0.5" />
        <dependency id="Oracle.ManagedDataAccess" version="19.11.0" />
      </group>
    </dependencies>
  </metadata>
  <files>
    <file src="**\*.dll" exclude="**\GraphQL.*;**\Oracle*.dll;**\obj\**\*.*;**\itextsharp.dll;**\log4net.dll" target="lib\net48" />
  </files>
</package>

这些更改帮助我将 nuget package 大小从 >10MB 减小到 <4MB。 当我在 Visual Studio 中通过 Nuget Package 管理器在其他解决方案中安装我的 package 时,将单独安装依赖项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM