开发者

MSBuild pre clean customization

I am working with Visual Studio 2010. I have directed project output to a specific folder which will contain all the DLLs and EXEs when built. However when I clean the solution, the folder is not getting cleaned, and the DLLs are still present in it.

Can anyone tell me how to handle the clean solution command to clear out the folders I want to clean? I tried working 开发者_如何学Gowith MSBuild and handling the BeforeClean and AfterClean targets, but it did not provide the desired result.


The answer from Sergio should work but I think it could be cleaner to override the BeforeClean/AfterClean targets. These are hooks into the build/clean process provided by microsoft. When you do a clean, VS do call the targets : BeforeClean;Clean;AfterClean and by default the first and the last do nothing.

In one of your existing .csproj file you can add the following :

<Target Name="BeforeClean">
  <!-- DO YOUR STUFF HERE -->
</Target>


You can add to your VS .sln file special target named let's say BuildCustomAction.csproj:

<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build"     xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
  </PropertyGroup>

  <ItemGroup>
      <CleanOutCatalogFiles Include="..\..\bin\$(Configuration)\**\*.dll">
         <Visible>false</Visible>
      </CleanOutCatalogFiles> 
      <CleanOutCatalogFiles Include="..\..\bin\$(Configuration)\**\*.exe">
         <Visible>false</Visible>
      </CleanOutCatalogFiles> 
  </ItemGroup>    

  <Target Name="Build">                                 
  </Target>                                            

  <Target Name="Rebuild"
          DependsOnTargets="Clean;Build">                    
  </Target>

  <Target Name="Clean"
          Condition="'@(CleanOutCatalogFiles)'!=''">
    <Message Text="Cleaning Output Dlls and EXEs" Importance="high" />
    <Delete Files="@(CleanOutCatalogFiles)" />
  </Target>
</Project>

Place it everywhere you want and specify relative path to the output catalog for your binaries. Add in VS this project as existing. That's all. With this you can do own custom actions for three common actions in VS: Build, Rebuild, Clean.

There exists more complex way to customize build process using CustomBeforeMicrosoftCommonTargets and CustomAfterMicrosoftCommonTargets but it requires to be very good in MSBuild.

Hope this helps.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜