简体   繁体   English

配置Nuget.Config在VS2013和VS2017中都可以工作

[英]Configuring Nuget.Config to work in both VS2013 and VS2017

Below is my current nuget.config that is working in VS2013. 下面是我当前在VS2013中运行的nuget.config。

Im looking to move over to VS2017 but i need to make changes to the nuget.config file in order to do so. 我正在寻找移至VS2017,但我需要对nuget.config文件进行更改。

Is there anyway i can write my nuget.config so that it can work in either VS2013 or VS2017? 无论如何,我可以编写我的nuget.config以便它可以在VS2013或VS2017中工作吗?

The reason i ask is because not all developers are working on the same version of visual studio, and when i checkin the changes required to work with nuget it will break for those loading the solution in VS2013. 我问的原因是因为并非所有开发人员都在使用同一版本的Visual Studio,并且当我签入使用nuget所需的更改时,对于那些在VS2013中加载解决方案的人来说,它会中断。

<!-- VS2013 -->

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <repositoryPath>..\lib</repositoryPath>
</settings>


<!-- VS2017 -->

<configuration>
 <config>
     <add key="repositoryPath" value="..\lib" />
     </config>
     <packageRestore>
     <add key="enabled" value="True" />
     </packageRestore>
 </configuration>

My suggestion is update the nuget version that you have to the latest one, then add a nuget repository in all visual studios in your teams to point to the same folder location. 我的建议是将您拥有的nuget版本更新到最新版本,然后在团队中所有视觉工作室中添加一个nuget存储库,以指向同一文件夹位置。

在此处输入图片说明

The only thing that you need to understand at least is if one of your developers(Team A) using 2013 version removes a package there should not be any problem for the developer(Team B) using 2017 because when Nuget recognizes after you get the latest version of the repository folder, that a folder is not present on the package folder because it recognizes based on the package.config which are installed and which not, then you should get the dependency again an submit it as new change to your source control tool, after that the second project will point to the correct references, and your Team A will not have it included as part of their dependencies. 至少需要了解的唯一一件事是,如果使用2013版本的开发人员(团队A)删除了一个程序包,那么使用2017的开发人员(团队B)应该不会有任何问题,因为当Nuget识别出最新版本后,版本号的存储库文件夹,则该文件夹不存在于package文件夹中,因为它会根据package.config识别已安装和未安装的package.config,那么您应该再次获取依赖项,并将其作为对源代码管理工具的新更改提交,此后,第二个项目将指向正确的引用,并且您的团队A不会将其包含在其依赖项中。

Second approach: you can go to your vs2013 projects and update all references to the new repository using the latest configuration(NuGet.Config file) , that should work 第二种方法:您可以转到vs2013项目,并使用最新的配置(NuGet.Config文件)更新对新存储库的所有引用,该配置应该可以正常工作

Here are the steps I follow when I need to configure NuGet from scratch 当我需要从头开始配置NuGet时,请按照以下步骤操作

NUGET CONFIGURATION: The approach is to unify and reduce duplicated dependencies of all projects and place them in a common place where every project can make use of it. NUGET CONFIGURATION:方法是统一和减少所有项目的重复依赖项,并将它们放置在每个项目都可以使用的公共位置。 The advantage is not just unifying the dependencies; 好处不仅在于统一依赖关系;还在于 it also brings a reduction of space on the TFS and preserves a hierarchical structure for automatic builds as well. 它还减少了TFS上的空间,并保留了用于自动构建的分层结构。

STEP 1: Common folder for Nuget Packages 步骤1:Nuget包的通用文件夹

在此处输入图片说明

First you need to create the Common folder at the root level of all your projects with a Packages folder inside. 首先,您需要在所有项目的根目录级别创建Common文件夹,并在其中包含Packages文件夹。 Feel free to copy manually if you want all .NuGet packages that you have in other projects to the latter folder or leave it to the Visual Studio when you include a new dependency. 如果要将其他项目中拥有的所有.NuGet包都希望复制到后者文件夹中,或者在包含新依赖项时将其保留在Visual Studio中,则可以手动进行手动复制。 Common will work as a Nuget repository, but on this case as a local Nuget. Common将用作Nuget存储库,但在这种情况下,将用作本地Nuget。 repository. 资料库。 Your Visual Studio configuration should know that. 您的Visual Studio配置应该知道这一点。 Follow the next step 继续下一步

在此处输入图片说明

Then you need to create with the plus button a new Seed Repository (your local). 然后,您需要使用加号按钮创建一个新的种子存储库(您的本地存储库)。 Below I named: Packages. 下面我命名为:Packages。 After that browse the folder for your new packages. 之后,浏览文件夹中的新软件包。 Then click ok 然后点击确定

在此处输入图片说明

Now you can go to your Project and make a right click to include a new dependency 现在您可以转到项目并右键单击以包含新的依赖项 在此处输入图片说明

Then go to package source and select Packages and you will see all the packages available on the Packages folder that should be empty the first time 然后转到程序包源并选择“程序包”,您将在“程序包”文件夹中看到所有可用的程序包,这些程序在第一次应为空

在此处输入图片说明

The idea here is to download the packages we need from the Packages folder instead from the Nuget repository, if the package don't exist then change to the search to nuget.org option and download the package from there. 这里的想法是从Packages文件夹而不是从Nuget存储库中下载我们需要的软件包,如果该软件包不存在,请转到search to nuget.org选项并从那里下载该软件包。 The effect should be the same 效果应该一样

STEP 2: Prepare the configuration files 步骤2:准备配置文件

Here we want to avoid that Nuget to save packages inside “packages” folder at the root of our project, which is the default behavior of nuget when you download a package. 在这里,我们要避免Nuget将软件包保存在项目根目录下的“ packages”文件夹中,这是下载软件包时nuget的默认行为。

在此处输入图片说明

在此处输入图片说明

Rather we want to download the package to Common/Package folder. 而是我们要将包下载到Common / Package文件夹。 In order to do that, we need to add a Nuget.config file in our structure. 为此,我们需要在结构中添加一个Nuget.config文件。 Create a NuGet.Config file inside the root of your project. 在项目的根目录下创建一个NuGet.Config文件。 TFS View TFS视图

在此处输入图片说明

在此处输入图片说明

Inside NuGet.Config type 里面的NuGet.Config类型

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <config>
    <add key="repositoryPath" value="$\..\Common\Packages" />
  </config>
</configuration>

For the repositoryPath setting, you can specify an absolute path or relative path (recommended) using the $ token. 对于repositoryPath设置,您可以使用$令牌指定绝对路径或相对路径(推荐)。 The $ token based on where the NuGet.Config is located (The $ token is actually relative to one level below the location of the NuGet.Config). $令牌基于NuGet.Config的位置($令牌实际上是相对于NuGet.Config位置下方的一级)。 So, if I have \\ Team Project\\NuGet.Config and I want \\Team Project\\Common\\Packages I would need to specify $..\\Common\\Packages as the value. 因此,如果我有\\ Team Project \\ NuGet.Config并想要\\ Team Project \\ Common \\ Packages,则需要指定$ .. \\ Common \\ Packages作为值。

STEP 3: Final step. 步骤3:最后一步。

Start downloading the packages and make sure you check in all changes. 开始下载软件包,并确保签入所有更改。 If you have a package folder at the root of your project you can get rid of it in case you want to set the correct references to the new folder. 如果您在项目的根目录下有一个package文件夹,那么您可以删除它,以防您想对新文件夹设置正确的引用。 Feel free to delete it if that is what you want. 如果您要删除它,请随时将其删除。 But if you want to keep the legacy code pointing to the package and start using the new Common folder for future packages, then you need to keep both. 但是,如果要保留指向程序包的旧代码并开始为以后的程序包使用新的Common文件夹,则需要同时保留两者。 Below I fixed all references, there is no “package” folder and still can include new packages. 在我固定所有引用的下面,没有“ package”文件夹,并且仍然可以包括新的软件包。

在此处输入图片说明

Enjoy!!! 请享用!!!

I'm sorry to tell you that this is not possible. 很抱歉告诉您这不可能。 This was a breaking change to accommodate .Net Core and other versions of VS (ie, VS for Mac). 为适应.Net Core和其他版本的VS(即Mac的VS),这是一项重大更改。 I stumbled upon this info when looking into vs 2017 RC and .net core tools preview. 在查看vs 2017 RC和.net核心工具预览时,我偶然发现了此信息。

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

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