简体   繁体   中英

Can I have a `pre-build` script decide whether to build or skip the project?

TL;DR

Can I have a Visual Studio pre-build event that skips the build of a project without a visual error?

Details

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?

Example

# 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:

  • input file - The user edited file
  • output file - The file generated from the input file
  • output directory - Usually .\\bin\\debug\\ or .\\bin\\release

Steps:

  1. On build, copy the input file to the output directory (change this in the properties window of Visual Studio).
  2. Change the script to only generate if the input file is different than the previous input file (currently sitting in the output directory).
  3. 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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM