christok
christok

Reputation: 1107

Force download of specific nuget package to local packages folder

I have a .NET Core Visual Studio 2017 solution that references Json.Net 12.0.1. Everything works great in my dev environment. When I run dotnet restore, Json.Net is downloaded to Newtonsoft.Json.12.0.1 in the solutions packages folder.

However, when I run restore on my build server the solution wants to pick up a version of Json.Net from the nuget global cache.

I can force download of all packages to a local packages folder, but this ends up using a different naming convention (Newtonsoft.Json/12.0.1), and of course re-downloads all that stuff that exists in globals anyway. So it just creates a lot of overhead and still doesn't work.

I suppose I could work some hocus pocus in my .csproj files in oder to provide a different hint path for Json.Net but this seems overly complicated.

Is there a way to force Nuget to download Json.net (or any package) to a specific folder in local packages, or otherwise resolve this issue using CLI tools?

Again, this is .NET Core so there is no packages.config.

Thanks!

Upvotes: 8

Views: 9560

Answers (1)

Leo Liu
Leo Liu

Reputation: 76928

Is there a way to force Nuget to download Json.net (or any package) to a specific folder in local packages, or otherwise resolve this issue using CLI tools?

Yes, you can place a NuGet.Config file next to the solution with the following content:

<?xml version="1.0" encoding="utf-8"?>
  <configuration>
    <config>
      <add key="globalPackagesFolder" value=".\packages"/>
    </config>
</configuration>

Then restart the Visual Studio, and re-open the solution and right click on the solution, select Restore NuGet Packages. All the packages are stored in the project repository.

I'm wondering if I can direct nuget only to download a single package to local packages folder.

Yes, you can download the nuget package with following command line:

nuget.exe install YourPackageName -source "https://api.nuget.org/v3/index.json" -OutputDirectory "D:\Test"

Hope this helps.

Upvotes: 6

Related Questions