steavy
steavy

Reputation: 1576

What is the best way to restore nuget packages?

I have a solution that uses custom nuget package sources. For now I specify them in Nuget.config file that is located near my solution file(so that it is checked out from source control):

|- MySoulution.sln
|- MyProjFolder
|- .nuget
   |- Nuget.exe
   |- Nuget.config
   |- Nuget.targets

This works well when building solution from VisualStudio. It manages to read this nuget.config file and successfully restore all packages.

Now I'm configuring my solution to be built from TeamCity. So I've added project configuration and a build step to build it. But TeamCity doesn't restore nuget packages by default. So I've added a separate Nuget installer build step that runs nuget(of specific version) restore for my solution. But the problem is that it doesn't seem to see my custom nuget package sources from Nuget.config file in .nuget folder next to solution file.

I see two possible ways to overcome this:

  1. Configure my custom package sources inside Nuget installer build step.
  2. Configure my custom package sources in Nuget.config in AppData folder on build machine.

I don't like neither of this approaches because they don't provide me single poing of configuration for building both from TeamCity and VisualStudio.

To sum up, the question is: how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places?

Upvotes: 0

Views: 1275

Answers (1)

Leo Liu
Leo Liu

Reputation: 76760

how do I configure my custom package sources so that they would be visible both from TeamCity and VisualStudio without requiring me to configure them several times in different places?

As you know, if you do not want to configure custom nuget sources several times in different places, you can set the custom nuget sources in the NuGet.config and add it to source control. So the key to your problem is why NuGet doesn't respect the your custom nuget package sources from Nuget.config file in .nuget folder next to solution file.

Just as my comment, if you're using NuGet 2.7 or later and have a solution that is still configured for MSBuild-integrated restore, you may have an older version of nuget.exe in the solution's .nuget folder. This will cause builds to fail with an error stating that you have not given consent to restore packages. To avoid this issue, it's recommended to migrate any project using MSBuild-integrated restore to use the automatic restore capabilities of NuGet 2.7 and above, you can follow the process as below:

  1. Close Visual Studio to avoid file potential file locks and conflicts.

  2. If using TFS: Remove nuget.exe and NuGet.targets from the solution's .nuget folder and remove those files from the solution workspace. Retain Nuget.Config with the disableSourceControlIntegration setting as explained in Omitting packages with Team Foundation Version Control.

  3. If not using TFS: Remove the .nuget folder from the solution and the solution workspace.

  4. Edit each project file in the solution, remove the element, and remove any references to the NuGet.targets file. Those settings generally appear as follows:

After that put NuGet.config next to the solution file with custom NuGet source:

<?xml version="1.0" encoding="utf-8"?>
   <configuration>
     <solution>
       <add key="disableSourceControlIntegration" value="true" />
     </solution>

    <packageSources>
      <add key="CustomSource" value="http://CustomSource/nuget" />
    </packageSources>

    <packageRestore>
      <add key="enabled" value="True" />
    </packageRestore>

  </configuration>

Now, in the NuGet Installer step, there is now a "Package Sources" field that you can fill in to have team city use a custom feed:

enter image description here

You can refer to this document NuGet Package Restore with TeamCity for more detail.

Besides, we can also specify custom feed in the NuGet.targets file in the .nuget folder(I did not verify it yet):

<ItemGroup Condition=" '$(PackageSources)' == '' ">
    <PackageSource Include="https://nuget.org/api/v2/" />
    <PackageSource Include="\\MyShare" />
    <PackageSource Include="http://MyServer/" />
</ItemGroup>

Upvotes: 1

Related Questions