{"id":1508,"date":"2014-02-25T21:17:31","date_gmt":"2014-02-25T21:17:31","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=1508"},"modified":"2019-03-18T14:07:25","modified_gmt":"2019-03-18T14:07:25","slug":"creating-local-packages-with-nuget","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/creating-local-packages-with-nuget\/","title":{"rendered":"Creating Local Packages with NuGet"},"content":{"rendered":"<p>NuGet allows us to download and install packages to our solutions using the NuGet manager in Visual Studio (or from the command line using nuget.exe) from a remote server &#8211; for example, nuget.org. But what if you want to host your own packages locally or remotely (outside of nuget.org) for example due to workplace policies or simply not wanting to publish your own libraries to a public server.<\/p>\n<p><em>All the information contained in this post was gleaned from the references below. These are fantastic posts and I would urge anyone visiting this site to go and read those resources. What I want to do here is just distill things down to some basics to remind myself how to quickly and easily reproduce packages and spec for packages<\/em><\/p>\n<p><strong>Getting Started<\/strong><\/p>\n<p>Let&#8217;s start of by creating a local (i.e. on your development machine) NuGet package folder. <\/p>\n<ol>\n<li>Simply create a folder on your machine which will be used to store your packages, let&#8217;s call it LocalPackages<\/li>\n<li>In Visual Studio 2012, select Tools | NuGet Package Manager | Package Manager Settings<\/li>\n<li>In Package Sources you&#8217;ll see the nuget.org source, click the + button, give your source a name such as &#8220;Local Feed&#8221; and set the Source to the location of your LocalPackages folder<\/li>\n<li>Press OK and we&#8217;ve now added a new package source<\/li>\n<\/ol>\n<p>At the point we now have Visual Studio set up to read from our packages folder &#8211; now we need to create some packages.<\/p>\n<p><strong>Creating a package<\/strong><\/p>\n<p>So we need to create a nupkg file (which is basically a compressed file which includes binaries and configuration &#8211; change the extension to .zip and open with your preferred zip editor\/viewer to see the layout). This can be created using the <a href=\"http:\/\/docs.nuget.org\/docs\/creating-packages\/using-a-gui-to-build-packages\" title=\"Using A GUI (Package Explorer) to build packages\" target=\"_blank\" rel=\"noopener noreferrer\">Package Explorer GUI<\/a> or via a nuspec file and nugget.exe.<\/p>\n<p>If you don&#8217;t already have it, download and install the command line version of NuGet which can be found <a href=\"http:\/\/docs.nuget.org\/docs\/start-here\/installing-nuget\" title=\"Installing NuGet\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a> see Command Line Utility. I placed mine in the folder I created previously, LocalPackages, as I will run it from here to work with the nupkg files etc.<\/p>\n<p>As I prefer to let tools do the heavy lifting for me, I&#8217;ve downloaded the clickonce application, <a href=\"http:\/\/docs.nuget.org\/docs\/creating-packages\/using-a-gui-to-build-packages\" title=\"Using A GUI (Package Explorer) to build packages\">Package Explorer GUI<\/a>.<\/p>\n<p>With the package explorer we can easily create nupkg files without a care in the world for what&#8217;s inside them :)<\/p>\n<p>So let&#8217;s create a package very quickly using the Package Explorer GUI<\/p>\n<ol>\n<li>If you haven&#8217;t already done so, download\/run the Package Explorer Gui<\/li>\n<li>Create a new package<\/li>\n<li>We can now edit the meta data or meta data source from the UI &#8211; the metadata editor gives us, a simple GUI with input fields and the meta data source is basically an text editor (well it&#8217;s a little better than that but you get the idea). For now let&#8217;s stick with the GUI meta data editor with it&#8217;s nice input fields<\/li>\n<li>Enter an Id &#8211; this is the package name, it&#8217;s the name used when installing the package<\/li>\n<li>Supply a version. NuGet allows us to download specific package versions. The version should be in the format 1.2.3<\/li>\n<li>Now supply a human friendly title, if none is supplied the Id is used instead<\/li>\n<li>Now enter a comma separated list of authors<\/li>\n<li>I&#8217;m not going to step through every UI element here &#8211; check out the <a href=\"http:\/\/docs.nuget.org\/docs\/reference\/nuspec-reference\" title=\"Nuspec Reference\" target=\"_blank\" rel=\"noopener noreferrer\">Nuspec Reference<\/a> for a complete list of fields and what&#8217;s expected<\/li>\n<li>We can add any framework assemblies and both dependencies and assmebly references via the GUI, but also we need to actually add our assemblies, i.e. what the package has been created to deploy. We drag and drop those into the right hand pane of the GUI tool<\/li>\n<li>We can right click on the right pane to add files and folders and more, all of which are a little beyond this first post<\/li>\n<li>For now just drag and drop an assembly you wish to package and accept the defaults<\/li>\n<li>Save the nupkg file to the LocalPackages folder<\/li>\n<\/ol>\n<p>If you now attempt to open this nupkg file, you&#8217;ll find it&#8217;s a binary file. If you prefer you can save the file as a nuspec (An XML file) via the GUI using the File | Save Meta data as&#8230; option and then when you are ready you can use<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nNuGet Pack MyPackageName.1.0.0.nuspec\r\n<\/pre>\n<p>However you may well find that this fails because the assemblies added may need updating to the actual locations on your hard drive, so open the nuspc file and check the src paths. See the GUI made things easy.<\/p>\n<p>Also when running the NuGet command line tool with Pack you may get a warning about the location of your assmebly, for example mine was by default stored in lib, but running the command line I find it is warning that I should move the file to a framework-specific folder.<\/p>\n<p>For now we&#8217;ll not worry about this. Instead let&#8217;s see our nuget package being used.<\/p>\n<p><strong>Is our nuget package available<\/strong><\/p>\n<p>We can now open Visual Studio (if not already opened) and assuming you&#8217;ve added your local package as outlined previously. <\/p>\n<ol>\n<li>Go to Tools | NuGet Package Manager | Package Manager Console<\/li>\n<li>Your should see &#8220;Package source&#8221; in the console, select &#8220;Local Feed&#8221; or whatever name you gave for your local package location<\/li>\n<li>Type\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nGet-Package -ListAvailable\r\n<\/pre>\n<p>and you should see the package you created<\/li>\n<\/ol>\n<p><strong>Adding a local nuget package to our application<\/strong><\/p>\n<ol>\n<li>Create yourself a new application to test your nuget package in<\/li>\n<li>Now we can add our package using the Package Manager Console, for example\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nInstall-Package MyPackage\r\n<\/pre>\n<p>or from the References folder on your solution, right mouse click and select Manage NuGet Packages, from Online, select your Local Feed and select your packages as normal and install<\/li>\n<\/ol>\n<p><strong>Remote Packages<\/strong><\/p>\n<p>Check out the excellent post <a href=\"http:\/\/docs.nuget.org\/docs\/creating-packages\/hosting-your-own-nuget-feeds\" title=\"Hosting Your Own NuGet Feeds\" target=\"_blank\" rel=\"noopener noreferrer\">Hosting Your Own NuGet Feeds<\/a>. The section on &#8220;Creating Remote Feeds&#8221; get&#8217;s you up and running in no time at all. <\/p>\n<p>But let&#8217;s list the steps anyway<\/p>\n<ol>\n<li>Creating a empty web application<\/li>\n<li>Add the NuGet.Server package using NuGet<\/em>\n<li>Change the WebConfig packagesPath value to match the path to your packages<\/li>\n<li>Run the app &#8211; yes it&#8217;s that simple<\/li>\n<li>In Visual Studio 2012, select Tools | NuGet Package Manager | Package Manager Settings<\/li>\n<li>In Package Sources click the + button, give your remote source a name such as &#8220;My Package Feed&#8221; and set the Source to the URL of your server which you just created and ran<\/li>\n<\/ol>\n<p>That&#8217;s is for now.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"http:\/\/docs.nuget.org\/docs\/reference\/nuspec-reference\" title=\"Nuspec Reference\" target=\"_blank\" rel=\"noopener noreferrer\">Nuspec Reference<\/a><br \/>\n<a href=\"http:\/\/docs.nuget.org\/docs\/creating-packages\/hosting-your-own-nuget-feeds\" title=\"Hosting Your Own NuGet Feeds\" target=\"_blank\" rel=\"noopener noreferrer\">Hosting Your Own NuGet Feeds<\/a><br \/>\n<a href=\"http:\/\/docs.nuget.org\/docs\/creating-packages\/using-a-gui-to-build-packages\" title=\"Using A GUI (Package Explorer) to build packages\" target=\"_blank\" rel=\"noopener noreferrer\">Using A GUI (Package Explorer) to build packages<\/a><br \/>\n<a href=\"http:\/\/docs.nuget.org\/docs\/creating-packages\/creating-and-publishing-a-package\" title=\"Creating and Publishing a Package\" target=\"_blank\" rel=\"noopener noreferrer\">Creating and Publishing a Package<\/a><br \/>\n<a href=\"http:\/\/docs.nuget.org\/docs\/reference\/package-manager-console-powershell-reference\" title=\"Package Manager Console Powershell Reference\" target=\"_blank\" rel=\"noopener noreferrer\">Package Manager Console Powershell Reference<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>NuGet allows us to download and install packages to our solutions using the NuGet manager in Visual Studio (or from the command line using nuget.exe) from a remote server &#8211; for example, nuget.org. But what if you want to host your own packages locally or remotely (outside of nuget.org) for example due to workplace policies [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[66],"tags":[],"class_list":["post-1508","post","type-post","status-publish","format-standard","hentry","category-nuget"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1508","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=1508"}],"version-history":[{"count":5,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1508\/revisions"}],"predecessor-version":[{"id":6851,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/1508\/revisions\/6851"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=1508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=1508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=1508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}