在 'dotnet' 中添加带有本地包文件的包 [英] Add a package with a local package file in 'dotnet'

查看:36
本文介绍了在 'dotnet' 中添加带有本地包文件的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 dotnet 命令行工具,如何添加对现有本地包的引用,该包没有通过 NuGet 下载?

我尝试使用 dotnet 将本地包添加到项目 bar:

dotnet 添加包/Users/sakra/foo/bin/Debug/foo.1.0.0.nupkg

foo.1.0.0.nupkg 是在另一个项目中使用 dotnet pack 创建的.但是,命令 dotnet add package 会尝试从 foo.1.0.0.nupkg="noreferrer">https://api.nuget.org/ 当然失败了.

解决方案

没有办法直接安装单个 .nupkg 包.NuGet 只能从提要安装和恢复,因此您需要将包所在的目录添加为提要.

为此,添加一个 NuGet.Config 文件,该文件将目录的位置添加为提要,因此您不必将源参数添加到每个 NuGet 相关命令(尤其是dotnet restore):

或者在 .NET Core 2.0 工具/NuGet 4.3.0 中,您还可以将源直接添加到应该使用 NuGet 提要的 .csproj 文件中:

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

这将使所有命令都能够使用该包:

  • dotnet add package foo(可选添加-v 1.0.0)
  • dotnet 恢复
  • dotnet 运行

dotnet add package foo 将向*.csproj添加一个包引用(这里假设,版本1.0.0):

+ <PackageReference 包括=foo";版本=1.0.0";/></项目组>

请注意,在开发过程中,如果您更改了 NuGet 包,但在生成 .nupkg 文件的项目和使用它的项目中都没有增加其版本,则需要清除本地包缓存在再次恢复之前:

dotnet nuget locals all --clear点网还原

我在 https://github.com/dasMulli/LocalNupkgExample 创建了一个小示例项目/p>

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 adding a local package to a project bar with dotnet:

dotnet add package /Users/sakra/foo/bin/Debug/foo.1.0.0.nupkg

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.

解决方案

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

dotnet add package foo will add a package reference (assumption here, version 1.0.0) to *.csproj:

<ItemGroup>
+    <PackageReference Include="foo" Version="1.0.0" />
</ItemGroup>

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

这篇关于在 'dotnet' 中添加带有本地包文件的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆