1

How to build a solution which uses nuget to resovles packages on a computer over MSBuild, where no visual studio is installed?

I've tried creating a .nuget directory and store the command line nuget.exe in it, but this does not help. Or is this not possible?

Thank you!

1 Answer 1

2

So you have a set of projects that are using NuGet packages and you want to be able to restore those packages before or during the build using MSBuild.

The basic approach is to run NuGet.exe restore for your solution before building the projects. So a simple way to do this is have a batch file which does that before you build your solution.

If you want MSBuild to do the package restore there are three approaches that I know of. Two of them use an MSBuild .targets file which runs the NuGet.exe restore for the solution. Another approach is to simply have a pre-build step that runs NuGet.exe restore for your solution.

  1. Visual Studio's Enable NuGet Package Restore.
  2. Cross Platform NuGet restore created by Daniel Cazzulino.
  3. Simple pre-build step that runs NuGet.exe restore.

Note that the Enable NuGet Package Restore menu in Visual Studio has been deprecated by the NuGet team. There are problems with this approach of doing the nuget package restore during the build if the NuGet packages being used have their own MSBuild .targets files. This problem can also occur for option 3.

Visual Studio's Enable Package Restore adds a NuGet.targets file to your project. This .targets file runs NuGet.exe to restore your NuGet packages by defining a RestoreCommand and then having MSBuild execute it. Some parts of this NuGet.targets file is shown below.

  <RestoreCommand>$(NuGetCommand) install "$(PackagesConfig)" -source "$(PackageSources)"  $(NonInteractiveSwitch) $(RequireConsentSwitch) -solutionDir $(PaddedSolutionDir)</RestoreCommand>

 <!-- We need to ensure packages are restored prior to assembly resolve -->
 <BuildDependsOn Condition="$(RestorePackages) == 'true'">
    RestorePackages;
    $(BuildDependsOn);
</BuildDependsOn>

<Target Name="RestorePackages" DependsOnTargets="CheckPrerequisites">        
    <Exec Command="$(RestoreCommand)"
          Condition="'$(OS)' != 'Windows_NT' And Exists('$(PackagesConfig)')" />

    <Exec Command="$(RestoreCommand)"
          LogStandardErrorAsError="true"
          Condition="'$(OS)' == 'Windows_NT' And Exists('$(PackagesConfig)')" />
</Target>

Daniel Cazzulino's package restore approach has a Before.[solution file name].targets file which is similar to the NuGet.targets file. One advantage of using the Before.YourSolution.targets file that it runs before the solution is built so it does not have the same problem as the other two options if your NuGet packages use their own .targets files.

The last approach is similar to the other two but instead of having a .targets file you add a pre-build step into your project which runs NuGet.exe. This is basically the same as moving some of the contents of the NuGet.targets file into your project.

<Target Name="BeforeBuild">
  <PropertyGroup>
    <NuGet>$(SolutionDir)\.nuget\NuGet.exe</NuGet>
  </PropertyGroup>
  <Exec Command="$(NuGet) restore -SolutionDirectory $(SolutionDir)" />
</Target>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.