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

前端 未结 4 1432
小蘑菇
小蘑菇 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 20:47

    There isn't a way to directly install a single .nupkg package. NuGet can only install and restore from feeds, so you'll need to add the directory where the package is in as a feed.

    To do this, add a NuGet.Config file that adds the location of the directory as a feed, so you don't have to add the source parameter to each NuGet-related command (especially dotnet restore):

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="local-packages" value="../foo/bin/Debug" />
      </packageSources>
    </configuration>
    

    Alternatively in .NET Core 2.0 tools / NuGet 4.3.0, you could also add the source directly to the .csproj file that is supposed to consume the NuGet feed:

    <PropertyGroup>
      <RestoreSources>$(RestoreSources);../foo/bin/Debug;https://api.nuget.org/v3/index.json</RestoreSources>
    </PropertyGroup>
    

    This will make all commands be able to use the package:

    • dotnet add package foo (optionally add -v 1.0.0)
    • dotnet restore
    • dotnet run

    Note that during development, if you change the NuGet package, but don't increment its version in both the project that produces the .nupkg file and in the project that consumes it, you'll need to clear your local packages cache before restoring again:

    dotnet nuget locals all --clear
    dotnet restore
    

    I have created a small example project at https://github.com/dasMulli/LocalNupkgExample

    0 讨论(0)
  • 2020-11-29 20:53

    In .NET Core 3.1 (Arm64), I can add the local source and package by

    dotnet nuget add source ~/my/nuget-packages/
    mv mypackage.1.1.1.nuget ~/my/nuget-packages/
    dotnet add package mypackage
    

    and ~/.nuget/NuGet/NuGet.Config was changed.

    0 讨论(0)
  • 2020-11-29 20:59

    Specify the package's location folder using -s|--source option. For example:

    dotnet add package Microsoft.AspNetCore.Cors -s "d:\Cache\localfeed" -f netcoreapp1.1
    

    UPDATE: Thanks to MartinUllrich's answer: you can't simply install a .nupkg file. It's necessary to specify a local feed and add a local .nupkg file to the feed before you could install the package. Check MartinUllrich's answer for details.

    Unfortunately you could face this blocking issue:

    Package 'NameOfPackage' is incompatible with 'all' frameworks in project

    At this moment it is open and I was able to reproduce it on a stable package version.

    0 讨论(0)
  • 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'.
    
    0 讨论(0)
提交回复
热议问题