NuGet 包显示没有依赖项?

NuGet package shows no dependencies?

我尝试从 .NET 4.7.2 class 库 (VS2017) 制作 NuGet 包,但令人惊讶的是生成的 NuGet 包没有依赖项(这是一个错误)。

我的设置是这样的:

nuget.exe pack 命令应该自动填写所需的依赖项——这也曾经是之前的情况(在另一个项目中)。但是,当时我在 class 库中使用 packages.config 而不是 packageReferences。这会改变什么吗?

这是怎么回事?

如何强制系统在我的包中再次包含所需的依赖项?

备注:

How can I force the system to again include the needed dependencies in my package?

这是关于 nuget pack is ignoring dependencies when using PackageReference instead of packages.config 的已知问题。

要解决此问题,您可以使用以下解决方法,NuGet 团队仍在积极努力改进此方案:

To package your C# Class Library which manages your dependencies via PackageReference in the csproj itself,

please add a reference to NuGet.Build.Tasks.Pack ( https://www.nuget.org/packages/NuGet.Build.Tasks.Pack/) and run msbuild /t:pack from the command line.

我已经测试了这个解决方法,它工作正常。为确保此解决方法正常工作,我们需要注意以下 :

  • 需要将nuget包NuGet.Build.Tasks.Pack添加到项目中。
  • 需要添加属性/p:PackageOutputPath="D:\TesterFolder" -p:Authors=tester
  • 使用命令msbuild.exe /t:pack,如:msbuild.exe /t:pack "MyTestLibrary.csproj" /p:PackageOutputPath="D:\TestFolder" -p:Authors=tester

此外,如果您想使用 .nuspec 文件创建 nuget 包,您应该使用以下 .nuspec 文件:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>MyTestLibrary</id>
    <version>1.0.0</version>
    <authors>Tester</authors>
    <owners>Tester</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>Package description</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2018</copyright>
    <tags>Tag1 Tag2</tags>
    <dependencies>
      <group targetFramework=".NETFramework4.7.2">
        <dependency id="Microsoft.Owin" version="4.0.0" exclude="Build,Analyzers" />
      </group>
    </dependencies>
  </metadata>

    <files>
        <file src="bin\Debug\MyTestLibrary.dll" target="lib\net472\MyTestLibrary.dll" />
    </files>
</package>

然后我们可以使用 nuget.exe pack 创建 nuget 包。但是,使用这种方法,我们必须在 .nuspec 文件中手动填写所需的依赖项。

希望这对您有所帮助。

我遇到了同样的问题,使用 leo Liu 提到的第二种方式 Pattrick'script 和一些小的修改来解决这个问题。步骤如下:

  1. 复制或下载下面的 powershell 脚本,将其命名为 NuGetPackDependencies.ps1 并将其添加到您的解决方案中(因为我想在这个解决方案)
    function Format-XML {Param ([string]$xmlfile) 
      
      $Doc=New-Object system.xml.xmlDataDocument 
      $doc.Load((Resolve-Path $xmlfile)) 
      $sw=New-Object system.io.stringwriter 
      $writer=New-Object system.xml.xmltextwriter($sw) 
      $writer.Formatting = [System.xml.formatting]::Indented 
      $doc.WriteContentTo($writer) 
      $sw.ToString() 
    }
    
    '*****'
    '***** PowerShell script NugetPackDependencies 1.0.'
    '***** Insert project package references as dependencies into package manifest (nuspec file)'
    '*****'
    '***** Start script'
    '*****'
    
    Set-Location -Path $args[0]
    
    # Get VB.NET or C# project file.
    $projFile = (ls -Path "*.vbproj", "*.csproj" | Select-Object -First 1).Name
    
    # If project file cannot be found exit script.
    if ($projFile -eq $null) {'***** Project file not found. Exit script'
                                exit}
    else {"***** Get package references from project file: '" + $projFile + "'"} 
    
    
                      
    # Get content from project file.
    $projFileContent = ls -Filter $projFile | Get-Content
    
    # Convert content from project file to XML.
    $projFileXml  = [xml]$projFileContent
    
    
    # Namespace 
    $nm = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $projFileXml.NameTable
    $nm.AddNamespace('x', 'http://schemas.microsoft.com/developer/msbuild/2003')
    
    
    # Get package references from project file xml and put them in an list of new objects containg id and version.
    $packRefs=$projFileXml.SelectNodes('/x:Project/x:ItemGroup/x:PackageReference', $nm) | 
    ForEach-Object {New-Object -TypeName PSObject -Property @{
                        id = New-Object -TypeName Reflection.AssemblyName -ArgumentList $_.Include
                        version = New-Object -TypeName Reflection.AssemblyName -ArgumentList $_.Version}               
                    } 
    
    Write-Output $packRefs
    
    # Create new XML tags for the nuspec file containing the id and version.
    $packRefsXml= $packRefs | Select-Object @{L='deps'; E ={ "<dependency id=""" + $_.id + """ version=""" + $_.version + """ />"}}
    
    
    # concatenate the tags.
    $packRefsXmlConcat = ""
    $packRefsXml | ForEach-Object { 
    $packRefsXmlConcat = $packRefsXmlConcat +  $_.deps
    }
    
    # Get the nuspec file.
    $nuspec = (ls -Path "*.nuspec" | Select-Object -First 1)
    $nuspecFile = $nuspec.FullName
    
    # If nuspec file cannot be found exit script.
    "*****"
    if (!$nuspecFile) {Write-Output '***** Nuspec file not found. Exit script'
                        exit}
    else{"***** Insert dependencies into nuspec file: '" + $nuspec.NAme + "'"} 
    
    # Put the nuspec XML in a var using .NET XmlDocument
    $xmlNuspec = New-Object System.Xml.XmlDocument
    $xmlNuspec.PreserveWhitespace = $true
    $xmlNuspec.Load($nuspecFile)
    
    # Remove all dependencies elements if present.
    $tags =$xmlNuspec.package.metadata.SelectNodes("dependencies")
    
    ForEach($tag in $tags) {
    $xmlNuspec.package.metadata.RemoveChild($tag) | Out-Null # Suppress unwanted Output
    }
    
    # Namespace.
    $nm = New-Object -TypeName System.Xml.XmlNamespaceManager -ArgumentList $xmlNuspec.NameTable
    $nm.AddNamespace('x', '')
    
    # Get the metadata tag from the xml
    $metaDataElement = $xmlNuspec.SelectNodes('/x:package/x:metadata', $nm)  
    
    # Create new dependecies element
    $newDependenciesChild = $xmlNuspec.CreateElement("dependencies") 
    
    # Add dependency elements to dependencies element
    $newDependenciesChild.set_innerXML($packRefsXmlConcat) | Out-Null # Suppress unwanted Output
    
    # Append dependencies child to metdata child
    $metaDataElement.AppendChild($newDependenciesChild) | Out-Null # Suppress unwanted Output
    
    # Write output to temporary nuspec file
    $xmlNuspec.OuterXml | Out-File -filepath temp.nuspec 
    
    # Pretty the nuspec file and overwrite original nupec file using the format-XML function.
    Format-XML -xmlfile temp.nuspec | Out-File -filepath $nuspecFile
    
    # delete temp nuspec.
    del temp.nuspec
    
    "*****"
    "***** Finished script"
    "*****"
    

2.Config Post-构建事件

  cd $(SolutionDir)
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File NuGetPackDependencies.ps1 $(ProjectDir)
  1. 像以前一样构建和发布您的项目。