Christian Findlay
Christian Findlay

Reputation: 7682

How to modify .nuspec file before NuGet pack in Visual Studio

Visual Studio makes creating NuGet packages easier these days by automating the process of creating the .nuspec file. It means that version dependencies etc. are automatically created and you can simply set up the textual values here:

enter image description here

So, most of the work is already done by the time the NuGet package is built. However, there are external assemblies that I need to add to the .nuspec file before it is built. I know I can add external files in the files collection of the outputted .nuspec file (in obj\Release):

enter image description here

My question is, what build event or otherwise should I use to edit the .nuspec after it is created, but before the pack command is called by msbuild or Visual Studio? Or, should I just run a separate build process afterwards?

Note: a bonus would be to find a tool which I can call that will add the files collection. Without that, I'm guessing I will need to write powershell code to add the files...

Upvotes: 4

Views: 5355

Answers (1)

Peska
Peska

Reputation: 4140

You could use Pack element in csproj file: https://learn.microsoft.com/en-us/nuget/reference/msbuild-targets#including-content-in-a-package

Add something like this to your project:

<ItemGroup>
  <Content Pack="True"
           PackagePath="lib\netcoreapp2.2"
           Include="C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All\2.1.2\Microsoft.AI.DependencyCollector.dll" />
</ItemGroup>

This will add a Microsoft.AI.DependencyCollector.dll library to your package:

package content

This way you can add whatever you like to your library.

Upvotes: 4

Related Questions