Skip to content

Using nuget packages

Eberhard Beilharz edited this page Feb 20, 2015 · 6 revisions

It's possible to use nuget packages on both Windows and Linux when using Mono >= 3 (as long as the installed nuget package doesn't have a powershell install step - most packages don't have that). This document explains the necessary steps.

Add .nuget directory

When msbuild/xbuild builds a project, one of the first build steps is to restore the nuget packages. This requires a NuGet.targets file that gets included in the .csproj file. One of the targets in NuGet.targets will download the nuget executable if it's not available - this means NuGet.exe shouldn't be added to source control.

The NuGet.targets file that ships with NuGet doesn't work on Linux, so it might be easiest to simply copy the .nuget folder from BloomDesktop to your own source tree. It should end up being a subdirectory of the directory that contains the solution (.sln) file.

Modify .csproj files

Edit the .csproj file and add the line

<RestorePackages>true</RestorePackages>

to the first PropertyGroup, e.g.:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
	<ApplicationManifest>app.manifest</ApplicationManifest>
	<ProjectGuid>{304D5612-167C-4725-AF27-B9F2BB788B57}</ProjectGuid>
	<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
	<TargetFrameworkProfile>Client</TargetFrameworkProfile>
	<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
	<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
	<Platform Condition=" '$(Platform)' == '' ">x86</Platform>
	<ProductVersion>8.0.30703</ProductVersion>
	<SchemaVersion>2.0</SchemaVersion>
	<RestorePackages>true</RestorePackages>
  </PropertyGroup>
  <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
	<DebugSymbols>true</DebugSymbols>

At the bottom of the file add the line

<Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />

after the line that imports Microsoft.CSharp.targets, e.g.

  <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
  <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
  <!-- To modify your build process, add your task inside one of the targets below and uncomment it.
	   Other similar extension points exist, see Microsoft.Common.targets.
  <Target Name="BeforeBuild">
  </Target>
  <Target Name="AfterBuild">
  </Target>
  -->
</Project>

If you don't build from a solution you'll have to adjust the import line and replace $(SolutionDir) with a different appropriate property.

Add .nuget packages to your project

Use the built-in tools of Visual Studio and MonoDevelop to add or update nuget packages to your project.

Source Control

You should check in the following files to source control:

.nuget/NuGet.Config
.nuget/NuGet.targets
.nuget/packages.config

and the packages.config files inside of your project.

You don't want to check in NuGet.exe (it'll get downloaded automatically at build time, having the advantage that you'll get the latest version), nor the packages subdirectory (that contains the nuget packages that get downloaded; since these will be downloaded at build time it's no use to pollute VCS with binary packages). It might be best to exclude these files in the .gitignore/.hgignore file:

.nuget/NuGet.exe
packages/

NUnit

A unit test project that uses NUnit should use the NUnit nuget package. This will add the necessary reference to nunit.framework.dll.

The project should also include the NUnit.Runners.Net4 nuget package. This provides nunit-console and thus makes it possible to run the unit tests without requiring a separately installed version of NUnit. This is helpful for automated builds. Recently I wasn't successful in adding the NUnit.Runners.Net4 package through MonoDevelop, so I ended up adding the line

<package id="NUnit.Runners.Net4" version="2.6.3" targetFramework="net40" />

to packages.config manually:

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="NUnit" version="2.6.4" targetFramework="net40" />
  <package id="NUnit.Runners.Net4" version="2.6.3" targetFramework="net40" />
</packages>

The NUnit.Runners.Net4 package should be added to every unit test project so that the order in which the projects build doesn't matter. The first project that includes this package will cause the download, after that nunit-console is available in all other projects as well.

NUnit can be run by using the following path:

packages/NUnit.Runners.Net4.2.6.3/tools/nunit-console.exe