在"dotnet"中添加带有本地软件包文件的软件包 [英] Add a package with a local package file in 'dotnet'
问题描述
使用dotnet
命令行工具,如何添加对使用NuGet下载的 not 现有的本地软件包的引用?
Using the dotnet
command line tool, how can I add a reference to an existing local package that is not downloaded with NuGet?
我尝试使用dotnet
将本地软件包添加到项目bar
:
I have tried adding a local package to a project bar
with dotnet
:
dotnet add package /Users/sakra/foo/bin/Debug/foo.1.0.0.nupkg
已在另一个项目中使用dotnet pack
创建了软件包foo.1.0.0.nupkg
.但是,命令dotnet add package
尝试从 https://api.nuget.org/下载文件foo.1.0.0.nupkg
. a>当然会失败.
The package foo.1.0.0.nupkg
has been created with dotnet pack
in a different project. The command dotnet add package
however tries to download the file foo.1.0.0.nupkg
from https://api.nuget.org/ which of course fails.
推荐答案
无法直接安装单个.nupkg
软件包. NuGet只能从提要中安装和还原,因此您需要将包所在的目录添加为提要.
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.
要执行此操作,请添加一个NuGet.Config
文件,该文件将目录的位置添加为提要,因此您不必将源参数添加到每个与NuGet相关的命令(尤其是dotnet restore
):>
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>
或者,在.NET Core 2.0工具/NuGet 4.3.0中,您也可以将源直接添加到应该消耗NuGet提要的.csproj文件中:
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
(可选添加-v 1.0.0
) -
dotnet restore
-
dotnet run
dotnet add package foo
(optionally add-v 1.0.0
)dotnet restore
dotnet run
请注意,在开发过程中,如果更改了NuGet软件包,但在生成.nupkg文件的项目和使用该文件的项目中都没有增加其版本,则需要清除本地软件包缓存再次恢复之前:
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
我在 https://github.com/dasMulli/LocalNupkgExample 中创建了一个小示例项目
I have created a small example project at https://github.com/dasMulli/LocalNupkgExample
这篇关于在"dotnet"中添加带有本地软件包文件的软件包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!