matt
matt

Reputation: 4244

How do I create an individual nuget package for each project in a solution, with project references converted to nuget dependencies?

I have a solution that looks like this.

enter image description here

I would like to generate separate nuget packages for ProjA, ProjB and ProjC, and I would like this process to detect the project references shown above in ProjC, and convert them to nuget dependencies in the nuspec file, rather than just including the dlls for ProjA and ProjB in the ProjC nuget package, which is what happens at the moment when I use the 'nuget pack' command with the -IncludeReferencedProjects option.

This option says something about automatically either including the files or adding them as a dependency, but always includes them, even though I would like them added as a dependency. Weirdly, in my realword solution, one of the project references always gets added as a dependency, but the rest don't and I can't figure out the difference.

How do I tell nuget to add them as a dependency?

I want the references to be project references as it makes it easier for development, rather than having to generate an update local nuget packages every time I want to test.

Upvotes: 5

Views: 1994

Answers (1)

LoLance
LoLance

Reputation: 28216

But always includes them, even though I would like them added as a dependency. How do I tell nuget to add them as a dependency?

Assuming you're using nuget pack command instead of dotnet pack or msbuild /t:pack.

For nuget pack xx.csproj -IncludeReferencedProjects command, to make sure the referenced projects are contained in your ProjC package with nuget dependency format, you should add corresponding xx.nuspec file in referenced projects's folder.

See the description of IncludeReferencedProjects here: If a referenced project has a corresponding .nuspec file that has the same name as the project, then that referenced project is added as a dependency. Otherwise, the referenced project is added as part of the package.

So here's the workaround:

1.For me, I open cmd.exe and navigate to ProjA's or ProjB's project folder, use nuget sepc command to create a ProjectName.nuspec for me in project folder.

2.Change the content of the ProjA.nuspec to something like this:

<?xml version="1.0"?>
<package >
  <metadata>
    <id>ProjA</id>
    <version>1.0.0</version>
    <title>This is title.</title>
    <authors>This is author.</authors>
    <owners>These are owners.</owners>
    <requireLicenseAcceptance>false</requireLicenseAcceptance>
    <description>This is description.</description>
    <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
    <copyright>Copyright 2019</copyright>
  </metadata>
</package>

Now navigate to ProjC's folder and pack the ProjC again, the IncludeReferencedProjects will work as you expected. To check this: Rename the ProjC.nupkg to ProjC.zip, and check the content of generated ProjC.nuspec file in ProjC.zip file. You'll see something like <dependency id="ProjA" version="1.0.0" />.

In addition:

1.Since you're using nuget pack command and your projects target .net framework, I suggest you use packages.config format to manage nuget packages for the three projects.

2.To make your ProjC adds the ProjA and ProjB as nuget dependencies: Apart from using IncludeReferencedProjects+ProjA.nuspec+ProjB.nuspec way, actually we can simply create a ProjC.nuspec file and add content like below to manually define the dependencies.

    <dependencies>
      <dependency id="ProjA" version="1.0.0" />
      <dependency id="ProjB" version="1.0.0" />
    </dependencies>
  </metadata>

Upvotes: 2

Related Questions