我尝试从 .NET 4.7.2 类库 (VS2017) 制作 NuGet 包,但生成的 NuGet 包令人惊讶地没有显示依赖项(这是一个错误)。
我的设置是这样的:
nuget.exe pack命令应该自动填写所需的依赖项 - 这也曾经是早期的情况(在另一个项目中)。然而,当时我在类库中使用了 packages.config 而不是 packageReferences。这会改变什么吗?
发生什么事了?
如何强制系统再次将所需的依赖项包含在我的包中?
备注:
“nuget.exe Restore” 和稍后的 “nuget.exe pack” 作为其构建逻辑的一部分。
如何强制系统再次将所需的依赖项包含在我的包中?这是一个关于
nuget pack 在使用 PackageReference 而不是 packages.config 时忽略依赖关系的已知问题。
要解决此问题,您可以使用引用NuGet.Build.Tasks.Pack
并使用
msbuild -t:pack
打包它的链接中的解决方法。 NuGet 团队仍在积极致力于改善这种情况。我已经测试了这个解决方法,效果很好。但是,msbuild 命令需要更多细节。
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
文件中手动填写所需的依赖项。
Pattrick'script 以及一些小的修改来克服这个问题。以下是步骤:
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"
"*****"
cd $(SolutionDir)
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File NuGetPackDependencies.ps1 $(ProjectDir)