2

I have two .Net projects:

  • One project is is a .Net standard project containing some DTOs; call it the "core library". This is intended to be published as a NuGet package
  • The other is a .Net framework project that includes the NuGet package of the core library. Call it "the app"

Using TFS we are able to compile the core library, publish the resulting NuGet library and then launch the app's build which restores the NuGet reference to the core library. Everything works as expected.

The problem is that when developing the core library + the app we have frequent compiles of the core and cannot wait for the entire core library build to finish in order to update the app's local reference. Moreover we must push complete commits of the core library (so we cannot push one DTO at the time before testing them in the app).

So I was wondering if exists a way in Visual Studio and Nuget to publish/install the core's Nuget library in the local user's .nuget folder in order to let it be visible automatically to the app's project on the next build.

Can you help me with this?

gvdm
  • 3,006
  • 5
  • 35
  • 73
  • If both projects are built locally, why not use convenient project reference? And if you want a nuget package visible automatically to the app's project, you can place the nuget package in any package source path defined in nuget.config as mm8 suggests. Also you can do this by UI, in VS IDE, go Tools=>nuget package manager=>package manager settings to add a new package source, it can be a local folder like `C:\CustomPackages`, and then any nuget package in this folder can be visible for the app's `nuget package manager UI`. (Also you can set the path as local cache `C:\xxx\.nuget\packages`). – LoLance Sep 05 '19 at 08:04
  • My problem is to automatize the `nuget push` command to the local feed, that in my case should be `%userprofile%\.nuget\packages`. I cannot ask any person in the company to manually copy the core package to the local feed in order to see the changes in the linked app – gvdm Sep 05 '19 at 08:21
  • Maybe what you want to do is similar to [what this user do](https://stackoverflow.com/questions/57788333/visual-studio-2017-during-build-exited-with-code-9009-error-code-msb3073)? Run a post-build event or target to execute the nuget push command automatically after every successful build of the core library project. – LoLance Sep 05 '19 at 08:31
  • Hi , any update for this issue? Or maybe I misunderstand your last comment :( – LoLance Sep 09 '19 at 06:32
  • Sorry, I thought I answered to your answer! – gvdm Sep 10 '19 at 09:38

2 Answers2

3

Starting from the answer of Lance Li-MSFT I made some changes for my needs. In fact, locally I do not have nuget.exe installed but only the .Net Core runtime, so I'm using the dotnet pack command instead of nuget.exe pack and dotnet nuget push instead of nuget.exe push. Moreover before pushing locally the updated NuGet package I delete the old package with the same version, else dotnet nuget push will not overwrite the package.

I accept Lance Li-MSFT's answer as it was the starting point for my solution and works for who have nuget.exe locally.

My solution is below

corelib.csproj

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <TargetFramework>netstandard2.0</TargetFramework>
    <GeneratePackageOnBuild>true</GeneratePackageOnBuild>
  </PropertyGroup>

  <Target Name="PublishNuGetLocally" AfterTargets="Build">
    <Exec Command="dotnet pack $(ProjectPath)"/>
    <Exec Command="dotnet nuget delete --source $(UserProfile)\.nuget\packages $(PackageId) $(PackageVersion) --non-interactive" ContinueOnError="WarnAndContinue"/>
    <Exec Command="dotnet nuget push --source $(UserProfile)\.nuget\packages $(ProjectDir)bin\$(ConfigurationName)\$(PackageId).$(PackageVersion).nupkg" />
  </Target>
  [...]

</Project>

As a side note, I did not have to configure the package source in Visual Studio because the global Nuget cache (at the path %UserProfile%\.nuget\packages) is automatically used as the first place where to search for the NuGet packages.

gvdm
  • 3,006
  • 5
  • 35
  • 73
  • In my experience, I still needed to add `%UserProfile%\.nuget\packages` to my package sources. Without it, browsing for packages using Nuget Package Manager would not find any of my packages. – Dave Oct 19 '22 at 13:36
2

So I was wondering if exists a way in Visual Studio and Nuget to publish/install the core's Nuget library in the local user's .nuget folder in order to let it be visible.

Before publishing nuget package(nuget push) to one location locally, we should add the path as package source. So we need to set the path of local packages as package source firstly. The content in nuget.config file is corresponding to the Package Source UI in VS, so you have two ways to do it.

1.In VS,go Tools=>nuget package manager=>package manager settings=>package source, click the green button to define new package source.

enter image description here

2.Or we can find the nuget.config file for current user, see this document. The UI operation in #1 actually help define the source in the nuget.config file in C:\Users\xxx\AppData\Roaming\NuGet, see:

enter image description here

So we can directly edit this file to set our new package source, after that save the file and restart VS, we can see new defined source in UI.

My problem is to automatize the nuget push command to the local feed, that in my case should be %userprofile%.nuget\packages. I cannot ask any person in the company to manually copy the core package to the local feed in order to see the changes in the linked app

After adding the local feed to package source, then you can use nuget push command to publish packages to the feed.

You can define a custom target similar to this in your core library project file(xx.csproj) to automatically pack and push the package automatically:

  <Target Name="CustomTarget" AfterTargets="build">
    <Exec Command="nuget.exe pack xx.csproj"/>
    <Exec Command="nuget.exe push -source xxx path\xxx.nupkg"/>
  </Target>

And you can also add Conditions to this target,<Target Name="CustomTarget" AfterTargets="build" Condition="$(Configuration)=='Debug'">. You can control in which configuration VS should run this target to pack and push for you.

In addition:

For .net core library projects, VS have a option to create. the nuget package. You can right-click the project in VS and select Pack button.So you can also define a .bat file for this project to do the nuget push. The process is build the project=>use pack option to easily get nuget package=>run the .bat to automatically push.

Hope it helps.

LoLance
  • 25,666
  • 1
  • 39
  • 73
  • 1
    Starting from this answer I added some details for my needs. See https://stackoverflow.com/a/57868239/2135719 – gvdm Sep 10 '19 at 09:53