I am trying to automate process Build -> Pack -> Add for nuget package in VS2022. I have created SDK type project and using default settings to create nuget package. After executing Pack Command in Release mode, I have the following output in /bin/Release/ folder:
net8.0/ contains library files
my-package.nupkg
So far so good. Now I would like to execute nuget add command automatically after the Pack command completed, and publish the nuget package to my local repository. When I start power shell in the folder where csproj file is, and execute the following command:
nuget add bin/Release/my-package.0.0.1.nupkg -Source 'c:\Users\MyUser\OneDrive\nuget\packages\'
the command completes successfully, and I can see and use the package from other solutions.
However, if I try to automate this, it does not work. In csproj file:
<Target Name="AddPackage" AfterTargets="Pack" Condition=" '$(Configuration)' == 'Release'">
<Message Text="Adding nuget file to local repository..." Importance="high" />
<Exec Command="nuget add bin/Release/$(PackageId).$(PackageVersion).nupkg -Source 'c:\Users\MyUser\OneDrive\nuget\packages\'" />
</Target>
Output window shows error:
1>Adding nuget file to local repository...
1>The given path's format is not supported.
error MSB3073: The command "nuget add bin/Release/my-package.0.0.1.nupkg -Source 'c:\Users\MyUser\OneDrive\nuget\packages\'" exited with code 1.
I also tried without relative path:
Command="nuget add $(PackageId).$(PackageVersion).nupkg -Source ....
Command="nuget add my-package.0.0.1.nupkg -Source ....
and copied manually the nupkg file in csproj folder, and same error.
Anyone knows why is it working from command line and not from VS, and how can I automate this task from VS Build/Pack process?
I am able to reproduce your issue:
I think you need something like this:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<GeneratePackageOnBuild>True</GeneratePackageOnBuild>
<PackageId>ClassLibrary1</PackageId>
<PackageVersion>1.0.0</PackageVersion>
</PropertyGroup>
<Target Name="AddPackage" AfterTargets="Pack" Condition=" '$(Configuration)' == 'Release'">
<Message Text="Adding nuget file to local repository..." Importance="high" />
<!--<Exec Command="nuget add bin/Release/$(PackageId).$(PackageVersion).nupkg -Source 'c:\Users\MyUser\OneDrive\nuget\packages\'" />-->
<Exec Command="nuget add "$(ProjectDir)bin\Release\$(PackageId).$(PackageVersion).nupkg" -Source "C:\Path\To\Your\NuGet\Package\Source"" />
</Target>
</Project>
And you can see that it success on my side:
The crucial thing is the file is XML format.