MS-Build BeforeBuild not firing

11,480

Solution 1

The event is firing, but you might need to change your settings in VS:

Tools->Options->Projects and Solutions->Build and Run:

And set MSBUild verbosity to minimal or normal.

Also, if you compile through msbuild in the console you will see the message without having to change the above settings.

Solution 2

Had the same problem today and found the way to make it work.

The BeforeBuild target in your .csproj file is intended to be a redefinition of a target defined (and referenced) in the Microsoft.Common.targets file, which is imported by the Microsoft.CSharp.targets file, which in turn is imported by your .csproj.

Your problem is that the line in your .csproj that imports Microsoft.CSharp.targets is after your definition of the BeforeBuild target. Move the import line to above your BeforeBuild target and everything should work fine.

Hope that helps,

Share:
11,480
Steve Cooper
Author by

Steve Cooper

Updated on June 17, 2022

Comments

  • Steve Cooper
    Steve Cooper almost 2 years

    I'm customising a .csproj project to run some custom tasks before the main build. However, I can't get the tasks to execute at all.

    I uncommented the <Target Name="BeforeBuild" /> element in the .csproj file and added a simple Message task, but when I build, the message doesn't appear in my output, so it seems the task isn't running. So this fragment does not output the message;

    Listing 1: No Message Appears

    <Target Name="BeforeBuild">
        <Message Text="About to build ORM layer" Importance="normal" />
    </Target>
    

    However, if I screw with some of the attributes, I can get the .csproj to fail to execute at all;

    Listing 2: An MSBuild configuration error

    <Target Name="BeforeBuild">
        <Message Text="About to build ORM layer" XXImportance="normal" />
    </Target>
    

    Note the XXImportance attribute. The build error I get is

    My.csproj(83,46): error MSB4064: The "XXImportance" parameter is not supported by the "Message" task. Verify the parameter exists on the task, and it is a settable public instance property.
    

    This suggests that the XML is being parsed, that the Message class has been found, and that the class is being reflected over for the available properties.

    Why would the task not execute?

    I'm using the 3.5 framework.

    UPDATE 1: On @Martin's advice, I tried to run MSBuild on the console, and got this error;

    c:\path\to\my.csproj(74,11): error MSB4019: The imported
    project "C:\Microsoft.CSharp.targets" was not found. Confirm
    that the path in the <Import> declaration is correct, and that
    the file exists on disk.
    

    Line 74 reads;

    <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
    

    UPDATE 2: I'm compiling in VS2008, which uses the C#3 compiler, but the project I'm compiling is a framework 2.0 project. When run from the command line (see UPDATE 1) the build seems to fail because there is a confusion as to where the Microsoft.CSharp.targets file is specified.

  • Steve Cooper
    Steve Cooper almost 14 years
    Hi, Martin. Thanks very much for the answer -- I appreciate it. I'm already on minimal verbosity, so I don't think it's that. I tried compiling through the console but got an error -- I've appended it to the original post. Any ideas?
  • Martin Ingvar Kofoed Jensen
    Martin Ingvar Kofoed Jensen almost 14 years
    Did you use the Visual Stuido Command Pront found under Visual Studio Tools (under Microsfot Vistual Studio 20xx) in the Start Menu->All Program. It sets a series of enviroment variables which make msbuild work.
  • Martin Ingvar Kofoed Jensen
    Martin Ingvar Kofoed Jensen almost 14 years
    Also, you could try. normal instead of minimal
  • Tobias Knauss
    Tobias Knauss about 4 years
    Here's detailed comparison what information you get at which verbosity level: docs.microsoft.com/en-us/visualstudio/msbuild/…
  • Christian Storb
    Christian Storb over 3 years
    It works in Microsoft Visual Studio Enterprise 2019 version 16.8.5. When I set the MSBuild project build output verbosity from Minimal to Normal, the BeforeBuild and AfterBuild targets are executed.