Question

Building a project file with the Microsoft.Build.BuildEngine library seems to be ignoring the BeforeTargets attribute.

My project file has the following target:

<Target Name="MyTestTarget" BeforeTargets="Build">
  <Message Importance="High" Text="Running MyTestTarget" />
</Target>

and when I compile it with the following code (F#, but does the same in C#):

open System
open Microsoft.Build.BuildEngine
open Microsoft.Build.Framework

[<EntryPoint; STAThread>]
let main argv =
    let engine = new Engine()
    engine.RegisterLogger(new ConsoleLogger())
    let project = new Project(engine)
    project.Load("MyProjectFile.fsproj")
    let succeeded = engine.BuildProject(project, "Build")
    0

the target "MyTestTarget" is not run. Note that when I use MSBuild.exe 4.0 instead, "MyTestTarget" is correctly run.

I know that BeforeTargets was added in MSBuild 4.0, and would throw an exception if used with an older version of Microsoft.Build. But I haven't found anything about BeforeTargets being ignored by MSBuild 4.

Was it helpful?

Solution

You're using deprecated types, if you use the new types then it works:

let projects = new ProjectCollection()
projects.RegisterLogger(new ConsoleLogger())
let project = projects.LoadProject("MyProjectFile.fsproj")
let succeeded = project.Build("Build")

If you didn't see any warnings about deprecated types, then perhaps you are referencing older versions of the MSBuild assemblies.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top