Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building NuGet packages from Visual Studio

I'm trying to share an internal company assembly via NuGet packages and a private source. This assembly targets .NET Framework 4.6.1. I want these NuGet packages to pack automatically from Visual Studio during the release build. I see I can add <GeneratePackageOnBuild>true</GeneratePackageOnBuild> to .csproj. I'm not sure if this is a .NET Standard-specific property but it seems to partially work. However, when I build, I get

error MSB4044: The "GetPackOutputItemsTask" task was not given a value for the required parameter "PackageOutputPath".

I've been trying to learn how to pass this parameter from within Visual Studio but I don't see a lot of documentation on parameters except when calling it from the command line manually. Is there an easy way to do this from within Visual Studio? Am I going about this wrong?

Edit: This is using a .NET Framework class library. I can run the pack command from the command line giving it the required parameters with /p:PackageOutputPath="path\here". It seems this might have been designed for .NET Core and Standard projects and Visual Studio might not handle packing .NET Framework projects.

like image 814
Ben Zuill-Smith Avatar asked Oct 30 '19 17:10

Ben Zuill-Smith


2 Answers

Am I going about this wrong?

This GeneratePackageOnBuild property is not something for .net framework projects. Create a new .net Standard or .net Core class library project, right-click the project you'll see a Pack command(not available for .net framework).

enter image description here

If we click the button, VS will pack that assembly into a nuget package. And if someone doesn't want to click that button every time manually, go Project=>Properties=>Package we can see the Generate Nuget Packages on build checkbox. Enable it, and then the nuget package will be created after every build.

Actually, enabling that checkbox in VS will add statement <GeneratePackageOnBuild>true</GeneratePackageOnBuild> to xx.csproj, but the Pack button or Package tab in Project=>Properties are not available for .net framework projects.

So I'm afraid the answer is negative, you should't use that property for .net framework projects.(I tested the property in VS2017 and VS2019, it all just did nothing, can't reproduce the partial work mentioned in your question)

Is there an easy way to do this from within Visual Studio?

You need to use nuget pack command to do that as Lex Li says. And to do this in VS automatically, you can consider configuring that command in Post-Build-Event or using custom after-build target to run that command.

Since you want these NuGet packages to pack automatically from Visual Studio during the release build. You can try adding this script into your xx.csproj file:

  <Target Name="CustomPack" AfterTargets="build" Condition="'$(Configuration)'=='Release'">
    <Message Text="Custom Pack command starts ..." Importance="high"/>
    <Exec Command="nuget pack $(MSBuildProjectFile) -Properties Configuration=Release"/>
  </Target>

To run this script successfully, you need to download the nuget.exe and add the path of it to Environment Variables. Or use the full path like "C:\SomePath\Nuget.exe" pack ...

If the build in release mode succeeds, you'll see a xx.nupkg file in project folder.

In addition:

1.For more details about nuget pack command please see this document.

2.And don't forget to create a xx.nuspec file in project folder to avoid encountering warnings like NU5115(xxx was not specified). Similar issue see here.

like image 61
LoLance Avatar answered Sep 27 '22 18:09

LoLance


To enable on-build packing in a "non SDK" project, using old .NET framework (eg:. 4.5.1) and visual studio 2019 without using custom Target. you need to do the following step:

  1. add a first PropertyGroup tag on the csproj
  2. add minimal tags Authors and PackageOutputPath
  3. check that the same PropertyGroup has GeneratePackageOnBuild

Now the variables will be passed to the internally triggered msbuild -t:Pack command.

Here an example of working configuration, please make sure this will be the first <PropertyGroup> of the .csproj:

  <PropertyGroup>
    <Title>packageid</Title>
    <Description>your description</Description>
    <Version>1.1.1</Version>
    <ReleaseNotes>New package system</ReleaseNotes>
    <Authors>authors</Authors>
    <Owners>owners</Owners>
    <Copyright>your copyrights</Copyright>
    <PackageOutputPath>bin\Package</PackageOutputPath>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

If you are looking for the references, here:

  • how to create a nuget package with msbuild
  • all possible tags to set on the csproj on the first PropertyGroup
like image 37
Giulio Caccin Avatar answered Sep 27 '22 20:09

Giulio Caccin