mrahhal
mrahhal

Reputation: 3497

Overriding dependencies in .nuspec

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.

Upvotes: 3

Views: 1590

Answers (1)

Joseph Devlin
Joseph Devlin

Reputation: 1800

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

Upvotes: 2

Related Questions