3

I want to package my project by packing the project file .csproj and not the .nuspec. But I also want to explicitly specify the dependencies.

The problem is that when packing the project file (so that I can use the props from my project) the <dependencies> in the .nuspec file is not being used.

I'm doing this so that I can change my props in one place (in AssemblyInfo.cs) without having to change the .nuspec also with every version. I know I can pack the .nuspec where I'll have full control but this already defeats the purpose. (I'm actually doing exactly this for now until I can override the dependencies)

Is there a way to override the dependencies explicitly in the .nuspec?

EDIT:

Incorporating developmentDependency this almost worked. I'm using dependency groups in the nuspec and for some reason nuget is flattening it after packing (of course only when packing the csproj). So this: (just for demonstration)

<dependencies>
  <group>
    <dependency id="Some.Core" version="0.1.0" />
  </group>
  <group targetFramework="net4">
    <dependency id="Microsoft.Bcl.Async" version="1.0.168" />
  </group>
  <group targetFramework="net45">
    <dependency id="Some" version="0.1.0" />
  </group>
</dependencies>

becomes this:

<dependencies>
  <dependency id="Some.Core" version="0.1.0" />
  <dependency id="Microsoft.Bcl.Async" version="1.0.168" />
  <dependency id="Some" version="0.1.0" />
</dependencies>

And because of that, when using the same package as dependency in different groups an error shows up when packing:

An item with the same key has already been added.

mrahhal
  • 3,322
  • 1
  • 22
  • 41
  • So you want to use both the csproj and the nuspec to pack a project? And the issue. Whatever is in the nuspec will override the csproj/assemblyInfo when packing but what exactly is your problem. Are you missing dependencies? Are you trying to exclude others? – Joseph Devlin May 17 '15 at 20:38
  • @Kazuo I want to use the csproj (which I'm sure uses some sections of the nuspec). The problem is that the dependencies that I specified in the nuspec will not be picked unless I use the nuspec directly. – mrahhal May 18 '15 at 08:36
  • So you are trying to pack this up, and then consume your new package in another project? – Joseph Devlin May 18 '15 at 14:03
  • @Kazuo Yeah, I can only approach it like this because we have certain needs that require us to "depend" on a couple more of packages only when targeting a certain framework from the other project (like net4). It's all well and fine if not for the flattening, I really have no idea why nuget does that in this case. – mrahhal May 18 '15 at 15:35

1 Answers1

2

Your csproj and nuspec file should work together when packing if they have the exact same name and live in the same directory e.g.

  • ProjectDirectory
    • MyProject.csproj
    • MyProject.nuspec

So calling

nuget pack MyProject.csproj

Should take all the implicit data from your csproj/assemblyInfo/packages.config etc and then combine that metadata with what is specified in your nuspec. When it comes to dependencies you can include non implicit dependencies using the normal syntax in your nuspec file as follows

<dependencies>
  <dependency id="RouteMagic" version="1.1.0" />
  <dependency id="RouteDebugger" version="1.0.0" />
</dependencies>

And you can exclude some implicit dependencies using this syntax in your projects packages.config

"By adding a developmentDependency="true" attribute to a package in packages.config, nuget.exe pack will no longer include that package as a dependency."

Sources

Joseph Devlin
  • 1,754
  • 1
  • 26
  • 37
  • I'm pretty sure I tried something like that, but I'll try again and see the results. If developmentDependency works I'll have to go with it though it will be such a pain. – mrahhal May 18 '15 at 10:21