views:

51

answers:

3

MSBuild is now the build engine for all supported languages in Visual Studio. I'd like to start taking advantage of that system in order to simplify a ton of logic I have crammed into pre- and post- build scripts.

To that end, I'd have to write the MSBuild script manually, because I'd like to have more than one target in a single file. I.e. I'd like to be able to just type "msbuild myproject.msbuild", and have the whole works build itself from scratch, like I can with, say, make.

But, I'd rather avoid having to maintain completely separate "project" files for Visual Studio given that it's all built on top of MSBuild now anyway.

Is there a way for me to do this?

A: 

In C#, at least, there's no reason to not add additional targets or even item groups. I'd just give it a try and see what happens.

Of course, you won't be able to edit such targets using the Visual Studio project designer.

John Saunders
Can multiple C++ targets be added?
Billy ONeal
@Billy: I have no idea. I haven't done C++ for years.
John Saunders
+1  A: 

My advice would be to just consolidate any targets into a common area that can be shared by all your projects, and call those targets from your standard projects.

<!-- Your normal projects -->
<Import Project="..\Library\Common.Targets" />
<Target Name="BeforeBuild">
    <CallTarget Targets="Common_BeforeBuild_DoFoo" />
</Target>
...

<!-- Library\Common.Targets -->
<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003"&gt;
    <Target Name="Common_BeforeBuild_DoFoo">
       ...
    </Target>
</Project>

That should also work for custom tasks that you may want to add, although we ended up using a similar approach to the way the MSBuild Community Tasks integrated themselves into MSBuild, it's open source so you can see how they did it, although this may be overkill for your requirements, and I probably wouldn't go this far again unless absolutely necessary.

Si
+1  A: 

In addition factoring common logic into commonly-imported targets as others have suggested, you can make a 'master' project that builds the others, see e.g. the examples at

http://msdn.microsoft.com/en-us/library/z7f65y0d.aspx

which show how to use the <MSBuild> task to invoke MSBuild on other projects (though I am unclear if each 'sub MSBuild' shares its dependency analysis or not).

Brian