Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable nuget package restore with msbuild command line options?

I want to disable NuGet package restore on build and use the individual command for that. Is this possible?

My idea is to use something like this:

nuget.exe restore
msbuild.exe /p:NuGetRestorePackages=false

Update:

  • the package updating can be disabled by MSBuild.exe ... /p:RestorePackages=false
  • and it looks like .nuget\nuget.exe restore solution.sln restores the packages
like image 398
Alex Netkachov Avatar asked Mar 28 '14 11:03

Alex Netkachov


People also ask

How do I stop NuGet package restore?

Go to Tools -> Options -> NuGet Package Manager -> General -> Package Restore. The first option disables restore itself, while the 2nd option disables on build restore. NuGet tries to restore to make sure that the packages were not deleted from disk or that the assets file (which helps the intellisense) is not deleted.

Does MSBuild restore NuGet packages?

Restore by using MSBuildYou can use msbuild -t:restore to restore packages in NuGet 4. x+ and MSBuild 15.1+, which are included with Visual Studio 2017 and higher. This command restores packages in projects that use PackageReference for package references.


2 Answers

You can use "MSBuild /p:RestorePackages=false" to disable package restore on build command.

like image 135
Sourav Lala Avatar answered Oct 04 '22 02:10

Sourav Lala


Nowadays, I suggest making your CLI approach more comprehensive and reliable.

Short plan

  1. Install Visual Studio Build Tools 2017
  2. Find proper MSBuild
  3. Clean solution
  4. Restore packages with nuget using correct MSBuild
  5. Build solution

Details

  1. Using Build Tools will give you independence from Visual Studio installation.

    Download Build Tools for Visual Studio 2017 from Visual Studio Downloads page (direct link)

    Command-line arguments documented here: Use command-line parameters to install Visual Studio 2017

    All workloads and components are listed here: Visual Studio Build Tools 2017 component directory

  2. Use PowerShell module VSSetup. And choose x86 or x64 MSBuild version

    Download it or install from here: Github: Microsoft/Visual Studio Setup PowerShell Module

  3. Run MSBuild with clean target

  4. Help nuget.exe to use proper MSBuild

    nuget.exe restore -MSBuildPath "C:\..."

  5. Run MSBuild with build target (you can add additional required parameters)


# 1. Find MS Build  

Import-Module $PSScriptRoot\VSSetup\VSSetup.psd1

$msBuildPath = (Get-VSSetupInstance | Select-VSSetupInstance -Version 15.0 -Product Microsoft.VisualStudio.Product.BuildTools).InstallationPath

if ([System.IntPtr]::Size -eq 8)
{
    $global:msbuildPath = Join-Path $msBuildPath 'MSBuild\15.0\Bin\amd64'
}
else
{
    $global:msbuildPath = Join-Path $msBuildPath 'MSBuild\15.0\Bin'
}

Write-Output "Using MSBuild from $global:msbuildPath"
Write-Output "MSBuild /version"

$msbuild = Join-Path $global:msbuildPath msbuild

& $msbuild /version


# 2. Clean

& $msbuild "$sln_file" /t:Clean /v:q /nologo


# 3. Restore

$nuget = Join-Path $PSScriptRoot "\.nuget\nuget.exe"
& $nuget restore -MSBuildPath $global:msbuildPath


# 4. Build

& $msbuild "$sln_file" /t:Build /v:q /nologo 

As the result, you will not have any filesystem, PATH or Visual Studio dependencies. And your solution will be reusable on the local machine and a build server.

like image 30
Vlad DX Avatar answered Oct 04 '22 02:10

Vlad DX