Visual Studio中的Before和AfterBuild目标未触发

时间:2012-05-09 12:41:52

标签: visual-studio-2010 msbuild

我正在做以下事情:

  • 我创建了一个默认的类文件项目
  • 编辑csproj文件以包含Pre和Post BuildEvents
  • 取消注释默认注释掉的BeforeBuild和AfterBuild目标

BeforeBuild和AfterBuild目标在Visual Studio中不是调用窗体,而是来自msbuild命令行,为什么会这样?

我宁愿使用msbuild目标而不是PostBuildEvent,就好像它给我更多的力量和灵活性,假设它有效。

干杯,

亚当

我缩短了输出中的一些路径,因此如果它们不一致就是


ClassLibrary1.csproj更改

<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Target Name="BeforeBuild">
  <Message Text="### BeforeBuild ###" />
</Target>
<Target Name="AfterBuild">
  <Message Text="### AfterBuild ###" />
</Target>
<PropertyGroup>
  <PreBuildEvent>echo PRE_BUILD</PreBuildEvent>
</PropertyGroup>
<PropertyGroup>
 <PostBuildEvent>echo POST_BUILD</PostBuildEvent>
</PropertyGroup>

来自VS 2010的构建输出

------ Rebuild All started: Project: ClassLibrary1, Configuration: Debug Any CPU ------
PRE_BUILD
ClassLibrary1 -> c:\ClassLibrary1\bin\Debug\ClassLibrary1.dll
POST_BUILD
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========

并从命令行

#>msbuild ClassLibrary1.sln
Microsoft (R) Build Engine Version 4.0.30319.1
[Microsoft .NET Framework, Version 4.0.30319.239]
Copyright (C) Microsoft Corporation 2007. All rights reserved.

Build started 09/05/2012 13:27:42.
Project "c:.sln" on node 1 (default targets).
ValidateSolutionConfiguration:
  Building solution configuration "Debug|Any CPU".
Project "c:.sln" (1) is building "c:\ClassLibrary1.csproj" (2) on node 1 (default targets).
    BeforeBuild:
      ### BeforeBuild ###
PreBuildEvent:
  echo PRE_BUILD
  PRE_BUILD
GenerateTargetFrameworkMonikerAttribute:
Skipping target "GenerateTargetFrameworkMonikerAttribute" because all output files are up-to-date with respect to the input files.
CoreCompile:
Skipping target "CoreCompile" because all output files are up-to-date with respect to the input files.
CopyFilesToOutputDirectory:
  ClassLibrary1 -> c:\bin\Debug\ClassLibrary1.dll
PostBuildEvent:
  echo POST_BUILD
  POST_BUILD
AfterBuild:
  ### AfterBuild ###
Done Building Project "c:\ClassLibrary1.csproj" (default targets).

Done Building Project "c:.sln" (default targets).

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.18

2 个答案:

答案 0 :(得分:47)

您的构建事件正在触发,您只是在Visual Studio中看不到它们。

默认情况下,VS将msbuild详细程度设置为minimal。您可以通过将邮件重要性更改为high

来显示您的邮件
<Target Name="BeforeBuild">
  <Message Text="### BeforeBuild ###"  Importance="high" />
</Target>
<Target Name="AfterBuild">
  <Message Text="### AfterBuild ###" Importance="high" />
</Target>

您还可以在工具 - &gt;选项下更改VS中的详细程度设置,然后在项目和解决方案 - >构建和运行下更改。

答案 1 :(得分:4)

只是为了别人的帮助,当他们遇到类似的问题但原因可能会有所不同。 如果您在目标之后导入,那么AfterBuild也可能无法正常工作。

确保您的所有导入应该在目标定义之前,目标定义应该在最后