简体   繁体   English

恢复nuget包的最佳方法是什么?

[英]What is the best way to restore nuget packages?

I have a solution that uses custom nuget package sources. 我有一个使用自定义nuget包源的解决方案。 For now I specify them in Nuget.config file that is located near my solution file(so that it is checked out from source control): 现在,我在解决方案文件附近的Nuget.config文件中指定它们(以便从源代码管理中检出):

|- MySoulution.sln
|- MyProjFolder
|- .nuget
   |- Nuget.exe
   |- Nuget.config
   |- Nuget.targets

This works well when building solution from VisualStudio. 从VisualStudio构建解决方案时,此方法效果很好。 It manages to read this nuget.config file and successfully restore all packages. 它设法读取此nuget.config文件并成功还原所有软件包。

Now I'm configuring my solution to be built from TeamCity. 现在,我正在配置要从TeamCity构建的解决方案。 So I've added project configuration and a build step to build it. 因此,我添加了项目配置和构建步骤。 But TeamCity doesn't restore nuget packages by default. 但是,TeamCity不会默认还原nuget软件包。 So I've added a separate Nuget installer build step that runs nuget(of specific version) restore for my solution. 因此,我为解决方案添加了一个单独的Nuget installer构建步骤,该步骤运行nuget(特定版本)还原。 But the problem is that it doesn't seem to see my custom nuget package sources from Nuget.config file in .nuget folder next to solution file. 但是问题是,似乎没有从解决方案文件旁边的.nuget文件夹中的Nuget.config文件中看到我的自定义nuget包源。

I see two possible ways to overcome this: 我看到两种可能的方法来克服此问题:

  1. Configure my custom package sources inside Nuget installer build step. Nuget installer构建步骤中配置我的自定义软件包源。
  2. Configure my custom package sources in Nuget.config in AppData folder on build machine. 在构建计算机上AppData文件夹中的Nuget.config中配置我的自定义程序包源。

I don't like neither of this approaches because they don't provide me single poing of configuration for building both from TeamCity and VisualStudio. 我不喜欢这两种方法,因为它们没有为我提供从TeamCity和VisualStudio进行构建的单一配置。

To sum up, the question is: how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places? 总而言之,问题是:如何配置自定义程序包源,以便它们在TeamCity和VisualStudio中都可见,而无需我在不同位置进行多次配置?

how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places? 如何配置我的自定义程序包源,以使它们在TeamCity和VisualStudio中都可见,而无需在不同位置进行多次配置?

As you know, if you do not want to configure custom nuget sources several times in different places, you can set the custom nuget sources in the NuGet.config and add it to source control. 如您所知,如果您不想在不同位置多次配置自定义nuget源,则可以在NuGet.config中设置自定义nuget源,并将其添加到源代码控制中。 So the key to your problem is why NuGet doesn't respect the your custom nuget package sources from Nuget.config file in .nuget folder next to solution file. 因此,解决问题的关键是为什么NuGet不尊重解决方案文件旁边.nuget文件夹中Nuget.config文件中的自定义nuget包源。

Just as my comment, if you're using NuGet 2.7 or later and have a solution that is still configured for MSBuild-integrated restore, you may have an older version of nuget.exe in the solution's .nuget folder. 就像我的评论一样,如果您使用的是NuGet 2.7或更高版本,并且仍为MSBuild集成还原配置了解决方案,则解决方案的.nuget文件夹中可能具有nuget.exe的较旧版本。 This will cause builds to fail with an error stating that you have not given consent to restore packages. 这将导致构建失败,并显示一条错误,指出您未同意还原软件包。 To avoid this issue, it's recommended to migrate any project using MSBuild-integrated restore to use the automatic restore capabilities of NuGet 2.7 and above, you can follow the process as below: 为避免此问题,建议使用MSBuild集成还原迁移任何项目,以使用NuGet 2.7及更高版本的自动还原功能,您可以按照以下过程进行操作:

  1. Close Visual Studio to avoid file potential file locks and conflicts. 关闭Visual Studio,以避免潜在的文件锁定和冲突。

  2. If using TFS: Remove nuget.exe and NuGet.targets from the solution's .nuget folder and remove those files from the solution workspace. 如果使用TFS:从解决方案的.nuget文件夹中删除nuget.exe和NuGet.targets,然后从解决方案工作区中删除这些文件。 Retain Nuget.Config with the disableSourceControlIntegration setting as explained in Omitting packages with Team Foundation Version Control. 如禁用Team Foundation版本控制程序中所述,使用disableSourceControlIntegration设置保留Nuget.Config。

  3. If not using TFS: Remove the .nuget folder from the solution and the solution workspace. 如果不使用TFS:从解决方案和解决方案工作区中删除.nuget文件夹。

  4. Edit each project file in the solution, remove the element, and remove any references to the NuGet.targets file. 编辑解决方案中的每个项目文件,删除元素,并删除对NuGet.targets文件的任何引用。 Those settings generally appear as follows: 这些设置通常如下所示:

After that put NuGet.config next to the solution file with custom NuGet source: 之后,将NuGet.config放在带有自定义NuGet源的解决方案文件旁边:

<?xml version="1.0" encoding="utf-8"?>
   <configuration>
     <solution>
       <add key="disableSourceControlIntegration" value="true" />
     </solution>

    <packageSources>
      <add key="CustomSource" value="http://CustomSource/nuget" />
    </packageSources>

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

  </configuration>

Now, in the NuGet Installer step, there is now a " Package Sources " field that you can fill in to have team city use a custom feed: 现在,在NuGet Installer步骤中,现在有一个“ Package Sources ”字段,您可以填写以使Team City使用自定义提要:

在此处输入图片说明

You can refer to this document NuGet Package Restore with TeamCity for more detail. 您可以参考此文档使用TeamCity还原NuGet软件包以获取更多详细信息。

Besides, we can also specify custom feed in the NuGet.targets file in the .nuget folder(I did not verify it yet): 此外,我们还可以在.nuget文件夹中的NuGet.targets文件中指定自定义供稿 (我尚未验证):

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <PackageSource Include="https://nuget.org/api/v2/" />
    <PackageSource Include="\\MyShare" />
    <PackageSource Include="http://MyServer/" />
</ItemGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM