views:

115

answers:

3

Hi, I'm trying to get a one-time costly target to run only when building a certain top-level project (that has many dependencies).

I have no problem on getting this working from plain msbuild / command line build. I do this with setting and InitialTargets on the project, or alternatively with

< BeforeBuild />.

The tricky part is with Visual Studio. When I build the same project from VS. VS runs the dependencies before even invoking my .csproj, so my target (which affects how the other projects are built) doesn't get to run until they have already been built.

Is there someway to force VS to run a target before invoking the dependencies?

I'm currently working around this, by running the same costly target from my most low-level project (the one that get's always built...) by using:

Condition=" $(BuildingInsideVisualStudio) "

Any ideas on how to get this done "properly"? Again, I'm looking for a solution that will work FROM VS.

+1  A: 

Why not override the <Target Name="BeforeBuild" /> target? That will be executed before the actual building of the project (since the Build target depends on BeforeBuild). Visual Studio is calling the Build target (or Rebuild), so overriding that target in the .csproj will cause it to be called first.

Unless you're looking to do something even earlier than that in the msbuild?

Agent_9191
BeforeBuild doesn't help.I did write this in the original question, however, StackOverflow decided this was an HTML tag and removed it...Anyway, the project's Build target doesn't even begin to run, so BeforeBuild will run only when VS decides to build the top-level project.Unfortunately, VS analyzes the dependencies and decides to build them BEFORE the project where I would want to run the special target.
damageboy
Are you talking about building a project, or building a solution? Because Visual Studio will pass the entire solution to MSBuild, which caches all the projects into a single project file that is then parsed in the build order specified. That may be the key to getting the appropriate target to build first.
Agent_9191
+1  A: 

Visual Studio uses an in proc compiler to help with the UX of the build process. Becuase of this there can be some difference between VS and MSBuild. Try setting the property UseHostCompilerIfAvailable to false in your project file. This will force VS to use MSBuild for the build. The drawback is performance inside Visual Studio.

You would use the follwing fragement in your .csproj (or .vbproj) file.

<PropertyGroup>
    <UseHostCompilerIfAvailable>false</UseHostCompilerIfAvailable>
</PropertyGroup>
Sayed Ibrahim Hashimi
+1  A: 

The only way to do this (if I understand the question) is to create a dummy project, that you tell VS to build first, that does whatever you want. VS will accept pretty much any MSBuild file if you give it a .csproj extension and the Build target.

dan
Thanks!Nice idea, this is indeed the best way to get the same behavior from INSIDE VS as well as when calling MSBuild directly...
damageboy