NuGet auto package restore does not work with MSBuild

后端 未结 12 2273
粉色の甜心
粉色の甜心 2020-12-02 08:51

I\'m trying to build a solution with packages content missing (except repositories.config inside) with MSBuild 12.0. I expect it to auto restore al

相关标签:
12条回答
  • 2020-12-02 09:37

    Nuget's Automatic Package Restore is a feature of the Visual Studio (starting in 2013), not MSBuild. You'll have to run nuget.exe restore if you want to restore packages from the command line.

    You can also use the Enable Nuget Package Restore feature, but this is no longer recommended by the nuget folks because it makes intrusive changes to the project files and may cause problems if you build those projects in another solution.

    0 讨论(0)
  • 2020-12-02 09:40

    Ian Kemp has the answer (have some points btw..), this is to simply add some meat to one of his steps.

    The reason I ended up here was that dev's machines were building fine, but the build server simply wasn't pulling down the packages required (empty packages folder) and therefore the build was failing. Logging onto the build server and manually building the solution worked, however.

    To fulfil the second of Ians 3 point steps (running nuget restore), you can create an MSBuild target running the exec command to run the nuget restore command, as below (in this case nuget.exe is in the .nuget folder, rather than on the path), which can then be run in a TeamCity build step (other CI available...) immediately prior to building the solution

    <Target Name="BeforeBuild">
      <Exec Command="..\.nuget\nuget restore ..\MySolution.sln"/>
    </Target>
    

    For the record I'd already tried the "nuget installer" runner type but this step was hanging on web projects (worked for DLL's and Windows projects)

    0 讨论(0)
  • 2020-12-02 09:40

    You can also use

    Update-Package -reinstall
    

    to restore the NuGet packages on the Package Management Console in Visual Studio.

    0 讨论(0)
  • 2020-12-02 09:42

    There is a packages.config file with the project, it contains the package details.

    Also there is a .nuget folder which contains the NuGet.exe and NuGet.targets. if any one of the file is missing it will not restore the missing package and cause "are you missing a using directive or an assembly reference?" error

    0 讨论(0)
  • 2020-12-02 09:47

    It took me some time to figure out the whole picture and I'd like to share here.

    Visual Studio has two approaches to use package restore: Automatic Package Restore and MSBuild-Integrated package restore. The 'MSBuild-Integrated Package Restore' restores packages DURING the building process that might cause issues in some scenarios. The 'Automatic Package Restore' is the recommended approach by the NuGet team.

    There are several steps to to make 'Automatic Package Restore' work:

    1. In Visual Studio, Tools -> Extensions and Updates, Upgrade NuGet if there is a newer version (Version 2.7 or later)

    2. If you use TFS, in your solution's .nuget folder, remove the NuGet.exe and NuGet.targes files. Then edit NuGet.Config to not check in NuGet packages:

      <configuration>  
        <solution>  
          <add key="disableSourceControlIntegration" value="true" />  
        </solution>  
      </configuration> 
      

      If you checked in the solution's packages folder to TFS before, delete the folder and check in the deletion of package folder deletion.

      If you don't use TFS, delete the .nuget folder.

    3. In each project file (.csproj or .vbproj) in your solution, remove the line that references NuGet.targets file. The reference looks like this:

      <Import Project="$(SolutionDir)\.nuget\NuGet.targets" Condition="Exists('$(SolutionDir)\.nuget\NuGet.targets')" />
      

      Remove this line in every project file in your solution.

    4. In Visual Studio menu, either through

      Tools -> Options -> Package Manager -> General or Tools -> NuGet Package Manager -> Package Manager Settings

      please enable the following two options 1) 'Allow NuGet to download missing packages' 2) 'Automatically check for missing packages during build in Visual Studio'

    5. Test your package restore configuration by the following steps

      • Save your solution and close Visual Studio
      • Delete your solution's packages folder
      • Start Visual Studio, open your solution and rebuild it.
    0 讨论(0)
  • 2020-12-02 09:47

    In Visual Studio 2017 - When you compile using IDE - It will download all the missing nuget packages and save in the folder "packages".

    But on the build machine compilation was done using msbuild.exe. In that case, I downloaded nuget.exe and kept in path.

    During each build process before executing msbuild.exe. It will execute -> nuget.exe restore NAME_OF_SLN_File (if there is only one .SLN file then you can ignore that parameter)

    0 讨论(0)
提交回复
热议问题