.net NuGet包显示没有依赖项?

olmpazwi  于 2023-10-21  发布在  .NET
关注(0)|答案(2)|浏览(283)

我尝试从.NET 4.7.2类库(VS 2017)中创建一个NuGet包,但结果NuGet包令人惊讶地显示没有依赖项(这是一个错误)。
我的设置是这样的:

  • 我的类库是.NET Framework 4.7.2
  • 我的类库使用了 * 另一个 * NuGet包(它有 * 依赖项)。
  • 我的类库在.csproj中使用 packageReferences
  • 我的类库包含一个适当的.nuspec文件
  • 我使用 *nuget. exepack * 来创建包
  • nuget.exe pack* 命令应该 * 自动 * 填充所需的依赖项-这也是以前的情况(在另一个项目中)。然而,在那个时候,我使用 packages.config 而不是我的类库中的 packageReferences。这会改变什么吗

这是怎么回事?

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

备注:

  • 该软件包由TeamCity构建服务器上的MSBuild脚本构建(没有VS 2017)。这是一个构建脚本,它调用 “nuget.exe restore” 和更高版本的 “nuget.exe pack” 作为其构建逻辑的一部分。
  • MSBuild版本15.7
  • nuget.exe版本为4.6.2
lztngnrs

lztngnrs1#

我怎样才能强制系统再次在我的包中包含所需的依赖项?
这是一个关于nuget pack的已知问题,即在使用PackageReference而不是packages.exe时忽略依赖项。
要解决此问题,您可以使用该链接中的解决方法:引用NuGet.Build.Tasks.Pack并使用msbuild -t:pack打包它。NuGet团队仍在积极努力改善这种情况。
我已经测试了这个解决方法,它工作得很好。但是,msbuild命令需要更多的细节。

  • 将nuget包NuGet.Build.Tasks.Pack添加到项目中,如GitHub issue中所述。
  • 使用命令msbuild.exe /t:pack。如果没有Authors属性,NuGet将出错,您可能希望将输出目录设置为默认目录以外的其他目录:
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文件中手动填写所需的依赖项。

yvgpqqbh

yvgpqqbh2#

我得到了同样的问题,并使用第二种方式刘少奇提到与帕特里克的脚本和一些小的修改,以克服这个问题。以下是步骤:
1.复制或下载下面的powershell脚本,将其命名为NuGetPackaddencies.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"
"*****"

1.配置生成后事件

cd $(SolutionDir)
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File NuGetPackDependencies.ps1 $(ProjectDir)

1.像以前一样生成和发布项目。

相关问题