Can I have a Visual Studio pre-build event that skips the build of a project without a visual error?
I have a project that contains a custom XML file and a Powershell script that generates a resource file from the XML during pre-build
event.
My goal is to only build the project if there are changes to the XML file. I can already determine if the file has changed, but I can't inform Visual Studio to skip the build. Either I stop the script with an exit code of 0
(which lets the build continue) or any other number (which shows an ugly error in the Error List).
Can I have a pre-build
script decide whether to build or skip the project?
# Check to see if the current file is different from the file copied during build.
if((Test-Path $buildXmlFile) -and (Compare-Object -ReferenceObject (Get-Content $projectXmlFile) -DifferenceObject (Get-Content $buildXmlFile))){
Write-Host "Changes found! Rebuilding!"
} else {
Write-Host "No changes found! Skipping Build"
# Exit 0 will cause it to still build...
# Exit -1, Exit 1, etc. will cause a big error to show...
# HOW DO I SKIP???
}
I was able to get this working, here's how:
Terminology:
.\bin\debug\
or .\bin\release
Steps:
Run the script as a BeforeBuild
target, like so:
Since the generated file only lives in the output directory, MSBuild has nothing to compare it to. That means it will only do a full build if the input file is different than the input file that was previously copied over (remember, the copy only happens after a build, but the comparison happens before the build).