NuGet package shows no dependencies?
I got the same issue and use 2nd way leo Liu mention with Pattrick'script and some small modification to overcome this issue. Below are steps:
- Copy or download powershell script below, name it NuGetPackDependencies.ps1 and add it to your solution (Because I want to reuse the same script for many projects in this solution)
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-Build Event
cd $(SolutionDir)
powershell.exe -ExecutionPolicy Bypass -NoProfile -NonInteractive -File NuGetPackDependencies.ps1 $(ProjectDir)
- Build and publish your project as you did before.
How can I force the system to again include the needed dependencies in my package?
This is a known issue about nuget pack is ignoring dependencies when using PackageReference instead of packages.config.
To resolve this issue, you can use the following workaround, and NuGet team are still actively working on improving this scenario:
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 runmsbuild /t:pack
from the command line.
I have test this workaround, it works fine. To make sure this workaround works fine, we need to pay attention to the following points:
- Need to add the nuget package
NuGet.Build.Tasks.Pack
to the project. - Need to add properties
/p:PackageOutputPath="D:\TesterFolder" -p:Authors=tester
- Use the command
msbuild.exe /t:pack
, like:msbuild.exe /t:pack "MyTestLibrary.csproj" /p:PackageOutputPath="D:\TestFolder" -p:Authors=tester
Besides, if you want to use .nuspec
file to create the nuget package, you should use the following .nuspec
file:
<?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>
Then we could use nuget.exe pack
to create the nuget package. But, using this method, we have to manually fill in the needed dependencies in the .nuspec
file.
Hope this helps.