Web developer from Sydney Australia. Currently using asp.net, mvc where possible.

Tuesday, July 24, 2012

NuGet Package Restore with a Local Repository and Offline Access

NuGet package restore is a great feature as it means you no longer need to check in packages to source control. The only downside is that you need to have a internet connection to "restore" the packages.

This post is going to help address that issue by showing how you can keep your downloaded NuGet packages in a central location to save space and provide offline access.

Warning: this works for me but it isn't a supported use case, so YMMV + use at your own risk.

Firstly, enable NuGet package restore on your solution

To enable package restore (make sure you have the NuGet.2.0 extension installed) simply right click on your solution and select "Enable NuGet Package Restore"

Once this is done any packages missing from your packages folder will be automatically downloaded when you build the solution. Try it out.., delete any folder under the "packages" directory and rebuild the solution. NuGet will automatically download the missing packages.

Next, Set up a central package repository

Now to minimize the number of items we need to download (and disk space) we are going to configure NuGet to use a single location for the packages. Normally NuGet will store the packages in a "packages" folder at the solution level but this means there is a lot of duplication of files. I have setup a global location for my packages at  "C:\ddrive\dev\_nuget_repository\packages" but this can vary between computers as it's based on an environment variable.

To do this, first create an environment variable called PackagesDir 
Next modify the "NuGet.targets" (this file is added to your solution in the first step) and add the attribute: Condition=" '$(PackagesDir)' == '' "  to the two lines, as shown below (NuGet.targets):
NB: Commit all the files under the .nuget folder to source control.
You might need to restart visual studio but now you should have your packages stored in your preferred central location.

Finally, Lets set up our own NuGet feed based on the packages in this central repository.

Notice how I set a extra "packages" directory to my location. This is because the packages directory will hold all the extracted packages created by NuGet and visual studio. We will  use the parent directory as the NuGet Feed directory.

This relies on a powershell script to copy the .nupkg (zipped packages) which exist in the sub directories up to the parent ( in my case its: C:\ddrive\dev\_nuget_repository\).

Save the following power shell command to a file called "_PackageCopier.ps1" into the _nuget_repository directory :
"" "Package Copier Starting..........." "" if($args.length -ne 2) { $source = resolve-path "..\..\packages" $destination = resolve-path "..\..\_nuget_repository\" } else { $source = $args[0] $destination = $args[1] } if( -not (Test-Path -PathType Container $source)) { throw "source directory does not exist, source: " + $source } if( -not (Test-Path -PathType Container $destination)) { throw "Destination directory does not exist!! " + $destination } Get-ChildItem -Recurse -Filter "*.nupkg" $source ` | Where-Object { -not( Test-Path -PathType Leaf (Join-Path $destination $_.Name)) } ` | Copy-Item -Destination $destination -Verbose `
Now save the following bat file into the same directory (called "_update_from_repository.bat" ):
powershell.exe .\_PackageCopier.ps1 "packages" "." pause
If you are errors running powershell see stackoverflow. Basically I have set my execution policy to unrestricted for both 32bit and 64bit powershell prompts.

Now when you double click the _update_from_repository.bat your local NuGet repository will be updated with all the NuGets you are using from all your solutions.

Setup the local repository in Visual Studio:


That's it. You should now have a central repository for all NuGet packages and the a local repository of all the packages you have used.

If would be great if the NuGet.targets file could be modified permanently globally to respect an environment variable!


Below is a screen grab of what my _nuget_repository directory looks like:





kick it on DotNetKicks.com      Shout it

6 comments:

seoinheritx said...

I enjoyed reading your blog! Thanks for posting such Wonderful information.

Ruby on Rails Development | Asp.net Development

Govind Sharma said...

This was an excellent post for such a hard subject to speak about.

Thanks.....

Mechanical Seals

Tegar Fajar said...


Prediksi Bola
Judi Online
Bursa Taruhan Casino
Taruhan Bola
Judi Bola Terpercaya
Bandar Bola Online
Agen Taruhan
Prediksi Bola
Judi Online
Agen Casino
Taruhan Bola
Casino Online
Agen Bola
Prediksi Dan Berita Sepakbola
Agen Bola Online
Bandar Bola
Agen Casino
Situs Judi
Agen Bola
Taruhan Online
Bandar Judi Terpercaya
Agen Bola
Taruhan Bola Online
Agen Casino Terpercaya
Taruhan Bola
Judi Online
Agen Casino Terpercaya
Agen Bola Online
Situs Judi Casino Online
Bandar Taruhan Bola
Agen Casino
Judi Online
Bursa Taruhan
Bandar Bola
Agen Judi Bola
Bandar Bola Online
Casino Online
Informasi Terupdate
Sumber Berita
Sumber Inspirasi
Berita Terbaru
Inovatif
Berita Bola
Prediksi Bola Jitu
Hanya Untuk Anda Pecinta Dunia Maya
Info Masa Depan
Ulas Bola Hari Ini
Info Masa Depan
Ulas Bola Hari Ini
Info Terupdate
Berita Inovatif
Berita Pilihan
Prediksi Skor
Berita Bola
Jadwal Bola
Prediksi Bola
Pasaran Bola
Berita Bola Online
Informasi Terkini
Prediksi Dan Berita Sepakbola
Info Berita Terbaik

Tegar Fajar said...


Bursa Taruhan
Berita Terhangat
Info Masa Depan
Berita Terupdate
Sumber Berita
Hasil Skor
Prediksi Bola Terpercaya
agen texas poker dan domino
bola pelangi agen bola sbobet
Judi Online
Taruhan Bola
Agen Casino
Bandar Bola
Agen bola
poker
judi on-line
Prediksi bola
togel
Info Skor Hari Ini
Update Bola
Prediksi Online
Bursa Taruhan Bola
Berita Terkini
Informasi Akurat Berimbang
Jadwal Dunia Sepak Bola
Prediksi Skor
Dapatkan Info Berita Bola Terbaru
Informasi
Berita Terbaru Era Masa Kini
Harian Bola
Bursa Online dan Berita Bola
Info Terakurat dan Terpercaya
Opini Info Terkini
Info Terkini
Kumpulan Informasi
Info Online
Berita Bola
Prediksi Skor
Cerita Dewasa
Semua Tentang Bola dan Khusus Dewasa
Portal Berita Bola
Prediksi Skor
Berita Informasi Terkini
Harian Bola
Ulas bola
Info Akurat
Prediksi Bola
Berita Bola
Jadwal Bola
Info Berita Bola
Prediksi Skor
Cerita Dewasa
Sumber Informasi Terkini

Tegar Fajar said...


Cerita Dewasa
Berita Akurat Bola
Cerita Sex dan Foto Bugil
Berita Akurat Bola
Cerita Sex dan Foto Bugil
Berita Akurat Bola
Cerita Sex dan Foto Bugil
Prediksi Bola
Info Akurat
Cerpen Dewasa
Foto Seksi
Info Bola
Bursa Bandar Bola
Dewasa News
Prediksi Bola Malam
Berita Bola
Berita Baru dan Selalu Update
Informasi Terkini
Prediksi Dan Berita Sepakbola
Berita Terkini Dan Terpecaya
Bocoran Togelria.com
Prediksi Togel Singapura
Hongkong
Macau
Info Asia Skor
Prediksi Skor
Berita Bola
Info Berita
Prediksi Skor
Berita Bola
Tips Dan Trik
Ulas Bola Hari Ini
Prediksi Skor
Jadwal Bola
Agen Bola Sbobet
Agen Bola Sbobet - Judi Online
Bursa Taruhan Bola
Agen Bola Terpercaya
Judi Bola
Taruhan Bola Indonesia
agen casino online terbaik dan terpercaya

Tegar Fajar said...


agen casino online terbesar sbobet dan terpercaya
Buahpoker.com Agen Poker Domino Online Uang Asli Tanpa Robot
BUNDAPOKER.COM Agen Texas Poker dan Domino Online Indonesia Terpercaya
DaunPoker.Net Agen Poker Domino Online Indonesia Terbaik Terbesar dan Terpercaya
SaranaPoker.COM Agen Texas Poker Dan Domino Online Indonesia
GLOBALBOLA.COM Agen Bola Piala Dunia 2014 SBOBET IBCBET Casino Poker Tangkas Togel Online Terbaik Terbesar Dan Terpercaya
Buahpoker.com Agen Poker Domino Online Uang Asli Indonesia Terpercaya Tanpa Robot
DAUNPOKER.NET AGEN POKER DOMINO ONLINE INDONESIA TERBAIK TERBESAR DAN TERPERCAYA
Buahpoker.com Agen Poker Domino Online Uang Asli Indonesia Terpercaya Tanpa Robot
Daunpoker.net Agen Poker Domino Online Indonesia Terbaik Terbesar Dan Terpercaya
BUNDAPOKER.COM AGEN TEXAS POKER DAN DOMINO ONLINE INDONESIA TERPERCAYA
SARANAPOKER.COM AGEN TEXAS POKER DAN DOMINO ONLINE INDONESIA TERPERCAYA