Direct links to NuGet project as version dependent versions

I have 2 project libraries in my example Solution> Visual Studio. I directly refer to one of them in another. When I publish my nuget package now, in the dependency overview I get that my direct reference nuget is >= 1.0.0.0 , when I do this via nuget, the link means there is no direct link, because the same solution I get the right version number under> = dependency overview, I will not change the default dependency behavior lowest . I tried updating my nuspec file using dependency / link / file elements, none of them worked for me. I would like to see the same version of this nuget in upright nuget as a dependency.

+5
source share
1 answer

From the last sentence of your question and your emphasis on direct links, I feel like I know what you're looking for:

NuGet defines the -IncludeReferencedProjects option to tell nuget.exe how it should handle reference projects as dependencies or as part of a package:

  • If the referenced project has a matching .nuspec file whose name matches the project name, this referenced project is added as an explicit NuGet dependency.
  • Otherwise, the specified project will be added as part of the package.

I think you are after the first.

Let's simplify the task to its main form: let's say you have a solution in which LibraryA refers to LibraryB directly as a reference to the project. When building a solution, the assembly output from LibraryA 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 

Customization

For illustration purposes, I will use the [assembly: AssemblyVersion("1.0.*")] Template in my AssemblyInfo.cs files and make a couple of separate assemblies for each project so that my assemblies get several different interesting versions.

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

In .nuspec below, the replacement tokens $id$ and $version$ will get their values printed when you run nuget.exe for the .csproj file that was 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'm going to run nuget.exe at the command line from the solutions directory ( ~ ) in the LibraryB project:

 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 the NuGet LibraryB.1.0.5993.6096.nupkg package with the explicit NuGet dependency on 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 replace all $version$ replacement tokens with those of the actual versions used.

LibraryB : the command above will only create a NuGet package for LibraryB , but it is obvious that you can create a package for LibraryA by simply running it again for LibraryA.csproj


I hope this is what you wanted, or at least sheds light on what you can do.

+7
source

All Articles