使用 .nuspec 文件/脚本自动将 nuget 包安装到新项目中 [英] Use a .nuspec file/script to automatically install nuget packages into a new project

查看:15
本文介绍了使用 .nuspec 文件/脚本自动将 nuget 包安装到新项目中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 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.

推荐答案

Nuget 已经支持安装自动化,我们可以使用 nuget 命令行来实现这一点

Nuget already supports automation of installation and we can use nuget commandline to achieve this

每次在 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 包安装到新项目中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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