0

I have two class library projects (ClassLibA and ClassLibB) in my solution, and I would like to package them together into a single NuGet package, rather than creating separate packages for each library. The goal is to include both libraries in one .nupkg file, so when someone installs this package, they can reference both ClassLibA and ClassLibB.

Here's the setup I currently have:

ClassLibA and ClassLibB are both .NET Framework 4.7.2 projects. I’ve created a third project (MergerClassLin) and added project references to ClassLibA and ClassLibB in it. I want to ensure that when I build the MergerClassLin project, both ClassLibA and ClassLibB are included in the resulting NuGet package. I’ve tried using ProjectReference in the .csproj of MergerClassLin, but when I generate the NuGet package, it only includes the MergerClassLin.dll and doesn’t package ClassLibA and ClassLibB.

Here's what I’ve done so far in MergerClassLin.csproj:

xml Copy code

<PropertyGroup>
    <TargetFramework>net472</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
    <IncludeReferencedProjects>true</IncludeReferencedProjects>
</PropertyGroup>

<ItemGroup>
    <ProjectReference Include="..\ClassLibA\ClassLibA.csproj">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </ProjectReference>
    <ProjectReference Include="..\ClassLibB\ClassLibB.csproj">
        <PrivateAssets>all</PrivateAssets>
        <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </ProjectReference>
</ItemGroup>
The NuGet package builds, but it only contains MergerClassLin.dll. I want to include both ClassLibA.dll and ClassLibB.dll in the NuGet package as well.

What’s the best way to combine multiple class libraries into a single NuGet package? Is there something I’m missing in the .csproj configuration or packaging process?

Any help would be appreciated!

2 Answers 2

1

You are missing some lines that should go in the .csproj file:

  <Target Name="CopyLibraries" AfterTargets="Build">
    <Copy SourceFiles="@(ProjectReference->'%(TargetDir)%(TargetName).dll')" DestinationFolder="$(OutputPath)" />
  </Target>

  <ItemGroup>
    <None Include="$(OutputPath)\ClassLibA.dll" Pack="true" PackagePath="lib\net472\" />
    <None Include="$(OutputPath)\ClassLibB.dll" Pack="true" PackagePath="lib\net472\" />
  </ItemGroup>

these lines are use for:

  • The CopyLibraries target copies the DLLs for both libraries into the output directory after building MergerClassLin.
  • The None Include statements ensure that these DLLs are packaged into the resulting .nupkg under the appropriate path (lib/net472).

Otherwise, you should follow the manual way explained in the comment above.

Sign up to request clarification or add additional context in comments.

Comments

1

You'll need to make your own .nuspec file and run the nuget CLI (either from nuget.exe or from dotnet) yourself to create the nupkg file. In your nuspec file you'll need to manually map the two project's dll files into the files/file elements. The dlls files should be sourced from ClassLibA's and ClassLibB's respective bin directories.

You can use Nuget Package Explorer to help make and visualize the structure.

The automatic Nuget package creation in project files can only create a nuget package for that one project - any project references are treated as nuget references.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.