我尝试从.NET 4.7.2类库(VS2017)中制作一个NuGet程序包,但是生成的NuGet程序包令人惊讶地显示没有依赖项(这是一个错误)。
我的设置是这样的:
nuget.exe pack 命令应自动填充所需的依赖项-在其他项目中,以前也是如此。但是,那时我在类库中使用了 packages.config 而不是 packageReferences 。这会改变什么吗?
这是怎么回事?
如何强制系统在软件包中再次包含所需的依赖项?
注意:
该软件包是由TeamCity构建服务器上的MSBuild脚本构建的(不带VS2017)。该构建脚本同时调用“ nuget.exe restore” 和更高版本的“ nuget.exe pack” 作为构建逻辑的一部分。
MSBuild版本15.7
nuget.exe是版本4.6.2
答案 0 :(得分:1)
如何强制系统在软件包中再次包含所需的依赖项?
这是关于nuget pack is ignoring dependencies when using PackageReference instead of packages.config的已知问题。
要解决此问题,您可以使用以下解决方法,NuGet团队仍在积极致力于改善此情况:
打包您的C#类库,该库通过以下方式管理您的依赖项
PackageReference
在csproj本身中,请添加参考
NuGet.Build.Tasks.Pack
( https://www.nuget.org/packages/NuGet.Build.Tasks.Pack/)并运行msbuild /t:pack
从命令行开始。
我已经测试了此替代方法,它工作正常。为确保此解决方法正常运行,我们需要注意以下要点:
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
文件中手动填写所需的依赖项。
希望这会有所帮助。
答案 1 :(得分:1)
我遇到了同样的问题,并使用Pattrick'script和刘易奥(Leo Liu)进行第二次提及,并做了一些小的修改来克服此问题。步骤如下:
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生成后事件
cd $(SolutionDir)
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File NuGetPackDependencies.ps1 $(ProjectDir)