Manage dependencies with NuGet and TFS

I have a lot of customers that had played with NuGet packages some time ago and found it too difficult. Wresting with spec and config files and using command line tools is not everybody’s thing. That’s why I’ve written today a small documentation how you use NuGet today together with the TFS or VSTS to manage the dependencies between you projects. I think this is useful for many teams so I decided to share it.

Enable NuGet in Visual Studio Project

To use nuget you have to use nuget.exe. Download it an place it somewhere in your %PATH%.

If you want to enable packaging for a Visual Studio project, open the the package manager console (Tools / NuGet Package Manager / Package Manager Console). Navigate to the project where your project file (csproj) is located. Execute

nuget spec

Enable NuGet in VS Project

This generates a nuspec file inside your project folder with the same name as your project. The file contains a lot variables that get populated with values from the project.

<?xml version="1.0"?>
 <package >
  <metadata>
  <id>$id$</id>
  <version>$version$</version>
  <title>$title$</title>
  <authors>$author$</authors>
  <owners>$author$</owners>
  <requireLicenseAcceptance>false</requireLicenseAcceptance>
  <description>$description$</description>
  <releaseNotes>Summary of changes made in this release of the package.</releaseNotes>
  <copyright>Copyright 2016</copyright>
  <tags>NuGet Demo</tags>
 </metadata>
</package>

Make sure that all the properties are filled in your project. Description for example is empty per default. It will throw an error when you package if you don’t set the value or remove the variable.

image

The cool thing is: if your package has nuget packages installed, they automatically get added as a dependency.

image

Enable Package Creation and Publishing in Team Build

Add a “NuGet packager” step to you’re your build. Change the path/pattern to use your .csproj instead of the .nuspec file.

image

To publish the package you have to add a “NuGet publisher” build step. You can leave the pattern to publish all packages or specify the path to a specific – depending your project structure. The server endpoint can be anything that accepts nuget packages. This can be nuget.org, a custom nuget server, a file share or TFS/VSTO. To the latter I will come in the next chapter.

image

The simplest thing would be to have a file share. The syntax for that would be file://server/share.

image

TFS / VSTS Package Management Extension

If you install TFS 2015 Update 2 you now also have access to the market place. In VSTS this is already available for some time. You can install the “Package Management” extension – this allows you to host your own feeds inside TFS/VSTS.

Note: the extension is still in preview.

To install the extension to just browse the marketplace, search for the extension and install it in your account.

image

image

After the installation is complete you have a new tab “PACKAGES*” (the star marks the feature as preview). To create a new feed and enter a name and description. Chose if you want a private feed (scope team project) or a global feed (scope account). Also choose who can add packages to the feed.

image

Copy the URL of the feed to your clipboard.

image

To publish to the feed set the feed type in the “NuGet Publisher” build step to internal and enter the URL of the feed.

image

To consume the feed from the client and retsore from it during the build you have to configure the package source. Just add a nuget.config to your solution folder and add the package source.

<?xml version="1.0" encoding="utf-8"?>
<configuration>
 <packageSources>
  <add key="nuget.org" value="https://api.nuget.org/v3" />
  <add key="Internal" value="https://account.pkgs.visualstudio.com/DefaultCollection/_packaging/DemoFeed/nuget/v3/index.json" />
 </packageSources>
</configuration>

In your team build just reference the nuget.config and the build will automatically restore any package from the source.

image

Conclusion

So that’s it. With a few clicks and just one command line you can create a loos coupling between your projects and manage your dependencies using nuget package. I hope this is good jump start for teams new to this. Leave me a comment or contact me on twitter if you need further help.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s