Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a Nuget package without its dependencies to be added as references

I need to create a Nuget package out of my library .net framework project.

The problem is that my project depends on many other dlls which I do not want my Nuget users to see. So I want people to install my package and see only my project dll in the references list, while all its dependencies will be added to bin folder only.

Actually, this is what happens when I reference this dll in any project- I can see it in my references list, and all its dependencies are simply copied to bin folder right after building. Can this behaviour be achieved with this project as Nuget ?

like image 880
Michael Mualem Avatar asked Oct 17 '25 11:10

Michael Mualem


1 Answers

Create a Nuget package without its dependencies to be added as references

Since you do not want your Nuget users to see the other dlls, so you can not set those dlls file as dependencies, which will be added to the references list by default.

To resolve this issue, we need add those dlls file in the other folder in the .nuspec file and add a function to copy this dlls file to the bin folder when we add this nuget package to the project. You can follow below steps:

  1. Add a xx.targets file in your project folder(The one you used to create nuget package ), make sure the name of the target file is the same name as the package id(TestDemo is my package ID, so the name of .targets is TestDemo.targets).

  2. Add below code in the targets file:

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
     <ItemGroup>
      <None Include="$(MSBuildThisFileDirectory)*.dll">
         <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
     </ItemGroup>
    </Project>
    

Note: The path of "$(MSBuildThisFileDirectory)" should be relative path, if you are not familiar with it, you can use the absolute path.

  1. In the nuspec file, add required file to the Build directory along with the targets file.

      <files>
        <file src="<ThoseDllsPath>\*.dll" target="Build\" />
        <file src="TestDemo.targets" target="Build\" />
        <file src="bin\Debug\TestDemo.dll" target="lib\462" />
      </files>
    
  2. Pack this package, then add it on other project to test, it work fine.

enter image description here

enter image description here

Hope this helps.

like image 148
Leo Liu-MSFT Avatar answered Oct 19 '25 07:10

Leo Liu-MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!