配置 Nuget.Config 以在 VS2013 和 VS2017 中工作 [英] Configuring Nuget.Config to work in both VS2013 and VS2017

查看:20
本文介绍了配置 Nuget.Config 以在 VS2013 和 VS2017 中工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我当前在 VS2013 中运行的 nuget.config.

Below is my current nuget.config that is working in VS2013.

我想转移到 VS2017,但我需要对 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.

无论如何我可以编写我的 nuget.config 以便它可以在 VS2013 或 VS2017 中工作吗?

Is there anyway i can write my nuget.config so that it can work in either VS2013 or VS2017?

我问的原因是因为并非所有开发人员都在使用相同版本的 Visual Studio,并且当我签入使用 nuget 所需的更改时,对于那些在 VS2013 中加载解决方案的人来说,它会中断.

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.

<!-- 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>

推荐答案

我的建议是将您拥有的 nuget 版本更新到最新版本,然后在您团队中的所有 Visual Studio 中添加一个 nuget 存储库以指向同一文件夹位置.

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.

您至少需要了解的唯一一件事是,如果您使用 2013 版本的开发人员(团队 A)删除了一个软件包,那么使用 2017 版本的开发人员(团队 B)应该没有任何问题,因为当 Nuget 在您之后识别时获取存储库文件夹的最新版本,该文件夹不存在于包文件夹中,因为它根据 package.config 识别哪些已安装,哪些未安装,那么您应该再次获取依赖项并将其作为新更改提交到您的源代码控制工具,之后第二个项目将指向正确的引用,您的团队 A 将不会将其作为其依赖项的一部分.

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.

第二种方法:您可以转到您的 vs2013 项目并使用最新配置(NuGet.Config 文件)更新对新存储库的所有引用,这应该可行

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

以下是我需要从头开始配置 NuGet 时遵循的步骤

Here are the steps I follow when I need to configure NuGet from scratch

NUGET 配置:方法是统一和减少所有项目的重复依赖,并将它们放在一个公共的地方,每个项目都可以使用它.优点不仅仅是统一了依赖关系;它还减少了 TFS 上的空间,并为自动构建保留了层次结构.

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. 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.

第 1 步:Nuget 包的通用文件夹

首先,您需要在所有项目的根级别创建 Common 文件夹,其中包含一个 Packages 文件夹.如果您希望将其他项目中的所有 .NuGet 包复制到后一个文件夹,或者在包含新依赖项时将其留给 Visual Studio,请随意手动复制.Common 将作为 Nuget 存储库工作,但在这种情况下作为本地 Nuget.存储库.您的 Visual Studio 配置应该知道这一点.按照下一步

First you need to create the Common folder at the root level of all your projects with a Packages folder inside. 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. Common will work as a Nuget repository, but on this case as a local Nuget. repository. Your Visual Studio configuration should know that. Follow the next step

然后您需要使用加号按钮创建一个新的种子存储库(您的本地).下面我命名为:Packages.之后,浏览新包的文件夹.然后点击确定

Then you need to create with the plus button a new Seed Repository (your local). Below I named: 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

这里的想法是从 Packages 文件夹而不是从 Nuget 存储库下载我们需要的包,如果包不存在,则更改为搜索 nuget.org 选项并从那里下载包.效果应该是一样的

第 2 步:准备配置文件

这里我们希望避免 Nuget 将包保存在我们项目根目录的packages"文件夹中,这是 nuget 在下载包时的默认行为.

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.

相反,我们希望将包下载到 Common/Package 文件夹.为此,我们需要在我们的结构中添加一个 Nuget.config 文件.在项目的根目录中创建一个 NuGet.Config 文件.TFS 视图

Rather we want to download the package to Common/Package folder. In order to do that, we need to add a Nuget.config file in our structure. Create a NuGet.Config file inside the root of your project. TFS View

NuGet.Config 类型内部

Inside NuGet.Config type

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

对于repositoryPath 设置,您可以使用$ 标记指定绝对路径或相对路径(推荐).$ 令牌基于 NuGet.Config 所在的位置($ 令牌实际上是相对于 NuGet.Config 所在位置的下一级).所以,如果我有 \ Team Project\NuGet.Config 并且我想要 \Team Project\Common\Packages 我需要指定 $..\Common\Packages 作为值.

For the repositoryPath setting, you can specify an absolute path or relative path (recommended) using the $ token. 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). 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.

第 3 步:最后一步.

开始下载软件包并确保签入所有更改.如果您的项目根目录下有一个包文件夹,您可以删除它,以防您想设置对新文件夹的正确引用.如果这是您想要的,请随意删除它.但是,如果您希望保留指向包的遗留代码并开始将新的 Common 文件夹用于未来的包,那么您需要同时保留两者.下面我修复了所有引用,没有包"文件夹,仍然可以包含新包.

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. 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. Below I fixed all references, there is no "package" folder and still can include new packages.

享受!!!

这篇关于配置 Nuget.Config 以在 VS2013 和 VS2017 中工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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