我可以制作一个Visual Studio解决方案文件以自动下载Nuget软件包吗?

发布于 2025-01-26 03:26:11 字数 522 浏览 2 评论 0原文

我正在学习如何使用Visual Studio并发布/消费Nuget软件包。遵循以下教程之后:

https:> https:// .com/en-us/nuget/overume-packages/概述和工作流

我可以创建一个简单的nuget软件包并发布/构建良好,并找到所得的.csproj项目文件。

我被分配的一项任务是创建一个将自动下载Nuget软件包的Visual Studios解决方案文件,但我还没有在Nuget文档中看到此内容。

如何为Visual Studio 2019创建一个解决方案文件(或项目文件?),以使任何用户可以使用(运行?)与他们的项目一起使用此文件,以使Nuget Project自动下载到其Visual Studio Project中?

I am learning how to use Visual Studio and publish/consume NuGet packages. After following these tutorials:

https://learn.microsoft.com/en-us/nuget/consume-packages/overview-and-workflow

I can create a simple NuGet package and publish/build it fine, as well as find the resulting .csproj project file.

One task I've been assigned is creating a Visual Studios Solution File that will automatically download a NuGet package, but I haven't seen this talked about in the NuGet documentation yet.

How can I to create a Solution File ( or maybe a project file?) for Visual Studio 2019, such that any user can consume (run?) this file with their project, to have NuGet project automatically downloaded to their Visual Studio project?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风月客 2025-02-02 03:26:11

OP:

我可以创建一个简单的nuget软件包并发布/构建正常,并找到结果.csproj项目文件。

...和OP:

我知道您可以通过Visual Studio接口搜索Nuget软件包以下载。但是,如果我有一个私有式文物存储库的软件包,想知道Visual Studio中是否还有其他方法可以下载它

所以我假设您已经发布了Nuget软件包,并通过 Manage Manage Nuget Packages在项目中引用了它对于解决方案... 至少一次,并提交结果来为您的团队中的其他人提供控制。但是问题在于,您将某种形式的自动工作流程 ,因此,当您的团队成员打开解决方案时,即使包裹在您的上,这些软件包也会自动恢复私人Nuget Server 无论您身在何处,您的团队成员环境当前未配置。这不是问题。

因此,可以做到两种方法:

  1. 在Visual Studio中指定其他Nuget软件包在Visual Studio中
  2. 修改Nuget actry at 解决方案,用户机器范围

1.0选择

1.1通过Visual Studio#1中的软件包管理器设置手动配置Nuget

,可以说是通过VS的用户界面完成的最简单的。不幸的是,这确实要求团队成员手动应用这些设置,这首先是您想要避免的。此外,本质上是用户cope so 将影响您在Visual Studio中打开的任何解决方案,如果多个解决方案正在使用自定义配置进行战斗,可能会导致可能的问题:

也要警告,如果像我一样,您正在工作,并将您的雇主的Nuget服务器添加到了上面,请不要惊讶构建突然停止工作,特别是Nuget Restore。我经常发现自己必须与雇主帐户进行重新认证或暂时禁用自定义Nuget源。

1.2通过nuget.config文件配置nuget。在此选项中,源控件中存在的文件

您可以通过 nuget.config 文件配置Nuget,可以将其添加到 source Control 。为什么这很重要?好吧,当您的团队通过git抓住最新的代码说话时,他们还会在回购的root文件夹中找到一个新品牌的new nuget.config 为此,将检测到,导致Nuget 的自动解决方案特定配置,该配置将 将您的自定义Nuget软件包添加到RESTORE操作中时,您可以构建解决方案。耶!。

您可以完全自由地根据范围进行自定义应用:

  • 解决方案
  • 用户
  • 机器

MSDN可以在 nuget.config 文件上说:

nuget.config是一个包含顶级节点的XML文件,然后包含本主题中描述的部分元素。每个部分包含零或更多项目。请参阅示例配置文件。设置名称是不敏感的,值可以使用环境变量。 1

...和:

nuget的行为是由一个或多个nuget.config(xml)文件中的累积设置驱动的,该文件可能存在于项目,用户和计算机范围内。全局nugetdefaults.config文件还专门配置了软件包源。设置适用于CLI,包装管理器控制台和软件包管理器UI的所有命令。 2

这是一个示例 nuget.config 。请注意,在列出标准Nuget源之前,我首先列出了我的自定义Nuget软件包源(尽管不是完全必要)。配置文件不必与Visual Studio解决方案( .sln )文件中的同一文件夹中存在,但也许是在repo root(请参阅下文),因此,当构建解决方案时,优先考虑偏好它可以在任何其他设置(包括VS的用户界面中的内容)上:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <clear />
        <add key="repositoryPath" value="packages" />
    </config>
    <packageSources>
        <!-- When <clear /> is present, previously defined sources are ignored -->
        <!-- Remove this tag or un-comment the nuget.org source below to restore packages from nuget.org -->
        <!-- For more info, see https://docs.nuget.org/consume/nuget-config-file -->
        <clear />       
        <add key="Contoso" value="https://mickyd.com/packages/" />
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    </packageSources>

    <packageRestore>
        <add key="enabled" value="True" />
        <add key="automatic" value="True" />
    </packageRestore>

    <activePackageSource>
        <add key="All" value="(Aggregate source)" />
    </activePackageSource>
</configuration>

Microsoft建议您放置 nuget.config 文件(我的强调):

... 在您的项目存储库的根部。这被认为是最佳实践,因为它可以提高可重复性并确保不同的用户具有相同的nuget配置。 1

请务必查看MSDN上相当详细的示例配置文件。 3


1 “ nuget.config参考” ,msdn

2 “ common nuget配置” ,msdn

3 “示例配置文件”

OP:

I can create a simple NuGet package and publish/build it fine, as well as find the resulting .csproj project file.

...and OP:

I know you can search for a NuGet package through the visual studio interface to download. But If I have a package hosted in a private artifactory repo, wondering if there are any other ways in visual studio to download it

So I am assuming you have already published the NuGet package, referenced it in your projects via Manage NuGet Packages for solution... at least once and committed the results to source control for others in your team to use. But the problem is that you would some form of automatic workflow so that when your team members open the solution, the packages are automatically restored even though they are on your private NuGet server wherever that may be, something your team members environment is not currently configured for. This is not a problem.

So there's a two ways this can be done:

  1. Specifying additional NuGet package sources in Package Manager Settings in Visual Studio
  2. Modifying NuGet behaviour at solution, user or machine scope

1.0 Choices

1.1 Manually configure NuGet via Package Manager settings in Visual Studio

#1, is arguably the easiest as is done via VS's user interface. Unfortunately it does require team members to manually apply these settings which is something you wanted to avoid in the first place. Additionally is is essentially user-scope so will affect any solution you open in Visual Studio leading to possible problems if multiple solutions are fighting over custom configuration:

enter image description here

Also be warned, if like me you are working-from-home and have added your employer's NuGet server to the above, don't be surprised that the build suddenly stops working, specifically NuGet restore. I often find myself having to re-authenticate with my employer account or temporally disabling the custom NuGet source.

enter image description here

1.2 Configure NuGet via NuGet.config files present in source control

In this option you configure NuGet via nuget.config files, something that can be added to source control. Why is that important? Well when your team grabs the latest code say via Git, they'll also find a brand spanking new nuget.config in the repo's root folder that Visual Studio (and CI servers for that matter) will detect, leading to an automatic solution-specific configuration of NuGet that will add your custom NuGet packages to the restore operation when you go to build your solution. Yay!.

enter image description here

You have complete freedom over where such customisation are applied depending upon scope:

  • solution
  • user
  • machine

MSDN has this to say on nuget.config files:

nuget.config is an XML file containing a top-level node, which then contains the section elements described in this topic. Each section contains zero or more items. See the examples config file. Setting names are case-insensitive, and values can use environment variables.1

...and:

NuGet's behavior is driven by the accumulated settings in one or more NuGet.Config (XML) files that can exist at project-, user-, and computer-wide levels. A global NuGetDefaults.Config file also specifically configures package sources. Settings apply to all commands issued in the CLI, the Package Manager Console, and the Package Manager UI.2

Here's an example nuget.config. Note how I list my custom NuGet package source first (though not entirely necessary) before listing the standard NuGet source. The config file does not have to exist in the same folder as the Visual Studio solution (.sln) file but perhaps in the repo root (see below) so when the solution is built, preference is given to it over any other settings (including what is in VS's user interface):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <config>
        <clear />
        <add key="repositoryPath" value="packages" />
    </config>
    <packageSources>
        <!-- When <clear /> is present, previously defined sources are ignored -->
        <!-- Remove this tag or un-comment the nuget.org source below to restore packages from nuget.org -->
        <!-- For more info, see https://docs.nuget.org/consume/nuget-config-file -->
        <clear />       
        <add key="Contoso" value="https://mickyd.com/packages/" />
        <add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
    </packageSources>

    <packageRestore>
        <add key="enabled" value="True" />
        <add key="automatic" value="True" />
    </packageRestore>

    <activePackageSource>
        <add key="All" value="(Aggregate source)" />
    </activePackageSource>
</configuration>

Microsoft recommends that you place the nuget.config file (my emphasis):

...in the root of your project repository. This is considered a best practice as it promotes repeatability and ensures that different users have the same NuGet configuration.1

Be sure to check out the rather detailed Example config file on MSDN.3


1 "nuget.config reference", MSDN

2 "Common NuGet configurations", MSDN

3 "Example config file"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文