Nuget 自动包还原和自定义源
Nuget Automatic Package Restore and custom sources
看起来 MSBuild nuget 恢复方法是 no longer recommended
另见:
过去,我们将自定义包源放在 nuget 包的 .targets 或 .config 文件中,以确保每个人都使用自定义源,即使是普通 VS 安装。这意味着构建服务器等的配置更少。
EG 在 nuget.config:
<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 将使用它们而无需手动设置源每台机器构建项目?
我找过类似的东西,但没找到。虽然新的自动包恢复方法比以前简单得多,但它失去了使用解决方案存储自定义 nuget 包源的能力。相反,它依赖于配置文件 %AppData%\nuget\nuget.config
.
这对于开发工作站来说不是什么大问题:如果有自定义的 nuget 存储库,每个开发人员都需要知道更新他们的包源。不过,根据我的经验,构建服务器更麻烦。这是我在构建服务器上构建脚本的有点不完美的解决方案。
对于正式构建,我使用一个小的 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 一起提供,除非我弄错了)。
我知道这可能不是您所希望的简单好用的解决方案。如果其他人提出更好的解决方案,我会很感兴趣。
David Ebo 不久前 post 描述了如何让它工作:
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>
看起来 MSBuild nuget 恢复方法是 no longer recommended
另见:
过去,我们将自定义包源放在 nuget 包的 .targets 或 .config 文件中,以确保每个人都使用自定义源,即使是普通 VS 安装。这意味着构建服务器等的配置更少。
EG 在 nuget.config:
<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 将使用它们而无需手动设置源每台机器构建项目?
我找过类似的东西,但没找到。虽然新的自动包恢复方法比以前简单得多,但它失去了使用解决方案存储自定义 nuget 包源的能力。相反,它依赖于配置文件 %AppData%\nuget\nuget.config
.
这对于开发工作站来说不是什么大问题:如果有自定义的 nuget 存储库,每个开发人员都需要知道更新他们的包源。不过,根据我的经验,构建服务器更麻烦。这是我在构建服务器上构建脚本的有点不完美的解决方案。
对于正式构建,我使用一个小的 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 一起提供,除非我弄错了)。
我知道这可能不是您所希望的简单好用的解决方案。如果其他人提出更好的解决方案,我会很感兴趣。
David Ebo 不久前 post 描述了如何让它工作:
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>