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:
MSBuild.exe ... /p:RestorePackages=false
.nuget\nuget.exe restore solution.sln
restores the packages 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.
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.
You can use "MSBuild /p:RestorePackages=false" to disable package restore on build command.
Nowadays, I suggest making your CLI approach more comprehensive and reliable.
Short plan
Details
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
Use PowerShell module VSSetup
. And choose x86 or x64 MSBuild version
Download it or install from here: Github: Microsoft/Visual Studio Setup PowerShell Module
Run MSBuild with clean
target
Help nuget.exe
to use proper MSBuild
nuget.exe restore -MSBuildPath "C:\..."
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With