Search code examples
c#visual-studio-2017nugetnuspecmsbuild-15

How to remove the dependencies section from nuspec file in the generated nuget package using msbuild 15


I use msbuild v15.8.168.64424 (vs 2017.8) commandline to build nuget package for console application. I used ILMerge to merge all dependencies, so I get only one exe file without dependencies.

When runing the command

        msbuild project1.csproj /t:build /p:configuration=release /p:IsTool=true

I get the nuget package, but when inspecting it, I find the dependencies section.

     <dependencies>
           <group targetFramework=".NETFramework4.5">
            <dependency id="lib1" version="1.3.0" exclude="Build,Analyzers" />
            <dependency id="lib2" version="2.3.0" exclude="Build,Analyzers" />              
          </group>
        </dependencies>

I want the `dependencies section to be empty (because I merged all the dependencies).

I can use nuspec file to generate the package without dependencies section, but the drawback is passing manually all the metadata which is included in csproj. I didn't find a property for the dependencies in pack target inputs

The Question:

How I remove the dependencies section from the nuspec file which is included in the generated nuget package?


Solution

  • You can set the metadata PrivateAssets="All" on the package or project references (<PackageReference> / <ProjectReference>) in the csproj file to exclude them as NuGet dependencies.

    Note that this change needs a restore so perform a NuGet restore in between or add -restore to the MSBuild invocation.