问题描述
我正在尝试自动化将 nuget 包安装到新的 Visual Studio 项目的过程.我的想法是通过在可以运行以安装这些包的自定义文件中指定包来减少通过 nuget 包管理器获取所有包所需的时间.因此只需要每个新项目都包含这个文件并每次运行它.我对 nuget 很陌生,并且在没有太多先验知识的情况下就被分配了这项任务.我被告知 .nuspec 路由会引导我走向正确的方向,因为它包含有关包的元数据.尽管由于消费者无法直接访问包的 .nuspec 文件,但我无法理解如何将其用作此自动化的一部分.我也听说过自动包恢复,但由于它仅适用于丢失的参考,我看不出它对不一定引用与该项目有关的任何内容的新项目有何帮助.
I am trying to automate the process of installing nuget packages into new visual studio projects. My idea is to reduce the time it takes to source all the packages via the nuget package manager, by specifying the packages in a custom file that can be run to install these packages. Therefore only requiring every new project to include this file and running it each time. I'm very new to nuget and have been assigned this task without much prior knowledge. I was advised that .nuspec route would lead me in the right direction, since it contains the meta data about a package. Although since consumers don't have direct access to the .nuspec file of a package, I am failing to understand how it can be used as part of this automation. I have also heard about automatic package restore, but since that only works for lost reference, I don't see how it will help in new projects that haven't necessarily referenced anything to do with that project.
推荐答案
每次在 Visual Studio 中添加 nuget 包时,它都会添加到名为 packages.config 的文件中.
Everytime you add a nuget package in Visual Studio,it gets add to a file called packages.config file.
例如看起来像这样
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="3.5.2" targetFramework="net451" />
<package id="Microsoft.Owin" version="3.1.0" targetFramework="net452" />
<package id="Microsoft.Owin.Host.HttpListener" version="3.0.1" targetFramework="net451" />
<package id="Microsoft.Owin.Hosting" version="3.1.0" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net451" />
<package id="Owin" version="1.0" targetFramework="net451" />
<package id="Topshelf" version="3.2.0" targetFramework="net451" />
</packages>
您的解决方案中的每个项目都有packages.config 文件.您可以转到所有项目的父文件夹,只需运行命令nuget restore",它就会为您获取所有包.
Every project you have in your solution will have packages.config file. You can go to the parent folder of all the projects and simply run comand 'nuget restore', it will get all the packages for you.
为此,nuget.exe 需要单独下载.更多详细信息nuget 命令行可以在这里找到这是命令行参考
For this to work, nuget.exe needs to be downloaded separately .More details on the nuget command line can be found here and here's the commandline reference
感谢@Martin Ullrich 指出.需要说明的是,上述方法不会自动添加对项目文件的引用,它只会将包获取到包文件夹中.在 VS2017 中,支持@Martin 的回答地址.
Thanks @Martin Ullrich pointing out.Just to be clear, The above method will not add the references to project file automatically,it will only get the packages to the packages folder.In VS2017,the support is there which @Martin's answer addresses.
希望这会有所帮助!
这篇关于使用 .nuspec 文件/脚本将 nuget 包自动安装到新项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!