views:

1230

answers:

5

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.

A: 

I use Visual Studio for building and testing and it works fine, but I prefer using NAnt for more detailed builds.

Where I need to have specific AssemblyInfo files, or these sub-projects need to be compiled and their resulting libraries moved to a specific folder.

Microsoft's answer to NAnt is MSBuild.

This doesn't answer your question directly but these alternatives work great for when I need to perform certain functions at build/clean/setup time.

For reference, NAnt tasks that can be completed in a build file. Also the NAntContrib tasks. It makes for very powerful builds.

Bryan Bailliache
+1  A: 

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

Scott Dorman
Scott - Could you detail how to add the "AfterClean" target?
Marc
Do you mean .vsproj - not .csproj?
Marc
A: 

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

Aaron Saarela
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).
Kache4
A: 

I know this is an old question, but it came up as a Google hit while I was researching exactly the same problem, so I'm answering it anyway. :-)

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.

Drew Thaler
A: 

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: http://blogs.msdn.com/b/msbuild/archive/2005/11/23/496396.aspx

Pete