首页 文章

Nuget自动包恢复和自定义源

提问于
浏览
2

看起来MSBuild nuget还原方法是no longer recommended

另见:How do I Enable NuGet Package Restore in Visual Studio 2015

在过去,我们已将自定义包源放在nuget包的.targets或.config文件中,以确保每个人都使用自定义源,即使使用vanilla VS安装也是如此 . 这意味着构建服务器上的配置更少 .

在nuget.config中的EG:

<packageSources>
    <add key="NuGet official package source" value="https://nuget.org/api/v2/" />
    <add key="Another Package source" value="https://another.package.source/nuget/" />
  </packageSources>

但是,使用自动包恢复时,不再有.targets或.config文件来放入额外的源 . 还有另一个地方我们可以放置其他源,以便nuget将使用它们,而无需在构建项目的每台机器上手动设置源?

2 回答

  • 0

    一段时间后,David Ebo发表了一篇文章,描述了如何使其发挥作用:

    http://blog.davidebbo.com/2014/01/the-right-way-to-restore-nuget-packages.html

    您需要在与解决方案.sln文件相同的级别创建NuGet.Config文件 . NuGet.Config文件的内容包含自定义源 .

    我试过这个,确实有效;但是,我注意到我必须关闭并重新打开解决方案才能使配置更改生效 .

    此外,我将其作为解决方案项目包含在解决方案中,尽管我怀疑这是必要的 .

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageSources>
        <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
        <add key="MyCustomSource" value="https://myServer.com/CustomSource/nuget" />
      </packageSources>
    </configuration>
    
  • 4

    我找了类似的东西而没找到它 . 虽然新的自动包恢复方法比以前简单得多,但它失去了使用该解决方案存储自定义nuget包源的能力 . 相反,它依赖于配置文件 %AppData%\nuget\nuget.config .

    对于开发工作站来说,这不是什么大问题:如果有自定义nuget repostories,每个开发人员都需要知道更新他们的包源 . 但根据我的经验,构建服务器更麻烦 . 这是构建服务器上构建脚本的一些不完美的解决方案 .

    对于正式版本,我使用一个小的MSBuild脚本来恢复包,然后使用Visual Studio生成的MSBuild文件进行构建 .

    这是一个msbuild文件的例子,它就像它获得的一样简单 . 本讨论最重要的部分是运行nuget restore的 <Exec> 元素 .

    <Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003" DefaultTargets="Build">
      <PropertyGroup>
        <SolutionDir>$([System.IO.Path]::GetDirectoryName('$(MSBuildProjectDirectory)'))\</SolutionDir>
        <Configuration>Release</Configuration>
      </PropertyGroup>
      <PropertyGroup>
        <ToolsDir>$(SolutionDir)tools\</ToolsDir>
        <NuGetDir>$(ToolsDir)nuget\</NuGetDir>
      </PropertyGroup>
      <PropertyGroup>
        <SolutionProperties>
          Configuration=$(Configuration)
        </SolutionProperties>
      </PropertyGroup>
      <Target Name="Build">
        <Exec Command="$(NuGetDir)\nuget.exe restore -ConfigFile $(NuGetDir)nuget.config" WorkingDirectory="$(SolutionDir)"/>
        <MSBuild Projects="MyProject.sln"
                 Properties="$(SolutionProperties)"
                 Targets="Clean;Build">
        </MSBuild>
      </Target>
    </Project>
    

    nuget restore命令引用指定包源的配置文件 . 这是一个示例 nuget.config

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <packageRestore>
        <add key="enabled" value="True" />
        <add key="automatic" value="True" />
      </packageRestore>
      <packageSources>
        <add key="Custom MyGet" value="https://www.myget.org/F/myrepo/api/v2" />
        <add key="nuget.org" value="https://www.nuget.org/api/v2/" />
      </packageSources>
    </configuration>
    

    请注意,我还将 nuget.exe 的副本保存在与配置文件相同的目录中 . 我觉得在VCS中使用它更容易,而不是依赖于在特定位置具有 nuget.exe 的构建机器 . ( nuget.exe 似乎没有附带VS2015,除非我弄错了) .

    我知道这可能不是你希望的好的,简单的解决方案 . 如果有其他人想出更好的解决方案,我会感兴趣 .

相关问题