Before reading this answer, you probably want to see:
The old way to extend MSBuild and the one mentioned in the manual that I have, in fact, is based on overriding the default goals set by Microsoft. The new way, as indicated in the second link above, is to define your own custom target and use the "BeforeTargets" and "AfterTargets" properties to make your target work before or after the intended target.
In my specific case, I needed the ANTLR Grammars task to run before the CLCompile target, which actually creates C ++ files, because the ANTLR Grammars task creates .cpp files. Therefore, the XML looks like this:
<Project ... <!-- Other things put in by VS2010 ... this is the bottom of the file --> <UsingTask TaskName="ANTLR.MSBuild.AntlrGrammar" AssemblyName = "ANTLR.MSBuild, Version=1.0.0.0, Culture=neutral, PublicKeyToken=d50cc80512acc876" /> <Target Name="AntlrGrammars" Inputs="Configuration.g" Outputs="ConfigurationParser.h;ConfigurationParser.cpp;ConfigurationLexer.h;ConfigurationLexer.cpp" BeforeTargets="ClCompile"> <AntlrGrammar AntlrLocation="$(MSBuildProjectDirectory)\Antlr.jar" Grammar="Configuration.g" RenameToCpp="true" /> </Target> <ImportGroup Label="ExtensionTargets"> </ImportGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> </Project>
As to why this is superior to PreBuildEvent and / or PostBuildEvent; it's smart enough not to rebuild .cpps when the grammar itself is not updated. You will get something like:
1> AntlrGrammars:
1> Skipping target "AntlrGrammars" because all output files are up-to-date with respect to the input files.
1> ClCompile:
1> All outputs are up-to-date.
1> All outputs are up-to-date.
It also raises a Visual Studio-independent complaint every time you run the program necessary to restore it, for example, for simple steps before and after the build.
Hope this helps someone - forever made me freak to understand.
source share