Add a package with a local package file in 'dotnet'

前端 未结 4 1452
小蘑菇
小蘑菇 2020-11-29 20:24

Using the dotnet command line tool, how can I add a reference to an existing local package that is not downloaded with NuGet?

I have tried addin

4条回答
  •  盖世英雄少女心
    2020-11-29 21:07

    I've struggled with this a lot, and this the only way that I can make it work:

    • Create a new 'package source' to use

    • Install the .nupkg file into the package source, using nuget add ...

    • Use dotnet add package Foo -s ... to install the package.

    Specifically, the commands you need to use are:

    nuget add ../whatever/lib/MyPackage.1.0.0.nupkg -Source ./packages

    And:

    dotnet add package MyPackage -s ./packages

    Notice specifically, a couple of points here:

    • First, you cannot simply copy the .nupkg file into the packages folder. I've tried lots of variations of this, and all I can say is that it does not work for me, on Windows, Mac, or Linux.

    • You must use a version of NuGet which is at least 3, or this doesn't work. The default version for .NET Core is 2.xx at the time of writing. You will need to manually upgrade NuGet.

    • Your reference in the .csproj file will look like this:

    ... PackageReference Include="SolidMud" Version="1.0.0" ...

    I.e., specifically note that it does not reference your package source in the dependency information; just the package name and version.

    Basically, this means that if you run dotnet restore, it won't work unless the package is cached in your global NuGet cache on that machine; you need to run dotnet restore -s ./packages the first time you do a restore.

    As mentioned in another answer, packages are globally cached; if you want to roll to a new version with the same version id, you need to use dotnet nuget locals all --clear or manually delete the cached package version.

    Here is a specific, complete example of restoring a .nupkg called 'SolidMud' into a brand new .NET Core console application:

    $ dotnet new console -n TestPkgs
    The template "Console Application" was created successfully.
    
    Processing post-creation actions...
    Running 'dotnet restore' on TestPkgs/TestPkgs.csproj...
    Restore succeeded.
    
    $ cd TestPkgs/
    $ mkdir packages
    $ nuget add ../core-solidmud/lib/SolidMud.1.0.0.nupkg -Source ./packages
    Installing SolidMud 1.0.0.
    Successfully added package '../core-solidmud/lib/SolidMud.1.0.0.nupkg' to feed './packages'.
    
    $ dotnet add package SolidMud -s ./packages
    Microsoft (R) Build Engine version 15.3.117.23532
    Copyright (C) Microsoft Corporation. All rights reserved.
    
    Writing /var/folders/29/0695l4fj26j64kp4p8vwqq5h0000gn/T/tmpkRBaST.tmp
    info : Adding PackageReference for package 'SolidMud' into project '/Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj'.
    log  : Restoring packages for /Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj...
    info : Package 'SolidMud' is compatible with all the specified frameworks in project '/Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj'.
    info : PackageReference for package 'SolidMud' version '1.0.0' added to file '/Users/doug/dev/dotnet-packages/TestPkgs/TestPkgs.csproj'.
    

提交回复
热议问题