Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dependencies in Nuspec vs. csproj

I have a Dotnet Core 2.1 project which has both a nuspec and a csproj file - one major hassle is that the csproj describes dependencies like this:

  <ItemGroup>
    <PackageReference Include="Refit" Version="4.6.16" />
      <PackageReference Include="Microsoft.AspNetCore.All" Version="2.1" />
      <PackageReference Include="Refit.HttpClientFactory" Version="4.6.16" />   
  </ItemGroup>

While the nuspec does this:

 <dependencies>
      <dependency id="Refit" version="4.6.16" />
      <dependency id="Refit.HttpClientFactory" version="4.6.16" />
      <dependency id="Microsoft.AspNetCore.All" version="2.1" />
    </dependencies>

Both are easily out of sync and keeping the same information twice is annoying. Is there a way to avoid that?

like image 603
Christian Sauer Avatar asked Jun 22 '18 07:06

Christian Sauer


1 Answers

There is certain to be a easy way for this, just use dotnet pack instead of nuget pack and .csproj files instead of .nuspec files.

dotnet pack supports 2 ways to specify the nuget package properties.

  1. The legacy way: using .nuspec file, which would disable the 2nd way
  2. The new way: specifying them in .csproj file

dotnet pack supports both ways but you must add a NuspecFile property to reference the .nuspec file and there are a lot of bugs and feature missings for the legacy way, which means you can only use the new one.

dotnet pack executes restore and build on the project and packs it with a automatically generated .nuspec file resolving all nuget metadata properties in .csproj as .nuspec properties and all projects references as nuget package references (This is not available with manually specified .nuspec file), so that versioning, dependency, and package file structure things can be automatically ensured.

My own library could be an example. Version and dependency things are specified for only once at where they are supposed to be and there are no longer any annoying duplicate configurations. Executing dotnet pack on the solution directory would generate all good .nupkgs on the dist directory.

like image 150
Alsein Avatar answered Oct 31 '22 04:10

Alsein