使用.nuspec文件/脚本自动将nuget软件包安装到新项目中

时间:2021-08-12 10:47:09

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包安装到新的visual studio项目中的过程。我的想法是通过在可以运行以安装这些软件包的自定义文件中指定软件包来减少通过nuget软件包管理器获取所有软件包所需的时间。因此,只需要每个新项目都包含此文件并每次运行它。我对nuget很新,并且在没有太多先验知识的情况下被分配了这项任务。我被告知.nuspec路线会引导我朝着正确的方向前进,因为它包含有关包的元数据。虽然由于消费者无法直接访问包的.nuspec文件,但我无法理解如何将其用作此自动化的一部分。我也听说过自动程序包还原,但由于这只适用于丢失的引用,我不知道它对于那些不一定引用与该项目有关的新项目有什么帮助。

2 个解决方案

#1


0  

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

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

Everytime you add a nuget package in Visual Studio,it gets add to a file called packages.config file.

每次在Visual Studio中添加nuget包时,都会将其添加到名为packages.config文件的文件中。

E.g. will look like this

例如。会是这样的

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

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.

解决方案中的每个项目都有packages.config文件。您可以转到所有项目的父文件夹,只需运行命令'nuget restore',它将为您获取所有包。

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

要使其工作,需要单独下载nuget.exe。有关nuget命令行的更多详细信息,请在此处找到,这里是命令行参考

Edit:

编辑:

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.

谢谢@Martin Ullrich指出。只是要明确,上面的方法不会自动添加对项目文件的引用,它只会将软件包送到packages文件夹。在VS2017中,支持@ Martin的答案地址。

Hope this helps!

希望这可以帮助!

#2


1  

note that you cannot simply drop a pre-built packages.config file into a new project and expect it to work. When installing, NuGet modifies the project file (.csproj) to include references and uses packages.config for downloading missing files (and update/conflict logic).

请注意,您不能简单地将预先构建的packages.config文件放入新项目中并期望它能够正常工作。安装时,NuGet会修改项目文件(.csproj)以包含引用,并使用packages.config来下载丢失的文件(以及更新/冲突逻辑)。

Using VS 2017 (released stable versions 15.2 and higher) and the PackageReference style of referencing projects, you can simply drop a Directory.Build.props file into the root of your solution containing all the projects you need:

使用VS 2017(发布的稳定版本15.2及更高版本)和PackageReference样式的引用项目,您只需将Directory.Build.props文件放入包含所需项目的解决方案的根目录中:

<Project>
  <ItemGroup>
    <PackageReference Include="Autofac" Version="3.5.2 />
    <PackageReference Include="Topshelf" Version="3.2.0 />
  </ItemGroup>
</Project>

This will add these NuGet packages to all new projects in the solution without the need for the .csproj files to be modified. (note that after adding/editing this file, you need to close and re-open the solution. this should be fixed in the upcoming VS 2017 15.3 update for editing the file).

这将把这些NuGet包添加到解决方案中的所有新项目,而无需修改.csproj文件。 (请注意,添加/编辑此文件后,您需要关闭并重新打开解决方案。这应该在即将发布的VS 2017 15.3更新中修复,以便编辑文件)。

#1


0  

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

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

Everytime you add a nuget package in Visual Studio,it gets add to a file called packages.config file.

每次在Visual Studio中添加nuget包时,都会将其添加到名为packages.config文件的文件中。

E.g. will look like this

例如。会是这样的

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

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.

解决方案中的每个项目都有packages.config文件。您可以转到所有项目的父文件夹,只需运行命令'nuget restore',它将为您获取所有包。

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

要使其工作,需要单独下载nuget.exe。有关nuget命令行的更多详细信息,请在此处找到,这里是命令行参考

Edit:

编辑:

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.

谢谢@Martin Ullrich指出。只是要明确,上面的方法不会自动添加对项目文件的引用,它只会将软件包送到packages文件夹。在VS2017中,支持@ Martin的答案地址。

Hope this helps!

希望这可以帮助!

#2


1  

note that you cannot simply drop a pre-built packages.config file into a new project and expect it to work. When installing, NuGet modifies the project file (.csproj) to include references and uses packages.config for downloading missing files (and update/conflict logic).

请注意,您不能简单地将预先构建的packages.config文件放入新项目中并期望它能够正常工作。安装时,NuGet会修改项目文件(.csproj)以包含引用,并使用packages.config来下载丢失的文件(以及更新/冲突逻辑)。

Using VS 2017 (released stable versions 15.2 and higher) and the PackageReference style of referencing projects, you can simply drop a Directory.Build.props file into the root of your solution containing all the projects you need:

使用VS 2017(发布的稳定版本15.2及更高版本)和PackageReference样式的引用项目,您只需将Directory.Build.props文件放入包含所需项目的解决方案的根目录中:

<Project>
  <ItemGroup>
    <PackageReference Include="Autofac" Version="3.5.2 />
    <PackageReference Include="Topshelf" Version="3.2.0 />
  </ItemGroup>
</Project>

This will add these NuGet packages to all new projects in the solution without the need for the .csproj files to be modified. (note that after adding/editing this file, you need to close and re-open the solution. this should be fixed in the upcoming VS 2017 15.3 update for editing the file).

这将把这些NuGet包添加到解决方案中的所有新项目,而无需修改.csproj文件。 (请注意,添加/编辑此文件后,您需要关闭并重新打开解决方案。这应该在即将发布的VS 2017 15.3更新中修复,以便编辑文件)。