Automatically pack dependencies into multiple target frameworks?

Topics: General
Apr 23, 2013 at 4:10 PM
I went through an exercise recently of trying to have a class library produce a NuGet package for both net40 and net45. Using the "nuget pack" command I could not figure out a way to get the dependencies automatically added to the package under the respective target frameworks. Is it possible without a .nuspec file?

With a .nuspec file, I was able to do it with this syntax:
  <group targetFramework="net40">
    <dependency id="Newtonsoft.Json" version="4.5.7" />
  <group targetFramework="net45">
    <dependency id="Newtonsoft.Json" version="4.5.7" />
Apr 23, 2013 at 5:41 PM
If you have the exact same dependency for different target frameworks, why do you want it to be in separate group?
Apr 23, 2013 at 9:03 PM
Edited Apr 24, 2013 at 4:26 PM
I found a shortcoming with the "nuget pack" command. When attempting to construct a NuGet package from a .csproj file, it attempts to add dependencies for each target framework; I kept seeing the message "An item with the same key has already been added."

The command is being executed as a Target within the .csproj file. Here are the relevant elements:
  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release 4.5|AnyCPU' ">
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release 4.0|AnyCPU'">
  <Target Name="Package">
    <RemoveDir Directories="$(PackageDir)" />
    <MakeDir Directories="$(PackageDir)" />
    <!-- Package the project -->
    <Exec WorkingDirectory="$(BuildDir)" Command="NuGet.exe pack -Symbols -OutputDir &quot;$(PackageDir)&quot;" />
Before targeting multiple frameworks, the command worked correctly and used packages.config to add the dependencies. It seems like the bug exists when adding dependencies for multiple frameworks. Instead of failing when a dependency already exists, just skip over it.