Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NuGet direct project references as versioned dependencies

I have 2 Projects Libraries in my Example Solution > Visual Studio. I directly reference one of them in the other. When I now publish my nuget package, on the dependency overview I get that my directly referenced nuget is >= 1.0.0.0, when I do it via nuget the reference means no direct reference because same solution I get the right version number under >= dependency overview. I won't change the default dependency behavior lowest. What I've tried is to update my nuspec file with dependencies / references / files elements none of them worked for me. I would like to see the same version of the given nuget in the directly referenced nuget as dependency.

like image 471
Mikatsu Avatar asked May 25 '16 18:05

Mikatsu


1 Answers

From the last sentence in you question and your emphasis on direct references makes me feel I know what you are after:

NuGet defines the -IncludeReferencedProjects option to instruct nuget.exe how it should treat referenced projects, either as dependencies or as part of the package:

  • If a referenced project has a corresponding .nuspec file that has the same name as the project, then that referenced project is added as an explicit NuGet dependency.
  • Otherwise, the referenced project is added as part of the package.

My guess is that you are after the former.

Let us simplify the problem to its most basic form: Let's say you have a solution where LibraryA references LibraryB directly as a project reference. When you build the solution, the assembly output from LibraryA is copied to LibraryB

~/
│   Solution.sln
├───LibraryA
│   │   ClassA.cs
│   │   LibraryA.csproj
│   │   LibraryA.nuspec
│   ├───bin
│   │   ├───Debug
│   │   │       LibraryA.dll
│   │   │       LibraryA.pdb
│   │   └───Release
│   └───Properties
│           AssemblyInfo.cs
└───LibraryB
    │   ClassB.cs
    │   LibraryB.csproj
    │   LibraryB.nuspec
    ├───bin
    │   ├───Debug
    │   │       LibraryA.dll
    │   │       LibraryA.pdb
    │   │       LibraryB.dll
    │   │       LibraryB.pdb
    │   └───Release
    └───Properties
            AssemblyInfo.cs

Setup

For illustration purposes I will use the pattern [assembly: AssemblyVersion("1.0.*")] in my AssemblyInfo.cs files and will do a couple of separate builds on each project to ensure my assemblies get some different interesting versions.

Make sure that each project contains a .nuspec file with the same name as the project. This is specially important for project LibraryA, as it is the project being referenced by LibraryB. I'll do it for both as good practice. Let us use a basic template for now:

In the .nuspec below, the replacement tokens $id$ and $version$ will get their values inferred when you run nuget.exe against a .csproj file that has been built.

<?xml version="1.0"?>
<package >
  <metadata>
    <id>$id$</id>
    <version>$version$</version>
    <authors>The author... (**mandatory element**)</authors>
    <description>Your description... (**mandatory element**)</description>
  </metadata>
</package>

Use nuget pack -IncludeReferencedProjects

Now, I am going to run nuget.exe in the command line from the solution directory (~) on project LibraryB:

PS> nuget pack .\LibraryB\LibraryB.csproj -IncludeReferencedProjects -Verbosity detailed
    Attempting to build package from 'LibraryB.csproj'.
    Packing files from '~\LibraryB\bin\Debug'.
    Using 'LibraryB.nuspec' for metadata.
    Add file '~\LibraryB\bin\Debug\LibraryB.dll' to package as 'lib\net451\LibraryB.dll'

    Id: LibraryB
    Version: 1.0.5993.6096
    Authors: The author... (**mandatory element**)
    Description: Your description... (**mandatory element**)
    Dependencies: LibraryA (= 1.0.5993.7310)

    Added file 'lib\net451\LibraryB.dll'.

    Successfully created package '~\LibraryB.1.0.5993.6096.nupkg'.
PS>

Generated packages

The above command will create a NuGet package LibraryB.1.0.5993.6096.nupkg with a explicit NuGet dependency to LibraryA.1.0.5993.7310.nupkg.

When you inspect the contents of LibraryB.1.0.5993.6096.nupkg, you will see that the .nuspec generated by nuget.exe will have replaced all the $version$ replacement tokens to that of the actual versions used.

One last thing, the command above will create the NuGet package only for LibraryB but obviously you can create the one for LibraryA just by running it again targeting LibraryA.csproj


I hope this is what you were after, or that at least sheds some light to what you can do.

like image 59
Fernando Espinosa Avatar answered Sep 18 '22 11:09

Fernando Espinosa