How to create custom clean (post-clean) event in Visual Studio 2008?
Asked Answered
A

7

23

In our build process, for each project we use Post Build events to copy our executable files into a separate deployment directory. That works just peachy, but the problem is that we run into problems with stale files after performing a Clean Solution/Clean Project. I'd like to set up a "Clean" event that deletes the copied file and Visual Studio 2008 does not seem to provide an option in the project properties page.

It has:

Build Events:
   Pre-Build Event
   Pre-Link Event
   Post-Build Event
Custom Build Step
   General

What I'd like to find is some way to execute an arbitrary command line when the project is cleaned.

Acceptation answered 10/3, 2009 at 18:22 Comment(0)
N
42

You can use the MSBuild target syntax in your csproj file. e.g

  <Target Name="AfterClean">
    <Delete Files="$(OutDir)\$(TargetName).exe" ContinueOnError="true" />
  </Target>

There is a neat way to edit your .csproj file directly in the Visual Studio IDE described in the MSBuild team blog, but this is my first post so I can only include one hyperlink. (briefly: Unload the project, then right-click on it to see the entry "Edit [project].csproj" ... your csproj will come up in the IDE as an xml file with intellisense on elements and attributes. Wonderful!)

A full list of custom Targets is here.

Navel answered 18/10, 2010 at 4:24 Comment(3)
Side note: can also edit the .csproj file in notepad[++], then when saved, Visual Studio detects it to reload the project. For multiple incessant trials and errors, this was a little quicker. Doesn't have the intellisense though.Microprint
The question is tagged with [c++] and [visual-c++] which implies that he would have a .vcproj file rather than a .csproj file. The file formats appear to be differentDougall
The above link appears to be broken & doesn't have a waybackmachine entry. No idea what the original link actually had, but these two MSDoc references seem to list all build process targets: learn.microsoft.com/en-us/visualstudio/msbuild/… and learn.microsoft.com/en-us/visualstudio/msbuild/msbuild-targetsCalcimine
S
9

For Visual C++ projects, you need to add the files to the "Extensions to Delete On Clean" section under the "General" project configuration properties. Even though it claims to want extensions, it's actually using globs and will happily accept full paths and expand MSBuild variables. This worked for me:

$(ProjectDir)\deployment\*.*

I'm not sure if you can remove directories this way, but it can at least get the files.

Spiculate answered 9/8, 2010 at 21:2 Comment(2)
This isn't working for me. I cannot specify a full path nor a relative one using VS 2012Normanormal
It worked in Visual 2013 Express as soon as I figured out it is relative to intermediate directory, so $(OutDir) must be used to delete files in Output directory.Linell
E
4

If you use "Project Properties --> Configuration Properties --> Custom Build Setup" you have to remember to fill in the 'Outputs' field, otherwise the it won't run.

from:

http://blogs.msdn.com/b/visualstudio/archive/2010/04/26/custom-build-steps-tools-and-events.aspx

"The Outputs property is a semicolon-delimited list of files, and must list whatever files are generated as part of the execution of your custom build step. If you leave your Outputs property empty, your Custom Build Step will never execute, since MSBuild will determine that no outputs are out of date. If your step doesn’t generate files but you still want it to execute with every build, making up a fake filename will do the trick, since that file will never exist and MSBuild will always determine that the custom build step is out of date."

Escargot answered 27/3, 2014 at 19:34 Comment(0)
N
2

You will need to edit the .csproj files by hand and add an "AfterClean" target.

Nu answered 10/3, 2009 at 18:45 Comment(2)
Scott - Could you detail how to add the "AfterClean" target?Annul
Do you mean .vsproj - not .csproj?Annul
A
1

There is no documented way to insert custom cleanup steps, unfortunately. You can clean up your output in your pre-build event but that will still leave artifacts around just after a clean.

From MSDN, here is the order of invocation for the various build steps:

  1. Pre-Build event
  2. Custom build steps on individual files
  3. Proxy generator
  4. MIDL
  5. Resource compiler
  6. The C/C++ compiler
  7. Pre-Link event
  8. Linker or Librarian (as appropriate)
  9. BSCMake
  10. Custom build step on the project
  11. Deployment tool.
  12. Post-Build event

    MSDN: Understanding custom build steps

Arduous answered 10/3, 2009 at 21:40 Comment(1)
Gives me an idea: Write clean up script in Pre-Build, set Post-Build only to run on successful build, and purposely write a basic syntax error in your code. Build (Pre-Build does custom stuff.) Clean Project (normal cleanup).Basically
B
0

The others didn't do exactly what I wanted, and I just found a better way. Tested in VS2010 for a C++ Win32 project, go to Project Properties --> Configuration Properties --> Custom Build Setup. You can add a custom command line, and tell VS what operation to execute the command before or after.

Bobwhite answered 16/11, 2011 at 22:20 Comment(1)
Doesn't seem like the feature is working for the Clean event. There are other clean events, like CppClean, CoreClean, and others. I did not have time to try them all. Maybe I've made a mistake.Bobwhite
E
0

Perhaps this wasn't available back then (VS2008 / .NET 3.5) but it works well for me now (VS2022 Professional)... This example is to stop and delete a Windows Service named Service1 before cleaning (which would remove the executable and cause difficulties manipulating the Service database afterwards) but any cmd commands can be used here.

<Target Name="BeforeClean">
    <Message Text="This target will only be built when cleaning the project." />
    <Exec Command="cmd.exe /c sc stop Service1" ContinueOnError="WarnAndContinue" />
    <Exec Command="cmd.exe /c sc delete Service1" ContinueOnError="WarnAndContinue" />
</Target>
Establishmentarian answered 2/1 at 13:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.