Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Saturday, January 23, 2016

Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions

Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


I have been using NuGet to retrieve packages from external and internal package sources, which is very convenient. But I have realized that the packages are by default stored per solution, which is very frustrating when some projects with NuGet references are included in several solutions. Then the references are changed to other solutions package folder which may actually be unavailable to another developer or build machine.

I have seen that there are ways to point out a common package location (perhaps at the project root level, we are using TFS source control) with the release 2.1 of NuGet, see release notes . I am using NuGet v2.7

But I have tried to add nuget.config files without seeing any effect of this. Packages are still stored in the solution folder. Is there anything I have missed? There seems to be different structures of the xml node to add to the nuget.config file, depending on who is answering that question: Schwarzie suggests on another Stackoverflow thread:

    ..\..\[relative or absolute path]    

The release notes for NuGet 2.1 (see link above) suggests this format:

                  

I don't know which one of these, or any, or both will work in the end. I have tried both at solution level. Can the nuget.config file be placed on TFS project root level, or must it be in the solution directory? It seems that NuGet reads and applies the settings from these files in a certain order, why it would make sense to add them in several levels, where a nuget.config file on solution level would override one on the TFS project root level. Can this be clarified?

Do I need to remove all installed packages before those references will work? I would love if someone could provide a step-by-step instruction for moving from solution-specific nuget usage to a common package folder where projects that belong to several solutions can find their required nuget packages.

Answer by Vermis for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


As of NuGet 2.7.1 (2.7.40906.75) with VStudio 2012

I have a similar situation with external and internal package sources with projects referenced in more than one solution. I just got this working with one of our code bases today and it seems to be working with the developer workstations and our build server. The below process has this scenario in mind (although it shouldn't be hard to adapt to have the common packages folder else where).

  • Codebase
    • Project A
    • Project B
    • Project C
    • Solutions
      • Solution 1
      • Solution 2
      • Solution 3
      • Packages (this is the common one shared by all solutions)

First off the thing to keep in mind is that nuget.config does not control all of the path settings in the nuget package system. This was particularly confusing to figure out. Specifically, the issue is that msbuild and Visual Studio (calling msbuild) do not use the path in nuget.config but rather are overriding it in the nuget.targets file.

Environment Preparation

First, I would go through your solution's folder and remove all \packages\ folders that exist. This will help ensure that all packages are visibly installing into the correct folder and to help discover any bad path references throughout your solutions. Next, I would make sure you have the latest nuget Visual Studio extension installed. I would also make sure you have the latest nuget.exe installed into each solution. Open a command prompt and go into each $(SolutionDir)\ .nuget\ folder and execute the following command:

nuget update -self  

Setting common package folder path for NuGet

Open each $(SolutionDir)\ .nuget\NuGet.Config and add the following inside the section:

          

Note: You can use an absolute path or a relative path. Keep in mind, if you are using a relative path with $ that it is relative to one level below the location of the NuGet.Config (believe this is a bug).

Setting common package folder path for MSBuild and Visual Studio

Open each $(SolutionDir)\ .nuget\NuGet.targets and modify the following section (note that for non-Windows there is another section below it):

            $([System.IO.Path]::Combine($(SolutionDir), ".nuget"))      $([System.IO.Path]::Combine($(ProjectDir), "packages.config"))      $([System.IO.Path]::Combine($(SolutionDir), "packages"))    

Update PackagesDir to be

$([System.IO.Path]::GetFullPath("$(SolutionDir)\..\Packages"))  

Note: The GetFullPath will resolve our relative path into an absolute path.

Restoring all of the nuget packages into common folder

Open a command prompt and goto each $(SolutionDir)\ .nuget and execute the following command:

nuget restore ..\YourSolution.sln  

At this point, you should have a single \packages\ folder in your common location and none within any of your solution folders. If not, then verify your paths.

Fixing project references

Open every .csproj file in a text editor and find any references to \packages and update them to the correct path. Most of these will be references, but not all of them. For example, WebGrease and Microsoft.Bcl.Build will have separate path settings that will need to be updated.

Build your solution

Open your solution in Visual Studio and kick off a build. If it complains about missing packages that need to be restored, don't assume that the package is missing and needs to be restored (error can be misleading). It could be a bad path in one of your .csproj files. Check that first before restoring the package.

Have a build error about missing packages?

If you have already verified that the paths in your .csproj files are correct, then you have two options to try. If this is the result of updating your code from source code control then you can try checking out a clean copy and then building that. This worked for one of our developers and I think there was an artifact in the .suo file or something similar. The other option is to manually force a package restore using the command line in the .nuget folder of the solution in question:

nuget restore ..\YourSolution.sln  

Answer by Benjol for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


My experience trying this with the latest version of NuGet (2.7) and VS2012:

  • Delete the .nuget folder (on disk and in solution)
  • Put a NuGet.Config file in a common parent folder of all solutions
  • Delete any existing packages folders
  • Go through all csproj files and change HintPaths to point to the new location
  • Profit

In my case, I wanted to put all packages in .packages, so my NuGet.Config looked like below.

                          

Note that there are a few 'strange' things that can happen, but I think they're bearable:

  • If you 'Update' a package from one solution, it will promptly delete the older version from your packages folder (it can't know whether you have another solution which is pointing there). This doesn't bother me greatly, as the other solution will just restore when required.
  • If you try to add package from right-click-on-solution, if the package is already present in another solution, it will see that it's there and show you the 'green tick' instead of the 'install' button. I usually install from right-click-on-project, so this doesn't bother me at all.

Disclaimer: I just tried this today, I don't have any long term experience to back it up!

Answer by daniel for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


Already there is no need to modify nuget.targets. It has been fixed in nuget 2.8 (http://nuget.codeplex.com/workitem/2921). You only need to set repositorypath.

Answer by user3285954 for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


These are the instructions as of NuGet 2.1: http://docs.nuget.org/docs/release-notes/nuget-2.1

No need to edit solution level files.

Works with Visual Studio 2013 and three solutions sharing projects.

Don't forget to update solution level NuGet (each nuget.exe in .nuget folders).

Answer by Adam Wygo for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


Instead of setting common package location for all projects, it is also possible to change HintPath in project as follow:

$(SolutionDir)\packages\EntityFramework.6.1.0\lib\net40\EntityFramework.dll  

In most cases there in shared project will be only few packages, so you can easily change it.

I think it is better solution, when you branching code, when setting common repo, you must change relative path, in this solution you don't need to do this.

Answer by amarnath chatterjee for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


Updating my experience with nuget 2.8.3. It was relatively simple. All did was enabled package restore from right clicking solution. Edited NuGet.Config and added these lines :

              

Then rebuilt the solution, it downloaded all packages to my desired folder and updated references automatically. I did the same for all of my other projects, where only incremental packages were downloaded and existing packages were referenced. Hence a common package repository for all projects as been set.

Here is a step by step procedure to enable package restore.

http://blogs.4ward.it/enable-nuget-package-restore-in-visual-studio-and-tfs-2012-rc-to-building-windows-8-metro-apps/

Answer by dotnetguy for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


A short summary for those on VS 2013 professional with NuGet Version: 2.8.60318.667

This is how you would direct packages to a path relative to the .nuget folder:

                  

For example, if your solution (.sln file) resides in C:\Projects\MySolution, when you enable NuGet package restore, the .nuget folder is created like so: C:\Projects\MySolution.nuget and the packages will be downloaded to a directory like so: C:\Projects\MySolution\Dependencies

NOTE: For some (unknown) reason, everytime I update the "repositoryPath", I have to close and reopen the solution for the changes to take effect

Answer by Janusz Nowak for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


From Visual Studio 2013 Update 4 and Nugget Package Manager version > 2.8.5...

Create nuget.config file in root of repository.

file content:

                                

This will cause that all packages will go to packages folder on level of your's nuget.config file.

Now you can go for each .sln nuget console with command 'update-package -reinstall'

If you have like multiple repository at the same level and what to share the same package folder across them try use way to go one folder up.

   

But this way you cause that nuget packages reference csproj is pointing one folder up outside you repositorys path.

Answer by Matthieu for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


I have NuGet version 2.8.50926 with VS 2013. You don't need to use multiple nuget.config files, or use complex directory structures. Just modify the default file located here:

%APPDATA%\NuGet\NuGet.Config  

Here is the content of my file:

                                  

So all packages go to the "C:\Projects\nugetpackages" folder, no matter where the solution is.

In all your solutions, just delete existing "packages" folders. Then build your solution, and NuGet will automatically restore the missing packages in the new, centralized directory you specified.

Answer by user803469 for Setting up a common nuget packages folder for all solutions when some projects are included in multiple solutions


Just hardlink /packages to the desired shared location. Then your project won't be broken for other users, that do not have a special packages location.

Open a command prompt as admin and use

mklink /prefix link_path Target_file/folder_path  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.