how to get all nuget dependencies for offline installation - nuget

I two computers, one with internet connection and the other one without.
I want to install a Nuget package (Nuget.server) with all its dependencies on the offline computer.
Unfortunately it is not possible just to download the package itself and I have to download all dependencies manually and there are dozens of them.
How can I create a package on the computer with internet connection that contains all dependencies?
Thanks.

I just went through the pain of this and wanted to find a solution using only the NuGet CLI. Turns out it's pretty simple really:
> nuget.exe install <PACKAGENAME> -OutputDirectory <OUTPUTDIR>
The key is the -OutputDirectory switch which makes the CLI install the specified package into a directory that doesn't have a project file in it. Running this command will download the package and all of its dependencies into the output directory, with each package put into a separate sub-folder. You can then grab all of the .nupkg from the output directory and do whatever you need to do with them.
Update: As Igand points out in the comments, the -OutputDirectory switch is not actually required. If omitted nuget.exe will just download into the current folder. Still best to not download it into a folder with a project file in it (unless that is what you're after).

I had a similar need, so I created NuSave.
Cache a single NuGet package with dependencies
nusave cache package "Newtonsoft.Json#12.0.3" --targetFrameworks ".NETStandard#1.0,.NETStandard#1.3" --cacheDir "C:\path\to\my-local-feed"
Cache multiple NuGet packages from a .csproj file
nusave cache csproj "C:\path\to\project.csproj" --cacheDir "C:\path\to\my-local-feed"
Cache multiple NuGet packages from an .sln file
nusave cache sln "C:\path\to\solution.sln" --cacheDir "C:\path\to\my-local-feed"
Restore & build a solution offline using my local cache
cd C:\path\to\my-solution
dotnet restore --packages C:\path\to\my-local-feed
dotnet build --no-restore

On the computer with internet access the NuGet packages (.nupkg) should be in the local machine cache. On Windows this is in the directory similar to:
C:\Users\YourUsername\.nuget\packages
So you should be able to copy the .nupkg files from there to the computer without internet access. I would create a directory on that computer and setup a new package source pointing to that directory. Alternatively you could copy the .nupkg files to the local machine cache, just be aware there is a limit of 200 NuGet packages in the cache. Then you can create a package source pointing to the cache.

Use the dotnet restore command with the --packages flag, which will download the packages to a specified directory.
dotnet restore --packages <TargetDirectory> <ProjectPath>
Ref: dotnet restore
Specifies the directory for restored packages.

A little late to the discussion here but I just ran into the same issue. I work with a medium size software development shop that works offline. Many new NuGet packages have very large dependency trees. Trying to walk the tree manually and download all required packages was very cumbersome. In the end, I wrote the NuGet Dependency Downloader. With it you give a package ID, optionally a version, and choose if you want to allow pre-release packages. After you click "Start" it will pull down the listed package and any dependencies it needs to run. As an example, when I put in "Microsoft.AspNet.Mvc" and selected "pre-release", this tool brought down 158 packages for MVC 6.0 RC1. Hopefully this will help those working in an offline environment.
https://github.com/StuffOfInterest/NuGetDependencyDownloader

In your csproj file:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
<PropertyGroup>
Or dotnet publish with all assemblies references.

Related

Force NuGet pack to use short folder names for PCL projects

I'm having a problem with NuGet producing different output folders when run locally to the version on my build server.
When I build my PCL project locally (using nuget 2.8.6), the folder structure within the nuget package is:
lib
- portable-net45 win wpa81 wp80 MonoAndroid10 xamarinios10 MonoTouch10
- HC.Common.dll
However when I build it on our build server (TeamCity, nuget 2.8.2) I get the following folders:
lib
- portable-Profile259
- HC.Common.dll
How can I ensure that both builds output consistent folder structures? It makes switching between the "published" version and my test versions quite tricky as the reference paths do not match between versions.
Is there a way to force nuget to use the short "Profile259" name?
NuGet will normally replace the PCL profile with the individual target frameworks if it can determine them.
On your build server it looks like you do not have the the PCL profile xml files installed so NuGet is not converting the directory. So I am assuming you are using NuGet pack YourProject.csproj. If you used NuGet pack YourNuGet.nuspec the directories in the .nupkg would be the same on both machines.
I do not know of any NuGet package that uses a PCL directory of the form portable-Profile78 and I am surprised this works but it seems to. All NuGet packages with PCLs have directories of the form:
portable-net45+win+wpa81+wp80+MonoAndroid10+xamarinios10+MonoTouch10
So to make them consistent on both machines you will need to either install the PCL profiles on the build server or use NuGet pack YourNuSpec.nuspec so that the directories are not renamed by NuGet.exe.

Why windows C# projects contain nuget executable in .nuget folder?

I don't understood the overall scheme on how NuGet is used on windows platform (because I never saw this, I work with Linux and monodevelop).
In many windows projects, there is a .nuget folder in repository, like
https://github.com/YAFNET/YAFNET/tree/master/yafsrc/.nuget
In this folder there is a nuget.exe executable.
Why it is necessary to add executables to source code control(SCC)?
How NuGet build tool differs from all other binary tools like compilers, which are not included into SCC?
Why other package managers like paludis are not added to SCC, but nuget does?
What are exactly the reasons to put NuGet to SCC?
It is not necessary to add NuGet.exe to source control.
The reason some projects have .nuget/NuGet.exe, with a set of other MSBuild files, is to use the MSBuild based package restore. The MSBuild based package restore will run NuGet.exe at build time to download any missing packages.
The MSBuild based package restore has been deprecated by the NuGet team.
Visual Studio with recent versions of the NuGet Package Manager will automatically restore NuGet packages before building the project. If you need to restore NuGet packages on a build server you can use NuGet.exe to restore the NuGet packages by running a command line similar to:
NuGet.exe restore YourSolution.sln

Nuget package restore is not working on TFS build server

I followed this blog post and I expected Nuget to just work. It obviously has not. I get errors on all of my third party dll's that I expect NuGet to update automatically:
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\
Microsoft.WebApplication.targets (182): Could not copy the file "bin\AutoMapper.dll"
because it was not found.
I have set everything up correctly and I have ensured that the solution has Package restore enabled. If I delete a package locally and build, NuGet restores that package for me.
I am not sure why it is not fuctioning on the build server though.
Any Suggestions?
Try to add NugetRestore Activity to the TFS workfolw
Note:
For Team Foundation Build 2013 on-premises and Visual Studio Team Services (formerly Team Foundation Service), the default Build Process Templates already implement the NuGet Package Restore workflow without any special configuration.
I came across a similar error for log4net nuget package and I solved it in a different way. May be this will be useful to someone some day.
The error from TFS Team Build 2015
C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v14.0\WebApplications\
Microsoft.WebApplication.targets (182): Could not copy the file "bin\Log4net.dll"
because it was not found.
Cause
The path to the nuget package in the .csproj file for referencing the package in the project cannot be found
Resolution
Edit the .csproj file of the project that has the error.
Look for the node ItemGroupin the .csproj file
You will see the HintPath pointing to a folder, most like the folder contain the name of solution as where it will file the DLL.
Replace the HintPath with <HintPath>..\..\Assemblies\3rd party\log4net\1.2.10.0\2.0\log4net.dll</HintPath>
You nuget package library may not be log4net but a different library. A very easy way would be to be edit the .csproj file of a project in the solution that contains the same nuget package library, copy the reference of the nuget in the ItemGroup and replace in the project that has the error. The reference should start contain ...\Assemblies\3rd party\ ... instead of a physical file location on your machine

Nuget package restore with local feed

I'm trying to get nuget package restore working in a solution that is looking for packages in the regular, public nuget feed and in a local feed located on a shared network drive.
What I've tried so far is enabling nuget package restore on the solution, then editing the Nuget.targets and changing the PackageSources variable, but that doesn't seem to work. Is this possible?
My Nuget.targets file looks like this for the package sources section:
<ItemGroup Condition=" '$(PackageSources)' == '' ">
<!-- Package sources used to restore packages. By default, registered sources under %APPDATA%\NuGet\NuGet.Config will be used -->
<!-- The official NuGet package source (https://nuget.org/api/v2/) will be excluded if package sources are specified and it does not appear in the list -->
<PackageSource Include="https://nuget.org/api/v2/" />
<PackageSource Include="\\mynetworkshare\Development\NugetPackages" />
</ItemGroup>
The error I get on build is "Unable to find version '1.0.0' of package 'MyCustomPackage'. I have verified that the nupkg exists at \\mynetworkshare\Development\NugetPackages\MyCustomPackage.1.0.0.nupkg.
The fix for me was bhuvak's answer plus I had to rename some packages.
I had been creating symbols packages (named similar to MyCustomPackage.1.0.0.symbols.nupkg) and using those from my local package feed folder. However, package restore doesn't seem to pick those up. I had to rename them, removing the .symbols portion of the file name.
After I did that, package restore works great.
Updating the sources in NuGet.targets should work.
Make sure you have added both official NuGet source and local feed explicitly in the targets file.
What error are you noticing ?
We had a similar issue to #evan. We're using the CreateNewNuGetPackageFromProjectAfterEachBuild package to create our NuGet packages on a TFS 2013 build server. We have a local, share based package source and copy the nupkg files from the build output to this share. We found that builds that reference these packages fail because NuGet package restore fails to find the packages in the local source.
Running NuGet from the command line on the build server we get strange behaviour:
nuget.exe list <search-term-for-our-packages> -allversions
always succeeds and lists each version of our packages. However, when we go to a failed build's \src folder and run
nuget.exe restore .\oursolution.sln
we always get:
Unable to find version '1.0.6.341' of package 'our.package.name'.
I tried all sorts of things to resolve this, playing with nuget.config and swapping between versions of nuget.exe but the fix was simply to do with the naming of our nupkg files. By default CreateNewNuGetPackageFromProjectAfterEachBuild names the nupkg that it generates as the package Id plus the build configuration and target. E.g.
our.package.name.1.0.6.341.Release.AnyCPU.nupkg
With this file in our package source nuget list works but nuget restore fails. When I rename the package to
our.package.name.1.0.6.341.nupkg
then nuget list and nuget restore work as expected.
Our fix is simply to modify the config file of CreateNewNuGetPackageFromProjectAfterEachBuild to prevent it including the build configuration and target. But why does nuget restore depend on the package file name whereas nuget list doesn't?

Create nuget package with multiple DLLs

Let's say I have a project with this structure:
MyLibrary\
MyLibrary.sln
MyLibrary.Core\
MyLibrary.Core.csproj
MyLibrary.Extensions\
MyLibrary.Extensions.csproj
MyLibrary.Tests\
MyLibrary.Tests.csproj
I want to create a single NuGet package which packages MyLibrary.Core.dll and MyLibrary.Extensions.dll. I can't seem to figure out how to get NuGet to do this. I've tried building a spec file manually and I've tried building one using "nuget spec MyLibrary.Core.csproj". I've tried adding all of the DLLs to a lib/ folder which I understand to be the convention-based mechanism for adding DLLs to the package. In every case I can get the MyLibary.Core.dll to get into the package but the MyLibrary.Extensions.dll does not end up packaged along with it.
TLDR: What is the best practice for creating a NuGet package with multiple projects / assemblies? Is there a tutorial out there that focuses on this? The tutorials I've found all focus on simple single-project demos.
You'll run NuGet on a single project (or nuspec file), but it supports pointers to other projects via the file element. This element uses the names of your project's References, so you avoid having to a) find the location of other project files, and b) copy files to a particular place as a post-build step.
Supposing you have a nuspec file for MyLibrary.Core.csproj, and it references MyLibrary.Extensions and MyLibrary.Tests such that they end up in the bin directory after a build:
<package xmlns="http://schemas.microsoft.com/packaging/2010/07/nuspec.xsd">
<metadata>
...
</metadata>
<files>
<file src="bin\Release\MyLibrary.Extensions.dll" target="lib\net40" />
<file src="bin\Release\MyLibrary.Tests.dll" target="lib\net40" />
</files>
</package>
With this setup, all of your references should end up in the appropriate place in the NuGet package. You still have the hard-coded 'Release' in there, but I'd wager most probably don't distribute NuGet packages of their debug builds anyway.
Did you generate a blank nuspec file with:
nuget spec
If you use that file and then put your dlls in a folder under it named lib, it will package them up.
I had a little trouble with trying to generate a nuspec file from a project or dll. Also, if you manually reference any files in the nuspec file, the conventions are not used. This is probably the problem with nuspecs generated from dlls or projects.
Also, if you are trying to run this from a build script that executes in a different folder, you can tell nuget the location of your .\lib folder via the -BasePath command line:
build\nuget.exe pack nuget\Company.Project.nuspec -BasePath nuget\
Have you tried NuGet Package Explorer? Might be the easiest way:
http://nuget.codeplex.com/releases/view/59864
It seems your problem is the same as this question: Why doesn't nuget include the referenced project when packing?. If so, you can use the -includereferencedprojects option (See http://docs.nuget.org/docs/reference/command-line-reference#Pack_Command).
I recently published a solution for this...
My solution enables automatic creation of NuGet packages when you build the solution where each package can contain multiple assemblies, references to both external NuGets and NuGets created during the same build and even include the source code for debugging.
In your case, all you will need to do is add a new class library project to your solution, reference the projects you want to package, then add a post build event.
You can find an article with a walk-through guide here
and the source code here.
i have some tutorial how i did it with windows and visual studio:
create local folder and call it packages like: c:/packages//lib - important to create another folder in folder call it lib and past there dll.
open nuget package explorer - https://npe.codeplex.com/downloads/get/clickOnce/NuGetPackageExplorer.application
the ui very intuitive just add dll and export it to /lib (for tutorial https://blog.zwezdin.com/2014/building-nuget-packages-with-gui-tool/ it in russian but see on pictures the flow it's about 3 clicks)
it will create nuspec file
open GIT BASH - https://git-for-windows.github.io/ and navigate to: cd c: => cd packages (the path of )
*maybe on windows you will need provide developer options for windows's linux stuff (https://www.howtogeek.com/249966/how-to-install-and-use-the-linux-bash-shell-on-windows-10/)
in GIT BASH enter command: nuget add -source [options]
where: : the full name of nuspec (include .nuspec)
and: the path of folder lib in folder (c:/packages//lib)
after the action ended successfully
in GIT BASH enter another command:
nuget pack .nuspec
not in this folder you have .nupkg file.
How to install a Nuget Package .nupkg file locally? - tutorial how to add it to visual studio.
I had the same problem and I decided to create Nuget which will allow to create other nugets from chosen project.
Package is deployed on the Nuget.org site. After referencing it in the project You need to add nuspeck file to the projects which should generate the projects.
Project with the required nuspeck file
Last thing which should be done by you is invoke command Create-Nuspec in Package Manager. Than the powershell module will take all libraries which are result of the build it will add also the required dependencies and create the nuget in the output directory.
Description about this package is placed here.
I had an issue when adding extra dlls references to a Nuget packages, and testing the package on a sample project, the extra dlls was not being added apparently, no matter the way that I create the Nuget Package.
Then I released that when you uninstall and install again a local Nuget Package with the same version number the changes no take effect, the extra dlls are not added.
So each time you uninstall the package, close visual studio and clear the Nuget Cache,
How to clear NuGet package cache using command line?
Then open again Visual Studio and reinstall the local package to make the change takes effect.
Or make the package version to increase each time to be for Visual Studio to recognize your changes.
For example:
Package-1.0.0
Package-1.0.1
Package-1.0.2
.....
To create a Nuget package from a sample project there are some ways, for example:
Right click to your Visual Studio project and choose the "Pack" option
Then install the nuget package explorer:
https://www.microsoft.com/es-ec/p/nuget-package-explorer/9wzdncrdmdm3?activetab=pivot:overviewtab
And add manually your extra references (dlls files) to your specific targets.
Create a nuget package using a .nuspec file how specified in the documentation:
https://learn.microsoft.com/en-us/nuget/guides/create-packages-for-xamarin
<files>
<file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.dll" target="lib\netstandard1.4\Plugin.LoggingLibrary.dll" />
<file src="Plugin.LoggingLibrary\bin\Release\Plugin.LoggingLibrary.xml" target="lib\netstandard1.4\Plugin.LoggingLibrary.xml" />
<file src="Plugin.LoggingLibrary.iOS\bin\Release\iOsDependence.dll" target="lib\Xamarin.iOS10\iOsDependence.dll" />
<file src="Plugin.LoggingLibrary.Android\bin\Release\AndroidDependence.dll" target="lib\MonoAndroid10\AndroidDependence.dll" />
</files>
In your files part add your .dll files.
And dont forget, each time you uninstall and install again the nuget package from your local source.
Or you increase the version of the package each time:
Or close Visual Studio, clean the nuget cache and Rebuild your project.
In order to take effect the changes.