Second step: Fixing the HintPath of referenced assemblies
All distributed assemblies from the NuGet package are added as references to the project. NuGet sets the HintPath
of the reference with relative path (relative to the packages
directory). When using a shared repository as described in previous post this brings some problems.
Problem with CI builds
The shared repository is stored somewhere on your machine and all project references added by NuGet should point there. You usually don’t clone your development machine directory structure on your CI server. The CI build will fail because the assemblies were not found (but they are there in the directory).
Problem with package restore
Even NuGet package restore feature won’t help because it only downloads the missing packages and places them by default under packages
directory in solution directory. But it does not change the HintPaths
of referenced assemblies. You can’t event set the MSBuild project property AdditionalLibPath
because you don’t know all the directories where the package DLLs are placed beforehand (of course I can create a build task).
The solution to this is very easy, I found this discussion where a build property is used in HintPath
. I had to extend it a little to support both local and CI builds and turn the package restore on.
<PropertyGroup>
<PackagesDirectory>path to shared packages directory</PackagesDirectory>
</PropertyGroup>
<PropertyGroup Condition="Exists('$(PackagesDirectory)')">
<NuGetPackagesDirectory>$(PackagesDirectory)</NuGetPackagesDirectory>
</PropertyGroup>
<PropertyGroup Condition="!Exists('$(PackagesDirectory)')">
<NuGetPackagesDirectory>$(SolutionDir)\packages</NuGetPackagesDirectory>
</PropertyGroup>
This setting covers both features – local development and CI build. Value of NuGetPackagesDirectory
property will be set to the path to the shared directory (verified by Condition="Exists('$(PackagesDirectory)')"
) on local machine. On CI server this directory might or might not exist. If it does not exist it will be set to a packages
directory under solution folder (used by package restore).
And the reference to NuGet package DLL
<Reference Include="NLog">
<HintPath>$(NuGetPackagesDirectory)\NLog.2.0.1.2\lib\net40\NLog.dll</HintPath>
</Reference>
The branch and changset details are available here.