MSBuild预清理定制化

7

我正在使用Visual Studio 2010。我已将项目输出指向一个特定的文件夹,该文件夹在构建时将包含所有的DLL和EXE文件。但是当我清理解决方案时,该文件夹并没有被清理,DLL仍然存在其中。

有人能告诉我如何处理清理解决方案命令以清除我想要清理的文件夹吗?我尝试使用MSBuild,并处理BeforeClean和AfterClean目标,但没有提供所需的结果。

2个回答

8

Sergio提供的答案应该是有效的,但我认为更好的方法是覆盖BeforeClean/AfterClean目标。这些是由Microsoft提供的构建/清理过程的钩子。当您清理时,VS会调用目标:BeforeClean; Clean; AfterClean,默认情况下第一个和最后一个都不做任何事情。

在您现有的.csproj文件中,您可以添加以下内容:

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

1
请确保在导入Microsoft.CSharp.Targets文件后添加BeforeClean或AfterClean目标,否则它会重新定义您的BeforeBuild/AfterBuild自定义目标为空默认目标。+1支持此内容。 - Brian Walker
重写答案为: <Target Name="BeforeClean"> <RemoveDir Directories="$(MSBuildProjectDirectory)\mydir" /> </Target> - Jeroen

2
您可以在VS的.sln文件中添加一个名为“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>

将它放在你想要的任何地方,并指定二进制输出目录的相对路径。在VS中将此项目添加为现有项目即可。这样,你就可以为VS中的三个常见操作(Build、Rebuild、Clean)创建自定义操作了。如果你很擅长MSBuild,还可以使用CustomBeforeMicrosoftCommonTargets和CustomAfterMicrosoftCommonTargets来更复杂地自定义构建过程。希望这能帮到你。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接