fharreau
fharreau

Reputation: 2347

Download nuget package without project with the dotnet cli

Given the following file packages.config:

<packages>
  <package id="package.a" version="X.Y.Z" />
  <package id="package.b" version="A.B.C" />
</packages>

I used to run the following command:

nuget.exe restore .\packages.config -PackagesDirectory .\Dependencies

That downloads the specified packages into the Dependencies folder without having a csproj file.

I am looking for a way to do the same with the dotnet cli (because I need it to run on non-windows environment), but I can't find how.

I tried several things without success:

dotnet restore --config-file .\packages.config --packages dependencies
dotnet restore .\packages.config --packages dependencies
dotnet nuget restore .\packages.config -PackagesDirectory .\dependencies\

The closest I can get was by replacing my old packages.config with a project file with PackageReference (I named it dependencies.config):

<?xml version="1.0"?>
<Project Sdk="Microsoft.NET.Sdk">

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

    <ItemGroup>
        <PackageReference Include="package.a" Version="x.y.z" />
    </ItemGroup>

</Project>`

And running the following command:

dotnet restore .\dependencies.config --packages .\dependencies\ 

But doing so, I have to specify a TargetFramework property and the command automatically download some other packages (microsoft.netcore.platforms and netstandard.library). I can live with that but I would like to know if I was missing something.

Upvotes: 4

Views: 1136

Answers (1)

Arpit Dhiman
Arpit Dhiman

Reputation: 1

The TargetFramework property in the project file causes some additional packages to be downloaded because it includes dependencies necessary for that framework. While these extra packages are typically not avoidable due to the nature of how .NET handles dependencies, using a simple target framework like netcoreapp3.1 or netstandard2.0 helps minimize these additional downloads.

Upvotes: 0

Related Questions