Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
806 views
in Technique[技术] by (71.8m points)

msbuild - Before and AfterBuild Target in Visual Studio not firing

I am doing the following:

  • I have created a default class file project
  • Edited the csproj file to include Pre and Post BuildEvents
  • Uncomment the default commented out BeforeBuild and AfterBuild targets

The BeforeBuild and AfterBuild targets are not called form within Visual Studio but are from msbuild command line, why is that?

I would rather use msbuild targets rather than the PostBuildEvent as if gives me more power and flexibility, assuming it works.

Cheers,

adam

I shortened some of the paths in the output, so if they are inconsistent that is why


ClassLibrary1.csproj changes

<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>

my build output from VS 2010 is

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

and from the command line

#>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:inDebugClassLibrary1.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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your build events are firing, you're just not seeing them in Visual Studio.

By default VS sets the msbuild verbosity to minimal. You can get your message to show by changing the message importance to high

<Target Name="BeforeBuild">
  <Message Text="### BeforeBuild ###"  Importance="high" />
</Target>
<Target Name="AfterBuild">
  <Message Text="### AfterBuild ###" Importance="high" />
</Target>

You can also change the verbosity setting in VS under Tools->Options then under Projects and Solutions->Build and Run.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...