【问题标题】:MSBuild pre clean customizationMSBuild 预清理自定义
【发布时间】:2011-02-18 04:31:01
【问题描述】:

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

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

【问题讨论】:

    标签: visual-studio-2010 msbuild


    【解决方案1】:

    Sergio 的回答应该有效,但我认为覆盖 BeforeClean/AfterClean 目标可能会更干净。这些是微软提供的构建/清理过程的挂钩。当你进行清理时,VS 会调用目标:BeforeClean;Clean;AfterClean 并且默认情况下第一个和最后一个什么都不做。

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

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

    【讨论】:

    • 为此 +1。只需确保在 Import Microsoft.CSharp.Targets 文件之后添加 BeforeClean 或 AfterClean 目标,否则它会将您的 BeforeBuild/AfterBuild 自定义目标重新定义为默认的空目标。
    • 混合答案:&lt;Target Name="BeforeClean"&gt; &lt;RemoveDir Directories="$(MSBuildProjectDirectory)\mydir" /&gt; &lt;/Target&gt;
    【解决方案2】:

    您可以将名为 BuildCustomAction.csproj 的特殊目标添加到您的 VS .sln 文件中:

    <?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 中的三个常见操作执行自己的自定义操作:BuildRebuildClean

    使用 CustomBeforeMicrosoftCommonTargetsCustomAfterMicrosoftCommonTargets 自定义构建过程存在更复杂的方法,但它需要在 MSBuild 中非常好。

    希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 2011-04-24
      • 1970-01-01
      • 2021-10-10
      • 2010-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多