Ben Zuill-Smith
Ben Zuill-Smith

Reputation: 3802

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.

Upvotes: 9

Views: 7674

Answers (4)

nick
nick

Reputation: 11

Add this to your proj file to use the pack target on .net framework projects.

<PackageReference Include="NuGet.Build.Tasks.Pack" Version="6.4.0">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>

if you want to run from command line then use this:

msbuild -t:restore

Upvotes: 0

Giulio Caccin
Giulio Caccin

Reputation: 3052

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:

Upvotes: 12

Ben Zuill-Smith
Ben Zuill-Smith

Reputation: 3802

I have a unique situation of needing to Register for Com Interop for and older application but also needing to pack for use in other internal applications and development in our company. I actually got this to work for a .NET Framework project from Visual Studio. Manually adding GeneratePackageOnBuild did attempt to make a package for me in VS2017. I was also able to add other .NET Core project properties such as <Authors>,<Description>, etc. I haven't tried VS2019 yet so maybe that is more restricted but I hope not.

The issue is VS2017 doesn't feed the pack target the output parameter (in this type of project). So then I tried to call pack in the After Build events but that causes a recursive loop because packing also attempts to build (dotnet and nuget both seem to call the msbuild pack target which calls a build). I then found an option -p:NoBuild=true for msbuild that allows me to call the pack target without msbuild actually building the project. Therefore I added the following command to <PostBuildEvent> and it works.

"$(MSBuildBinPath)\msbuild" -t:Pack "$(ProjectPath)" -p:PackageOutputPath="$(SolutionDir)..\packages" -p:NoBuild=true

Edit: I eventually used the following in my csproj. Calling nuget directly worked better because I had a nuspec file that was not getting merged or fully used when calling MSBuild directly.

<Target Name="CustomPack" AfterTargets="Build" Condition="'$(Configuration)'=='Release'">
  <Message Text="Custom Pack command starts ..." Importance="high" />
  <Exec Command="&quot;nuget&quot; pack &quot;$(ProjectPath)&quot; -OutputDirectory &quot;$(ProjectDir)..\..\packages&quot; -Prop Configuration=Release" />
</Target>

Upvotes: 1

LoLance
LoLance

Reputation: 28086

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.

Upvotes: 4

Related Questions