Storing nuget packages in alternate location on build server
.net, msbuild, nuget
Solution
NuGet has a built-in local cache at `%localappdata%\NuGet\Cache` which you could use as a package source in your nuget.config.
NuGet will restore packages from packagesources in the order defined in your config and stop scanning at its first hit. (activepackagesource should be the aggregate 'All' feed)
Problem
When developing locally I have my Nuget packages installed in the default location (\packages within solution folder). I want have a different folder on my build server that acts as the repository path where I can download packages too, effectively giving me a local cache of packages which will persist across builds, not requiring all packages to be downloaded fresh every time build. On the CI server I drop a nuget.config file into the solution directory which specifies the location of the new packages folder: ``` <?xml version="1.0" encoding="utf-8"?> <configuration> <config> <!-- Specify repository path --> <add key="repositorypath" value="x:\nugetPackages" /> </config> <activePackageSource> <add key="nuget.org" value="https://www.nuget.org/api/v2/" /> </activePackageSource> <packageRestore> <add key="enabled" value="True" /> <add key="automatic" value="True" /> </packageRestore> </configuration> ``` This part works fine and downloads packages to x:\nugetPackages\ but when I try to build the solution I get exceptions as the dlls are not found. This makes sense as the hintPath for the references is "..\packages\lib\lib.dll" whereas I want it to be on a different drive completely. My main msbuild task is just building the Solution file. To get this to work I've tried various options in my msbuild script, from: - Specifying AssemblySearchPaths in BeforeResolveReferences task (as per http://www.beefycode.com/post/resolving-binary-references-in-msbuild.aspx) - Manually specifying AdditionalLibPaths and/or AssemblySearchPaths as a parameter when building the project. - Specifying another "activePackageSource" element in the nuget.config file. All of the above result in lots of warnings similar to: `error CS0246: The type or namespace name 'HttpRequestMessage' could not be found (are you missing a using directive or an assembly reference?)` I've managed to get it to work by altering the MSBuild script to: - Restore nuget packages to 'cache' folder (e.g. x:\nugetPackages). - Copy these packages over to the default location (..\packages). - Build solution. This seems to be an overly long winded way of dealing with this, am I missing something obvious? I've read a few blog posts where people are using XSLT to rewrite the 'hintPath' element for all project files in the solution - surely there's a better way of doing this?