如何打包nuget包,使其安装我们的项目dll,然后单独安装第3方nuget包

问题描述 投票:0回答:2

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

现在我使用 azurepipelines.yml 构建整个解决方案并打包基础项目并将其作为 nuget 包推送。现在,我的 nuget 包包含所有 dll,此外还有 log4net 的 dll 以及我安装到项目中的其他 nuget 包。所以这个包的体积是非常大的。相反,我只想要我的项目 dll 和 Microsoft dll,而不是我的 nuget 包中的第三方包。当我在其他解决方案中安装此 nuget 包时,我希望其他库(log4net、Oracle.ManagedDataAccess、GraphQL.Client)与其一起安装到“packages”文件夹中。

这将帮助我拥有一个小的 nuget 包,并且这些额外的库 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>
c# .net nuget azure-pipelines nuget-package
2个回答
0
投票

我通过添加 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 包大小从 >10MB 减小到 <4MB. And when I install my package in other solutions via Nuget Package Manager in Visual Studio, the dependecies get installed separately.


0
投票

更新:

我正在寻找的是我不需要任何第 3 方 dll(例如 Oracle.ManagedDataAcess 或 GraphQL)进入我的 nupkg,而不是我 当我在其他地方安装我的 nupkg 时,希望它们单独安装 使用 Visual Studio Nuget 包管理器的项目。这可以吗?

答案是肯定的。

如果您不想将任何第 3 方 dll 包含到 nupkg 中,您可以将这些包安装到基本 NET Framework 4.8 类库项目中,而不是添加 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,我们将获得包含这些包作为依赖项的包:

当我使用 Visual Studio Nuget 包管理器在其他项目中安装 nupkg 时,这些依赖项将单独安装

如何打包 nuget 包,使其安装我们的项目 dll,然后单独安装第 3 方 nuget 包

您可以使用

.nupsec
文件将这些库添加到工具文件夹中,这将在我们安装基本 NET Framework 4.8 类库包后将这些库保存在 packages 文件夹中。

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

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

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

此外,

<Pack>true</Pack>
仅适用于SDK样式项目:

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

注意:安装基础包后,这些库应该位于 Packages 文件夹中的

Tools\libraries
文件夹下,而不是添加到项目中。

© www.soinside.com 2019 - 2024. All rights reserved.