Alex Netkachov
Alex Netkachov

Reputation: 13522

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:

Upvotes: 14

Views: 13353

Answers (2)

Sourav Lala
Sourav Lala

Reputation: 91

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

Upvotes: 9

Vlad DX
Vlad DX

Reputation: 4730

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.

Upvotes: 2

Related Questions